dsg435665475 2014-11-10 18:02
浏览 73
已采纳

如何在MYSQLI中使用逻辑运算符 - PHP

I am writig a simple select statement, and since the connection object is mysqli, i cant use AND , && , OR like operators as it giving error

function getdata($value1,$value2)
{
global $conn;
$query = "SELECT id, name from users WHERE id!=$value1 && email==$value2 ";
$result = $conn->query($query);
}

This dosen't work :-(

Error: trying to get property of non object .. bla bla ..
Which means there something wrong with my sql statement, as when i try without logical operators it works, but of no use ofcourse .

Using prepare statement & bind parameters I referred to This thread SELECT in MYSQLI and tried with prepare statements.. but i'm doing something wrong i guess

$query=$conn->prepare("SELECT id, name from users WHERE id!=? && email==?");
    $query->bindParam("ss", $value1,$value2); // line 20 error points here
    $query->execute();

Error:

Call to a member function bindParam() on a non-object in /mydir/file.php line 20

P.S. var_dump($query); returns bool(false)

  • 写回答

3条回答 默认 最新

  • dongyanghan0556 2014-11-10 18:06
    关注
    SELECT id, name FROM users WHERE id <> $value1 AND email = '$value2'"
    

    <> and != is the same. AND and && is the same. You probably see AND and <> because is the standard in SQL. The issue was the equal operator is = instead of ==.

    In the second part of your question, ss is wrong. d is for type double, s is for strings.

    $query=$conn->prepare("SELECT id, name from users WHERE id != ? && email = ?");
    $query->bind_param('ds', $value1, $value2);
    $query->execute();
    

    You can check types on php bind_param function help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)