Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_error() not displaying an error

I am trying to debug my code but mysql_error() isn't displaying anything. I know there is something wrong, because when I write

or die("ERROR");

It displays ERROR. So the problem must be with that line of code. When I write

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

like image 935
nitrous Avatar asked Dec 20 '13 00:12

nitrous


People also ask

How do I show errors in MySQL?

The SHOW COUNT(*) ERRORS statement displays the number of errors. You can also retrieve this number from the error_count variable: SHOW COUNT(*) ERRORS; SELECT @@error_count; SHOW ERRORS and error_count apply only to errors, not warnings or notes.

How can I see SQL error in php?

You can just use: $this->db_link->error to get the last error message. For all errors use $this->db_link->error_list .

How show MySQL error message in PHP?

Description ¶ Instead, use mysql_error() to retrieve the error text. Note that this function only returns the error text from the most recently executed MySQL function (not including mysql_error() and mysql_errno()), so if you want to use it, make sure you check the value before calling another MySQL function.

Could not run MySQL server has gone away?

The most common reason for the MySQL server has gone away error is that the server timed out and closed the connection. In this case, you normally get one of the following error codes (which one you get is operating system-dependent). The client couldn't send a question to the server.


1 Answers

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.

mysql_fetch_row() returns false when there are no more rows this is what is triggering your die() statement despite there being no error.

like image 128
Sammitch Avatar answered Oct 13 '22 10:10

Sammitch