为什么以下 SQL 命令不能与 PHP 中的 mysql_query 一起使用?
SELECT *
FROM `enzymes`
INNER JOIN `compounds`
ON compounds.compound_id = enzymes.compound_id
WHERE `ec` LIKE '1.11%'
它适用于 phpmyadmin 和 mysql 工作台。
无论我是否使用 mysql_real_escape_string 反引号所有内容并在适当的位置添加数据库名称,它都不会工作。 phpmyadmin php 代码也不起作用。
错误:警告:mysql_fetch_assoc() 期望参数 1 为资源,布尔值给出......
对于所有其他查询资源依赖函数
Upd< /strong>:
case 'ec':
$dbl = mysql_connect(DB_ADDRESS, DB_USER, DB_PASSWORD);
mysql_select_db("kegg", $dbl);
//a)connection is fine
//b)tried with explicitly providing the db name vs with pre-selection
$sql = "SELECT * FROM enzymes INNER JOIN `compounds` ON compounds.compound_id=enzymes.compound_id";
print_r($sql);
$result = mysql_query( $sql, $dbl);
print mysql_error ($dbl);
while ($row = mysql_fetch_assoc($result)) {
print_r($row)
$items[ $row['name'] ] = $row['compound_id'];
}
,,,
解决方案: 谢谢大家,- Brad 首先给出了关键的答案。
$sql = "SELECT enzymes.*, compounds.*\n"
. "FROM enzymes\n"
. " INNER JOIN compounds\n"
. " ON compounds.compound_id = enzymes.compound_id\n"
. "WHERE enzymes.ec LIKE '1.11%' LIMIT 0, 30 ";
最近出现了一个类似的主题,建议它仅在选择特定字段时有效。当需要所有数据时,关键的解决方案是解决歧义:
enzymes.*, compounds.*
SELECT *
FROM `enzymes`
INNER JOIN `compounds`
ON compounds.compound_id = enzymes.compound_id
WHERE `ec` LIKE '1.11%'
it works in phpmyadmin and mysql workbench.
It won't work regardless whether I backtick everything use mysql_real_escape_string and add the database name where appropriate. the phpmyadmin php code won't work either.
ERRORS: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in....
same error for all other query-resource dependent functions
Upd:
case 'ec':
$dbl = mysql_connect(DB_ADDRESS, DB_USER, DB_PASSWORD);
mysql_select_db("kegg", $dbl);
//a)connection is fine
//b)tried with explicitly providing the db name vs with pre-selection
$sql = "SELECT * FROM enzymes INNER JOIN `compounds` ON compounds.compound_id=enzymes.compound_id";
print_r($sql);
$result = mysql_query( $sql, $dbl);
print mysql_error ($dbl);
while ($row = mysql_fetch_assoc($result)) {
print_r($row)
$items[ $row['name'] ] = $row['compound_id'];
}
,,,
Solution: Thanks everyone,- Brad gave the crucial answer first.
$sql = "SELECT enzymes.*, compounds.*\n"
. "FROM enzymes\n"
. " INNER JOIN compounds\n"
. " ON compounds.compound_id = enzymes.compound_id\n"
. "WHERE enzymes.ec LIKE '1.11%' LIMIT 0, 30 ";
A similar topic came up recently where it was suggested that it only works when selecting specific fields. The crucial solution when all data is desired is to resolve ambiguities to:
enzymes.*, compounds.*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
鉴于该错误,mysql_query 返回布尔值 FALSE,指示错误。试试这个:
看看到底是什么错误。
Given that error, mysql_query is returning a boolean FALSE, indicating an error. Try this:
to see exactly what the error is.
我的直觉告诉我数据库被你的专栏引用混淆了。当您开始进行联接时,尤其是当您的列名称可能匹配时,最好明确您正在选择、比较等内容。例如
但是,在不知道发生的确切错误的情况下,这只能是猜测。更新
现在我看到了错误,请确保您正在测试正确的查询响应。当您调用
mysql_query
(或您使用的任何内容)时,请在尝试获取结果之前检查资源是否失败。大多数连接库都有 *_error 或 *_last_error 方法,您可以调用该方法来查看导致失败的原因。My gut is telling me the database is confused by your column references. When you start doing joins, and especially when you have columns names that may match, it's good practice to be explicit on what you're selecting, comparing, etc. e.g.
But, without knowing the exact error that's occurring, this can only be a guess.UPDATE
Now that I see the error, make sure you're testing for a proper query response. When you call
mysql_query
(or whatever you use), check the resource for failure before trying to fetch the result. Most connection libraries have a *_error or *_last_error method you can call to see what caused the failure.有多种原因导致查询可以在 phpMyAdmin 等环境中运行,但不能通过 PHP 的 mysql 函数运行。对于初学者,请确保您已通过 PHP 的
mysql_select_db 函数
使用USE
命令选择了正确的数据库 (文档)。使用 phpMyAdmin 时,该操作会在后台自动执行,而 PHP 脚本则不然。如果您在查询中指定数据库的名称,则不需要执行上一步,但是,如果数据库名称发生更改,您的代码将更加难以维护。
要指定表,请执行以下操作:
SELECT field1, field2 FROM database_name.table_name WHERE...
其次,如果您错误地使用双引号或单引号,则可能无法解析变量或出现其他此类问题:这将导致您的查询无效。代码示例将有助于确定是否属于这种情况。
There are a number of reasons that a query will work in environments like phpMyAdmin but not through PHP's mysql functions. For starters, make sure you've selected the proper database with a
USE
command via PHP'smysql_select_db function
(docs). When using phpMyAdmin, that is being performed in the background automatically, not so with a PHP script.If you specify the names of the database in your query, the previous step won't be required, BUT, your code will be that much harder to maintain if there is ever a change in the database name.
To specify the table, do this:
SELECT field1, field2 FROM database_name.table_name WHERE...
Second, if you are using double or single quotes incorrectly, the variables might not be parsed or other such issues: this will cause your query to be invalid. A code sample would help determine if this is the case.
这表明您应该在 mysql_query 中传递一个 mySQL 资源。
看一下:
打开连接时您将获得 $link_identifier:
This indicated that you should pass an mySQL resource in whith the mysql_query.
look at:
You will get the $link_identifier when opening a connection:
避免 MySQL INNER JOIN 中的列不明确错误。
请尝试:
如果您得到相似的 id 对,则问题就出在这里。
Avoid column ambiguous error in MySQL INNER JOIN.
Please try:
And if you'll get similar pairs of ids, the problem is here.