简体   繁体   English

mysql_query不返回资源

[英]mysql_query does not return a resource

I've been all up and down StackExchange, and much of the internet's cornucopia of lesser sites, looking for any good reason my code doesn't work, but this one has me stumped. 我一直在StackExchange上下徘徊,互联网在许多较小的站点上都是聚宝盆,寻找我的代码无法正常工作的任何正当理由,但这让我很困惑。 I'd like to display the total number of rows in a particular MySQL table, and set the count as a variable, to use later in the script. 我想显示特定MySQL表中的总行数,并将计数设置为变量,以便稍后在脚本中使用。 When I run the following, the script dies, and I get a PHP warning, telling me that mysql_result() expects parameter 1 to be resource, string given. 当我运行以下命令时,脚本死了,并且得到了PHP警告,告诉我mysql_result()期望参数1为资源,给定字符串。

$conn = mysql_connect('mysql_server', 'username', 'password');
if (!$conn) {
    die('Connect Error ' . mysql_error());
}
mysql_select_db('my_database', $conn);
if (!mysql_select_db('my_database')) {
    die('Could not select database: ' . mysql_error());
}
$max_count_query = ("SELECT COUNT(*) FROM table");
// Perform Query
$max_count_action = mysql_query($max_count_query);
$mcount = mysql_result($max_count_action, 0, 0);
printf("\nNumber of Records to Process: ", $mcount);

What does the collective genius of StackOverflow think? StackOverflow的集体天才在想什么?

In response to the comments, I have another mini-slab of code to offer: 为了回应这些评论,我提供了另一个迷你代码块:

$conn = mysql_connect('127.0.0.1', 'username', 'password');
if (!$conn) {
    die('Connect Error ' . mysql_error());
}
mysql_select_db('my_database', $conn);
if (!mysql_select_db('my_database')) {
    die('Could not select database: ' . mysql_error());
}
$max_count_query = ("SELECT COUNT(*) FROM directory_nap");
// Perform Query
$max_count_action = mysql_query($max_count_query);
if (!$max_count_action){
    die('mysql query error: ' . mysql_error());
    }
$mcount = mysql_result($max_count_action, 0, 0);
if (!$mcount){
    die('mysql result error: ' . mysql_error());
    }
printf("\nNumber of Records to Process: ", $mcount);

Provided the table tablename is just a sample (see answer by bitfox), there's nothing wrong with your code. 只要table tablename只是一个示例(请参阅bitfox的答案),您的代码就没有问题。 I can use the same code on my test server and get results by changing the table name to one that I know exists in my own db. 我可以在测试服务器上使用相同的代码,并通过将表名更改为我知道自己数据库中存在的表名来获得结果。

What is most troubling, however, is that you indicate the error says mysql_result() expects parameter 1 to be resource, string given -- if your SQL has an error, mysql_query returns boolean ( docs ), not a string. 然而,最麻烦的是,您指出错误,表明mysql_result() expects parameter 1 to be resource, string given -如果您的SQL出错,则mysql_query返回布尔值( docs ),而不是字符串。 So, you're either not showing the same test code as you're actually using, or you've given us an inaccurate error message. 因此,您可能没有显示与实际使用的测试代码相同的代码,或者给我们提供了不正确的错误消息。

At some point, you must be assigning a string into the variable $max_count_action . 在某个时候,您必须将字符串分配给变量$max_count_action Here's what I get when I send a query with an intentional problem: Warning: mysql_result() expects parameter 1 to be resource, boolean given -- note "boolean", not string. 这是我发送有意问题的查询时得到的结果: Warning: mysql_result() expects parameter 1 to be resource, boolean given -注意“布尔值”,而不是字符串。

So, I think your first step is to choose a different table name. 因此,我认为您的第一步是选择其他表名。 That said, if you're using a reserved word as a table or column name you can still access it by surrounding the string in the backtick (`) character: 就是说,如果您使用保留字作为表名或列名,则仍然可以通过在反引号(`)字符中加上字符串来访问它:

SELECT COUNT(*) FROM `table`

Second step is to see what's happening to $max_count_action to turn it into a string. 第二步是查看$max_count_action发生了什么,将其转换为字符串。 Finally, use mysql_error consistently to debug, I would suggest doing something a little nicer with it than die for production code, however. 最后,始终如一地使用mysql_error进行调试,但是我建议对它进行一些比die生产代码更好的事情。

