0

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)

Community
  • 1
  • 1
echoashu
  • 912
  • 3
  • 14
  • 31
  • 2
    http://dev.mysql.com/doc/refman/5.0/en/logical-operators.html | == is not a valid SQL operator – Ali Nov 10 '14 at 18:05

3 Answers3

1
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.

jherran
  • 3,337
  • 8
  • 37
  • 54
  • Your solution is for without prepare statement right (in my case use case 1) ? It worked. :-) I Did not understand having `email = '$value2'` while id<>$value1 did not required the same (the ` mark) Thanks – echoashu Nov 10 '14 at 18:54
  • When you compare a numeric field you don't need quotes. Instead if the field is a varchar, you need quotes. – jherran Nov 10 '14 at 19:01
  • Ok, but since m using a variable, dosen't it substitute numeric or varchar value in correct format? I know its dumb.. dont mine :D – echoashu Nov 10 '14 at 19:04
1

You have multiple problems in your query.

  1. Use ' = ' instead of ' == ' for assignments.
  2. In the first part You have assigned the query to variable $query, and running the query with variable $sql (wrong)

A modified code will look like this:

$query = "SELECT id, name from users WHERE id!='$value1' AND email='$value2'";
$result = $conn->query($query);
fortune
  • 3,361
  • 1
  • 20
  • 30
  • Sorry i wrote it wrrong 2) i do have the correct query in my code.. $result = $conn->query($query); corrected in question. I'll try wid single "=" and post the result Thanks – echoashu Nov 10 '14 at 18:46
  • Yes single "=" worked. That too without prepare statement use case. Simple Great!! :-) – echoashu Nov 10 '14 at 18:56
0

replace && With AND,

replace == with "="

its returning false, but its because you have a mysql syntax error, you could also try turn on error reporting to see the exception

Kypros
  • 2,997
  • 5
  • 21
  • 27