I am trying to debug my code but mysql_error() isn't displaying anything. I know there is something wrong, because when I write
我正在尝试调试我的代码,但mysql_error()没有显示任何内容。我知道有些不对劲,因为我写的时候
or die("ERROR");
It displays ERROR. So the problem must be with that line of code. When I write
它显示ERROR。所以问题必须在那行代码上。当我写作
or die(mysql_error());
It shows up blank. Here is my code for the line that I think has the error:
它显示空白。这是我认为有错误的行的代码:
while ($rows = mysql_fetch_array($sql6) or die(mysql_error())) {
Now here is the full code block:
现在这里是完整的代码块:
$sql6 = mysql_query("SELECT * FROM replies WHERE thread_id = $thread_id");
$numRows = mysql_num_rows($sql6);
$replies = '';
if ($numRows < 1) {
$replies = "There are no replies yet, you can make the first!";
} else {
while ($rows = mysql_fetch_array($sql6) or die(mysql_error())) {
$reply_content = $rows['5'];
$reply_username = $rows['7'];
$reply_date = $rows['8'];
$reply_author_id = $rows['4'];
$sql9 = mysql_query("SELECT * FROM users WHERE id = '$reply_author_id'");
$numRows = mysql_num_rows($sql9);
if ($numRows < 1) {
while ($rows = mysql_fetch_array($sql9)) {
$reply_user_fn = $rows['first_name'];
$reply_user_ln = $rows['last_name'];
$reply_user_id = $rows['id'];
$reply_user_pp = $rows['profile_pic'];
$reply_user_lvl = $rows['user_level'];
$reply_user_threads = $rows['threads'];
$reply_user_email = $rows['email'];
$replies .= '<tr><td valign="top" style="border: 1px solid black;">';
$replies .= '<div class="reply" style="min-height: 125px;"';
$replies .= '<h2>Re: ' . $thread_title . '</h2><br />';
$replies .= '<em>by: ' . $reply_username . ' - ' . $reply_date . '</em><hr />';
$replies .= $reply_content;
$replies .= '</div></td>';
$replies .= '<td valign="top" width="200" align="center" style="border: 1px solid black;"';
$replies .= '<img src="userdata/profile_pics/' . $reply_user_pp . '" width="80" height="80"><br />';
$replies .= '<a href="profile.php?u=' .$reply_username . '" style="color: black;">'. $reply_username .'</a><br />';
$replies .= '<a href="profile.php?u=' .$reply_username . '" style="color: black;">' . $reply_user_fn.' ' .$reply_user_ln . '</a><br />';
$replies .= 'Threads: ' . $reply_user_threads . ' <br />Level: '. $reply_user_lvl .'<br />Sign up date: ' . $reply_user_email/*PUT SIGNUP DATE*/ .'';
$replies .= '<input type="button" name="addfriend" value="Add Friend">';
$replies .= '</td>';
}
}
}
}
What am I doing wrong and why won't PHP display the mysql error? Thanks
我做错了什么,为什么PHP不显示mysql错误?谢谢
5 个解决方案
#1
5
Do not put or die()
inside of a loop condition. The loop condition becoming false
is what signals the loop to end, and that will also trigger die()
every time your loop completes.
不要在循环条件内放置或死()。循环条件变为false表示循环结束,并且每次循环完成时也会触发die()。
mysql_fetch_row()
returns false when there are no more rows this is what is triggering your die()
statement despite there being no error.
mysql_fetch_row()在没有更多行时返回false,这是触发你的die()语句的内容,尽管没有错误。
#2
1
You can use a try..catch
block to understand why your code is not working correctly, like this:
您可以使用try..catch块来了解代码无法正常工作的原因,如下所示:
try {
while ($rows = mysql_fetch_array($sql6)) {
// your code ..
}
} catch (Exception $e) {
echo $e->getMessage();
echo "---";
echo mysql_error();
}
Note that, as you say or die
is getting executed, which means that there is an error in your code somewhere, and not a mysql error
. And, therefore, the above code will capture that exception and display it for you. Also, it will display any mysql_error
, if it has occurred until that point for reference, so that you can compare the two strings.
请注意,正如您所说或死将被执行,这意味着您的代码中存在错误,而不是mysql错误。因此,上面的代码将捕获该异常并为您显示。此外,它将显示任何mysql_error,如果它已发生到该点以供参考,以便您可以比较这两个字符串。
#3
0
There is no mysql_error()
because mysql_query()
is probably correct.
没有mysql_error()因为mysql_query()可能是正确的。
You do mysql_error()
immediately after the query, because it will show a message if the query was wrong, not the PHP code that gets the rows.
您在查询后立即执行mysql_error(),因为如果查询错误,它将显示一条消息,而不是获取行的PHP代码。
$conn = mysql_connect(....);
$sql6 = mysql_query("...", $conn) or die(mysql_error($conn));
// ...
while ($rows = mysql_fetch_array()) { ... } // There's no mysql_error() here, it will return a data set (or not enter the while code block if it's null9
#4
0
If still no mysql error, after changing the "or die" position after "mysql_query"..do you get $thread_id from somewhere?? like
如果仍然没有mysql错误,在“mysql_query”之后更改“或死”位置之后..你从某个地方获得$ thread_id吗?喜欢
$thread_id=$_GET['thread_id'];
Also, if you use, multiple database connections, inside mysql_error('$connection_name'))
另外,如果您在mysql_error('$ connection_name')中使用多个数据库连接
Like:
喜欢:
//Connect to database
$main = mysql_connect(DB_HST, DB_USR, DB_PSS);
mysql_select_db(DB_DB);
$main2 = mysql_connect(DB_HST2, DB_USR2, DB_PSS2);
mysql_select_db(DB_DB2);
return the error executing the query to the right database..
将执行查询的错误返回给正确的数据库..
mysql_query("query") or die (mysql_error($main));
when using multiple database connection the query should look like:
使用多个数据库连接时,查询应如下所示:
$query = mysql_query("SELECT * FROM table_name WHERE id=1", $connection_name);
mysql_fetch_array($query);
#5
0
I had similar problem of not seeing any error messages from mysql. After research it appeared that the problem has got nothing to do with PHP itself, but with mysql server configuration. The default value of the variable lc_messages_dir pointed to non existing directory. After adding a line in mysqld.cnf, then restarted the mysql server, and finally it worked. For me the following was the right one:
我有类似的问题,没有看到来自mysql的任何错误消息。经过研究,似乎问题与PHP本身无关,而是与mysql服务器配置有关。变量lc_messages_dir的缺省值指向非现有目录。在mysqld.cnf中添加一行后,重新启动mysql服务器,最后工作了。对我来说,以下是正确的:
lc_messages_dir=/usr/share/mysql
It is described in mysql reference manual: https://dev.mysql.com/doc/refman/5.7/en/error-message-language.html
它在mysql参考手册中描述:https://dev.mysql.com/doc/refman/5.7/en/error-message-language.html
#1
5
Do not put or die()
inside of a loop condition. The loop condition becoming false
is what signals the loop to end, and that will also trigger die()
every time your loop completes.
不要在循环条件内放置或死()。循环条件变为false表示循环结束,并且每次循环完成时也会触发die()。
mysql_fetch_row()
returns false when there are no more rows this is what is triggering your die()
statement despite there being no error.
mysql_fetch_row()在没有更多行时返回false,这是触发你的die()语句的内容,尽管没有错误。
#2
1
You can use a try..catch
block to understand why your code is not working correctly, like this:
您可以使用try..catch块来了解代码无法正常工作的原因,如下所示:
try {
while ($rows = mysql_fetch_array($sql6)) {
// your code ..
}
} catch (Exception $e) {
echo $e->getMessage();
echo "---";
echo mysql_error();
}
Note that, as you say or die
is getting executed, which means that there is an error in your code somewhere, and not a mysql error
. And, therefore, the above code will capture that exception and display it for you. Also, it will display any mysql_error
, if it has occurred until that point for reference, so that you can compare the two strings.
请注意,正如您所说或死将被执行,这意味着您的代码中存在错误,而不是mysql错误。因此,上面的代码将捕获该异常并为您显示。此外,它将显示任何mysql_error,如果它已发生到该点以供参考,以便您可以比较这两个字符串。
#3
0
There is no mysql_error()
because mysql_query()
is probably correct.
没有mysql_error()因为mysql_query()可能是正确的。
You do mysql_error()
immediately after the query, because it will show a message if the query was wrong, not the PHP code that gets the rows.
您在查询后立即执行mysql_error(),因为如果查询错误,它将显示一条消息,而不是获取行的PHP代码。
$conn = mysql_connect(....);
$sql6 = mysql_query("...", $conn) or die(mysql_error($conn));
// ...
while ($rows = mysql_fetch_array()) { ... } // There's no mysql_error() here, it will return a data set (or not enter the while code block if it's null9
#4
0
If still no mysql error, after changing the "or die" position after "mysql_query"..do you get $thread_id from somewhere?? like
如果仍然没有mysql错误,在“mysql_query”之后更改“或死”位置之后..你从某个地方获得$ thread_id吗?喜欢
$thread_id=$_GET['thread_id'];
Also, if you use, multiple database connections, inside mysql_error('$connection_name'))
另外,如果您在mysql_error('$ connection_name')中使用多个数据库连接
Like:
喜欢:
//Connect to database
$main = mysql_connect(DB_HST, DB_USR, DB_PSS);
mysql_select_db(DB_DB);
$main2 = mysql_connect(DB_HST2, DB_USR2, DB_PSS2);
mysql_select_db(DB_DB2);
return the error executing the query to the right database..
将执行查询的错误返回给正确的数据库..
mysql_query("query") or die (mysql_error($main));
when using multiple database connection the query should look like:
使用多个数据库连接时,查询应如下所示:
$query = mysql_query("SELECT * FROM table_name WHERE id=1", $connection_name);
mysql_fetch_array($query);
#5
0
I had similar problem of not seeing any error messages from mysql. After research it appeared that the problem has got nothing to do with PHP itself, but with mysql server configuration. The default value of the variable lc_messages_dir pointed to non existing directory. After adding a line in mysqld.cnf, then restarted the mysql server, and finally it worked. For me the following was the right one:
我有类似的问题,没有看到来自mysql的任何错误消息。经过研究,似乎问题与PHP本身无关,而是与mysql服务器配置有关。变量lc_messages_dir的缺省值指向非现有目录。在mysqld.cnf中添加一行后,重新启动mysql服务器,最后工作了。对我来说,以下是正确的:
lc_messages_dir=/usr/share/mysql
It is described in mysql reference manual: https://dev.mysql.com/doc/refman/5.7/en/error-message-language.html
它在mysql参考手册中描述:https://dev.mysql.com/doc/refman/5.7/en/error-message-language.html