// working code on my test server
$max_count_query = ("SELECT COUNT(*) FROM users");
$max_count_action = mysql_query($max_count_query);
$mcount = $max_count_action ? mysql_result($max_count_action, 0) : 'Error: '.mysql_error();
print "\nNumber of Records to Process: ". $mcount;

it's very likely that the error is here: 错误很可能在这里:

SELECT COUNT(*) FROM table

" table " is a reserved word of SQL language. ”是SQL语言的保留字。 You should change it. 您应该更改它。

The cause of the problem is not clear from the code sample (it could be a wrong table name, the parentheses around the query string, and so on), however there are a number of inaccuracies in it. 从代码示例中并不能确定问题的原因(可能是错误的表名,查询字符串周围的括号等),但是其中有许多不正确之处。 You can try the following code, it should at least fix a couple side bugs and give you more details on the error: 您可以尝试以下代码,它至少应修复几个附带的错误,并为您提供有关该错误的更多详细信息:

$conn = mysql_connect('mysql_server', 'username', 'password');
if ($conn === false) {
    die('Connect Error ' . mysql_error($conn));
}
if (mysql_select_db('my_database', $conn) === false) {
    die('Could not select database: ' . mysql_error($conn));
}
$max_count_query = "SELECT COUNT(*) FROM table";
// Perform Query
$max_count_action = mysql_query($max_count_query, $conn);
if($max_count_action === false) {
    die('Query error ' . mysql_error($conn));
}
$mcount = mysql_result($max_count_action, 0, 0);
if($mcount === false) {
    die('Result retrieval error ' . mysql_error($conn));
}
printf("\nNumber of Records to Process: %s", $mcount);
mysql_free_result($max_count_action);

Hope this helps, bye! 希望这会有所帮助,再见!

Is the 'table' in your question a placeholder or a real name in your code? 您问题中的“表格”是代码中的占位符还是真实姓名? Try to change another name of your 'table' table, otherwise I would like to try mysql_fetch_array instead of mysql_result 尝试更改“表”表的其他名称,否则我想尝试使用mysql_fetch_array而不是mysql_result

$max_count_query = "SELECT COUNT(*) FROM t";
// Perform Query
$max_count_action = mysql_query($max_count_query) or die (mysql_error());
$mcount = mysql_fetch_array($max_count_action);
printf("\nNumber of Records to Process: ", $mcount[0]);
$conn = mysql_connect('mysql_server', 'username', 'password');
if (!$conn) {
    die('Connect Error ' . mysql_error());
}

This is OK, but the connect and coditional don't need to be split across 2 lines 可以,但是不需要将连接和条件分为两行

mysql_select_db('my_database', $conn);

Again, this is questionable. 同样,这是有问题的。 If you're using multiple databases then referencing databases in your SQL is much simpler and safer than tracking state within your PHP code. 如果您正在使用多个数据库,则在SQL中引用数据库比在PHP代码中跟踪状态要简单和安全得多。

if (!mysql_select_db('my_database')) {
   die('Could not select database: ' . mysql_error());
}

Haven't you already done that? 你还没有这样做吗?

$max_count_query = ("SELECT COUNT(*) FROM table");

Why the brackets? 为什么用括号?

// Perform Query

Isn't that obvious from the code? 从代码中不是很明显吗?

$max_count_action = mysql_query($max_count_query);
$mcount = mysql_result($max_count_action, 0, 0);

It would be neater to pass the db handle to the mysql_query call. 将数据库句柄传递给mysql_query调用会更加巧妙。

You checked for an error after connecting, you checked for an error after switching database, but you don't check for an error after mysql_query() ? 连接后检查错误,切换数据库后检查错误,但是mysql_query()之后不检查错误吗?

printf("\nNumber of Records to Process: ", $mcount);

There is no placeholder in the format string for the mcount argument.Try: mcount参数的格式字符串中没有占位符。

printf("\nNumber of Records to Process: %d", $mcount);

I get a PHP warning, telling me that mysql_result() expects parameter 1 to be resource, string given. 我得到一个PHP警告,告诉我mysql_result()期望参数1为资源,给定字符串。

Then the code you are running is not the code you've shown us; 那么您正在运行的代码不是您向我们展示的代码; mysql_query() will not return a string. mysql_query()将不会返回字符串。

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

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