繁体   English   中英

这个MYSQL查询有什么问题?

[英]What's wrong with this MYSQL query?

我有一个查询正在尝试运行,目前无法正常运行。

    require_once("functions.php");
$db_hostname = 'localhost'; 
$db_database = '***'; 
$db_username = '***'; 
$db_password = '***'; 
$db_status = 'not initialised'; 
$str_result = ''; 
$str_options = ''; 
$db_server = mysqli_connect($db_hostname, $db_username, $db_password); 

//write connection errors to external file
$handle = fopen("errorcon.txt", "w+");
$string = mysqli_connect_error($db_server);
fwrite($handle, $string );
fclose($handle);

$db_status = "connected";
//open database connection
mysqli_select_db($db_server, $db_database);
$pname = $_GET['pname'];
$uname = $_GET['uname'];
$ssaw = $_GET['ssaw'];
$feel = $_GET['feel'];
$loc = $_GET['loc'];

$handle = fopen("SearchCheck.txt", "w+");
$string = $pname . $uname . $ssaw . $feel . $loc;
fwrite($handle, $string );
fclose($handle);

$query = "SELECT tblPhoto.PID, tblPhoto.Name, tblUser.Username, tblPhoto.URL, tblPhoto.Description, tblPhoto.Season, tblPhoto.Feeling, tblPhoto.Location 
FROM tblPhoto
LEFT JOIN tblUser ON tblPhoto.UID = tblUser.UID
WHERE tblPhoto.Name LIKE '%$pname%' AND tblUser.Username LIKE '$uname' AND tblPhoto.Season LIKE '$ssaw' AND tblPhoto.Feeling LIKE '%$feel%' AND tblPhoto.Location LIKE '%$loc%'"; 

$results_array = array();
$results = mysqli_query($db_server, $query); 
//while ($row = mysqli_fetch_row($results)) {
 // $results_array[] = $row;
//}

while($row = mysqli_fetch_row($results)){
$table_data[]= array("PID"=>$row[0], "name"=>$row[1],"username"=>$row[2], "URL"=>$row[3], "desc"=>$row[4], "ssaw"=>$row[5], "feel"=>$row[6], "loc"=>$row[7]);
}
echo json_encode($table_data);

$handle = fopen("error2.txt", "w+");
$string = mysqli_error($db_server);
fwrite($handle, $string );
fclose($handle);


mysqli_close($db_server); 

?> 

$results_array = array();
$results = mysqli_query($db_server, $query); 

变量是在URL中传递给php的GET变量,并且可以正常工作,在此特定测试pname中,uname,feel和loc设置为*,而ssaw设置为winter。

当我通过将变量手动设置为上述值的PHPMyAdmin运行查询的测试运行时,收到以下(无用)错误消息:

“#1064-您的SQL语法有错误;请在与您的MySQL服务器版本相对应的手册中查找在'附近使用的正确语法。” SELECT tblPhoto.PID,tblPhoto.Name,tblUser.Username,tblPhoto.URL,tblPhoto .De'在第1行“

任何人都知道为什么错误消息不正确显示吗? 还是有鹰眼并在查询中发现一些错误?

作为参考,我在PHPMyAdmin中运行的测试运行已从变量编辑为

"SELECT tblPhoto.PID, tblPhoto.Name, tblUser.Username, tblPhoto.URL, tblPhoto.Description, tblPhoto.Season, tblPhoto.Feeling, tblPhoto.Location 
FROM tblPhoto
LEFT JOIN tblUser ON tblPhoto.UID = tblUser.UID
WHERE tblPhoto.Name LIKE '%*%' AND tblUser.Username LIKE '*' AND tblPhoto.Season LIKE 'winter' AND tblPhoto.Feeling LIKE '%*%' AND tblPhoto.Location LIKE '%*%'"

编辑:应Mihai的要求包含整个php文件

无法在PHP中工作,但要考虑一下...我不知道在打开的字符串中是否存在跨行多行的错误包装问题,并且以某种方式忽略了cr / lf并从一行连接到下一行

$query = "SELECT tblPhoto.PID, tblPhoto.Name, tblUser.Username, tblPhoto.URL, tblPhoto.Description, tblPhoto.Season, tblPhoto.Feeling, tblPhoto.Location 
FROM tblPhoto
LEFT JOIN tblUser ON tblPhoto.UID = tblUser.UID
WHERE tblPhoto.Name LIKE '%$pname%' AND tblUser.Username LIKE '$uname' AND tblPhoto.Season LIKE '$ssaw' AND tblPhoto.Feeling LIKE '%$feel%' AND tblPhoto.Location LIKE '%$loc%'"; 

使它像

...Feeling, tblPhoto.LocationFROM tblPhotoLEFT JOIN tblUser ON tblPhoto.UID = tblUser.UIDWHERE

注意,“ FROM”,“ LEFT JOIN”和“ WHERE”紧挨着先前的字段引用,没有空格?

另外,还有一个注意事项。 您对Users表有一个LEFT JOIN,但是在WHERE子句中包含Users表,迫使它进入INNER JOIN,因此在users表中需要一个匹配项。 不知道这是否是故意的,但是如果您正在寻找一个没有其他元素的特定用户,那么您将不会得到任何行...再次,这只是一个旁注,而不是您的语法错误本身。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM