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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?