我试图通过php mysqli在我的数据库上运行一个select查询但是,它一直返回false。我知道它不是查询语法,因为我直接从phpMyAdmin得到它并且它实际上返回一行。
这是我的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);
通过phpmyAdmin
查询相同的查询答案 0 :(得分:0)
您可以使用双引号而不是单引号
$query = $sql = "SELECT * FROM `zobo_vendors` WHERE vendorID = \"L00001\" LIMIT 0, 30 ";
答案 1 :(得分:0)
直接在查询中使用php变量
$query = "SELECT * FROM `zobo_vendors` WHERE vendorID = '$VendorID' LIMIT 0, 30 ";
$result = mysqli_query($conn,$query);
答案 2 :(得分:0)
实际上,它是您的查询语法。
您直接从phpmyadmin 进行了查询,如果将其放在单引号中,该查询将起作用-但您将其置于双引号中。>
代替:
“ SELECT * FROM
zobo_vendors
,其中vendorID = \'L00001 \'LIMIT 0,30”
要做:
'SELECT * FROM
zobo_vendors
,其中vendorID = \'L00001 \'LIMIT 0,30'
第一个无效,因为双引号字符串中的单引号不需要转义。因此,反斜杠字符不会被删除。
在phpmyadmin中,查询文本为:
SELECT * FROM
zobo_vendors
,其中vendorID ='L00001'LIMIT 0,30
您的查询文字是这样的:
SELECT * FROM
zobo_vendors
,其中vendorID = \'L00001 \'LIMIT 0,30
它带反斜杠进入MySQL!
或者,您可以删除反斜杠:
“ SELECT * FROM
zobo_vendors
,其中vendorID ='L00001'LIMIT 0,30”
ABS Rana的答案是另一个有效的选择。