我正在运行的查询从PhpMyAdmin运行时返回2个结果,但从PHP脚本运行时不返回任何内容:
SELECT event_contacts.*, tasks.all_forms_in, tasks.drive_folder_empty FROM event_contacts, tasks WHERE event_contacts.eid = tasks.eid AND tasks.all_forms_in = 'y' AND tasks.drive_folder_empty = 'completed' AND event_contacts.email_sent_date IS NULL
我的PHP代码如下:
$query = mysql_query("SELECT event_contacts.*, tasks.all_forms_in, tasks.drive_folder_empty FROM event_contacts, tasks WHERE event_contacts.eid = tasks.eid AND tasks.all_forms_in = 'y' AND tasks.drive_folder_empty = 'completed' AND event_contacts.email_sent_date IS NULL");
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
while ($row = mysql_fetch_array($query)){
// do stuff
}
}
为什么从PhpMyAdmin运行时返回2行,但从PHP脚本运行时“查询为空”死了?
答案 0 :(得分:1)
您需要在while语句中使用$result
而不是$query
;
$query = mysql_query("SELECT event_contacts.*, tasks.all_forms_in, tasks.drive_folder_empty FROM event_contacts, tasks WHERE event_contacts.eid = tasks.eid AND tasks.all_forms_in = 'y' AND tasks.drive_folder_empty = 'completed' AND event_contacts.email_sent_date IS NULL");
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
while ($row = mysql_fetch_array($result)){
// do stuff
}
答案 1 :(得分:0)
在你的第一个
$query = mysql_query("SELECT event_contacts.*, tasks.all_forms_in, tasks.drive_folder_empty FROM event_contacts, tasks WHERE event_contacts.eid = tasks.eid AND tasks.all_forms_in = 'y' AND tasks.drive_folder_empty = 'completed' AND event_contacts.email_sent_date IS NULL");
必须只是一个字符串。像这样:
$query = "SELECT event_contacts.*, tasks.all_forms_in, tasks.drive_folder_empty FROM event_contacts, tasks WHERE event_contacts.eid = tasks.eid AND tasks.all_forms_in = 'y' AND tasks.drive_folder_empty = 'completed' AND event_contacts.email_sent_date IS NULL";
(没有mysql_query()函数)。您正在下一个mysql_query()调用中执行查询。
另外,正如@Umit Apari所说,必须使用mysql_query结果调用mysql_fetch_array()。
但是,今天,你不应该使用mysql_ *函数,而是使用PDO函数。