简体   繁体   English

难道PHP的MySQL不支持?

[英]Does PHP MySQL not support IN?

Ok, I have a rather long query that only fails when I add an IN statement. 好的,我有一个相当长的查询,只有在添加一个IN语句时才会失败。 The following query works great in MySQL console directly but returns nothing when done in PHP. 以下查询可直接在MySQL控制台中很好地工作,但在PHP中完成时不返回任何内容。

$db = mysql_connect('localhost', 'something', 'somepassword');

$query = "SELECT * 
FROM alt_table f 
INNER JOIN alt_fac_table_c cl ON (f.id = cl.alt_table2_ida) 
INNER JOIN alt_table c ON (cl.alt_table_idb = c.id) 
LEFT OUTER JOIN alt_table_contacts_c con ON (f.id = con.alt_table_ida) 
LEFT OUTER JOIN contacts cc ON (con.alt_table_idb = cc.id) 
WHERE c.name = 'myname' 
AND f.state IN ('AL','FL') ORDER BY fname;";

$result = mysql_query($query);

The problem is not with connecting with the database and its not with the query itself. 问题不在于连接数据库,也不在于查询本身。 Obviously I have renamed a couple things but the query is the same. 显然我已经重命名了几件事,但是查询是相同的。 In PHP it returns nothing (No errors, no nothing) but in SSH/Console it returns all of the rows I need. 在PHP中,它什么也不返回(没有错误,什么都没有),但是在SSH /控制台中,它返回我需要的所有行。

If I remove "AND f.state IN ('AL','FL') ORDER BY fname;";" the query works but gives me every single state. So the big question is... does PHP have a Problem with the IN statement? 如果我删除“ AND f.state IN('AL','FL')ORDER BY fname;“;”,该查询有效,但为我提供了每个状态。因此,最大的问题是……PHP是否存在问题? IN语句?

UPDATE FIX It ended up being a problem with the post data. UPDATE FIX最终导致发布数据出现问题。 I was posting the single quotes for 'AL',FL'. 我在发布'AL',FL'的单引号。 It was apparently encoding them strangely upon post. 显然,在发布时对它们进行了奇怪的编码。 I assume some sort of security feature. 我假设某种安全功能。 I took the quote out of the post and added it it server side PHp instead and it works. 我从帖子中删除了报价,而是将其添加到服务器端PHp,并且可以正常工作。

You've neglected to select a database... 您忽略了选择数据库...

mysql_select_db('database_name', $db);

This should go just after the call to the mysql_connect() function. 这应该在调用mysql_connect()函数之后执行。

http://uk.php.net/mysql_select_db http://uk.php.net/mysql_select_db

It will provide a true/false on success so you can use the following to ensure it connects. 它将在成功时提供对错信息,因此您可以使用以下内容确保连接成功。

if( ! mysql_select_db('database_name', $db) ) {
    die('failed to select database');
}

In addition to the above, you can modify the following line 除上述内容外,您还可以修改以下行

$result = mysql_query($query);

To be more like the following. 如下所示。 This will show you any errors returned, and in your case a No database selected error 这将向您显示返回的任何错误,并且在您的情况下显示No database selected错误

$result = mysql_query($query) or die(mysql_error());

From http://php.net/manual/en/function.mysql-query.php http://php.net/manual/en/function.mysql-query.php

The query string should not end with a semicolon.

Try removing just that and see if it works. 尝试删除它,看看是否可行。

It ended up being a problem with the post data. 最终导致发布数据出现问题。 I was posting the single quotes for 'AL',FL'. 我在发布'AL',FL'的单引号。 It was apparently encoding them strangely upon post. 显然,在发布时对它们进行了奇怪的编码。 I assume some sort of security feature. 我假设某种安全功能。 I took the quote out of the post and added it it server side PHp instead and it works. 我从帖子中删除了报价,而是将其添加到服务器端PHp,并且可以正常工作。

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

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