这个MYSQL查询有什么问题?

时间:2015-01-07 18:46:51

标签: mysql phpmyadmin

我有一个我正在尝试运行的查询,但是无法正常运行。

    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服务器版本对应的手册,以便在'ttlPhoto.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文件

1 个答案:

答案 0 :(得分:0)

不是在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"和"在哪里"是否在前一个字段引用旁边没有任何空格?

另外,只是旁注。您有一个LEFT JOIN到Users表,但在WHERE子句中包含Users表,强制将其转换为INNER JOIN,因此需要在users表中匹配。不知道这是否是故意的,但是如果你正在寻找一个没有其他元素的特定用户,你就不会有任何行......再次,这是一个侧面注释,本质上不是你的语法错误。