Reputation: 21
I'm trying to run a select query on my db via php mysqli but, it keeps returning false. I know it's not query syntax since I got it straight from phpMyAdmin and there it actually returns a row.
Here's my php:
//connect
$conn = mysqli_connect($server,$username,$password,$dbname);
//test connection
if(!$conn){
die("Connection Failed:"." ".mysqli_connect_error());
}
$VendorID = mysqli_real_escape_string($conn,$_GET['vendorID']);
$VendorPass = mysqli_real_escape_string($conn,$_GET['vendorPass']);
$query = $sql = "SELECT * FROM `zobo_vendors` WHERE vendorID = \'L00001\' LIMIT 0, 30 ";
$result = mysqli_query($conn,$query);
print(mysqli_num_rows($result));
if(mysqli_num_rows($result) == 1){
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
if($VendorPass == $row['vendorPass']){
date_default_timezone_set("Africa/Johannesburg");
$day = date("d");
$monthNum = date("m");
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('M');
$date = $day." ".$monthName.", ".date("h:i");
echo "<script>window.AppInventor.setWebViewString('CorrectPass');</script>";
echo $date;
}else{
echo "<script>window.AppInventor.setWebViewString('WrongPass');</script>";
}
}else{
echo "<script>window.AppInventor.setWebViewString('NonExist');</script>";
}
if($result === FALSE) {
die("Uh oh something went wrong");
}
mysqli_close($conn);
Here's the same query via phpmyAdmin
Upvotes: 0
Views: 1792
Reputation: 21223
Actually, it is your query syntax.
You took a query straight from phpmyadmin that would have worked if you put it inside single quotes - but you put it inside double quotes.
Instead of:
"SELECT * FROM
zobo_vendors
WHERE vendorID = \'L00001\' LIMIT 0, 30 "
Do:
'SELECT * FROM
zobo_vendors
WHERE vendorID = \'L00001\' LIMIT 0, 30 '
The first one doesn't work because in a double-quoted string, single-quote does not need escaping. Because of this, the backslash character does not get removed.
In phpmyadmin, the text of query was this:
SELECT * FROM
zobo_vendors
WHERE vendorID = 'L00001' LIMIT 0, 30
Your query text literally is this:
SELECT * FROM
zobo_vendors
WHERE vendorID = \'L00001\' LIMIT 0, 30
It goes to MySQL with backslashes in it!
Alternatively, you could have removed the backslashes:
"SELECT * FROM
zobo_vendors
WHERE vendorID = 'L00001' LIMIT 0, 30 "
ABS Rana's answer is another valid alternative.
Upvotes: 1
Reputation: 5
you can use double quotation instead of single quotation
$query = $sql = "SELECT * FROM `zobo_vendors` WHERE vendorID = \"L00001\" LIMIT 0, 30 ";
Upvotes: 0