Hi,

I am a newbie to PHP and HTML and over here i am trying to display my sql results in a table format like this,

ID. Name. Contact
---------------------------------------------
----ROW1----------------------------------
----ROW2----------------------------------

any site recommendations where i can get help from? thanks alot.

Dani AI

Generated

Quick addendum for future readers: the PHP warning "supplied argument is not a valid MySQL result resource" simply means the database call returned false instead of a usable result. 's suggestion to inspect the SQL was on target, and confirmed the problem was fixed — below are safe troubleshooting steps, a concise modern example, and a few gotchas to avoid if you revisit this topic years later.

Checklist to diagnose the problem

  • Verify the DB connection is successful and that the SQL string you send is exactly what you expect (paste it into your DB client).
  • Capture the database API error (use your API’s error reporting — e.g., mysqli or PDO error reporting) rather than ignoring it.
  • Make sure the variable holding the query exists and hasn’t been overwritten.
  • Don’t mix fetch styles: use an associative fetch if you plan to reference column names.
  • Build the HTML table header once, loop to emit rows, and close the table after the loop.
  • Always escape output for HTML (use htmlspecialchars) to avoid broken markup or XSS.

A short, modern example (PDO, associative fetch and proper table building)

try {
    $pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8mb4', 'dbuser', 'dbpass', [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);
    $stmt = $pdo->query('SELECT id, first_name, last_name, company FROM users ORDER BY first_name ASC');
    echo '<table border="1">';
    echo '<tr><th>ID</th><th>First</th><th>Last</th><th>Company</th></tr>';
    foreach ($stmt as $row) {
        echo '<tr>';
        echo '<td>' . htmlspecialchars($row['id']) . '</td>';
        echo '<td>' . htmlspecialchars($row['first_name']) . '</td>';
        echo '<td>' . htmlspecialchars($row['last_name']) . '</td>';
        echo '<td>' . htmlspecialchars($row['company']) . '</td>';
        echo '</tr>';
    }
    echo '</table>';
} catch (PDOException $e) {
    error_log('DB error: ' . $e->getMessage());
    echo 'A database error occurred.';
}

Final notes: the old mysql_* extension is removed in modern PHP — prefer mysqli or PDO. If you still see the original warning, focus first on the SQL and the connection; fixing those almost always resolves the fetch-time error.

Recommended Answers

All 4 Replies

http://www.w3schools.com/PHP/php_mysql_intro.asp

That should help! :)

Ah. :) thanks alot. but i have another problem again. what do they mean by,
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource ?

here is part of the code,

ORDER BY firstName ASC";

    $result = mysql_query($query); 

echo "<table border='1'>
        <tr>
        <th>ID</th>
        <th>FirstName</th>
        <th>LastName</th>
        <th>CompanyName</th>"

             while($row=mysql_fetch_row($result))
            {
                echo "<tr>";
                echo "<td>" .$row['ID']."</td>";
                echo "<td>" .$row['FirstName']."</td>";
                echo "<td>" .$row['LastName']."</td>";
                echo "<td>" .$row['CompanyName']."</td>";
echo "</table>";

so sorry to trouble you again. mind telling me where is my mistake? :)
Thanks again.

The simplest way to solve that error is to print out the query and execute it phpmyadmin / mysql console. You will know the exact problem. You can also try $result = mysql_query($query) or die (mysql_error()); to get the error message. Also, mysql_fetch_row should be used if you are using numeric index for your array. ie., $row[0], $row[1] and so on. If you want associated names, you should use mysql_fetch_array or mysql_fetch_assoc.

The simplest way to solve that error is to print out the query and execute it phpmyadmin / mysql console. You will know the exact problem. You can also try $result = mysql_query($query) or die (mysql_error()); to get the error message. Also, mysql_fetch_row should be used if you are using numeric index for your array. ie., $row[0], $row[1] and so on. If you want associated names, you should use mysql_fetch_array or mysql_fetch_assoc.

Ah, thanks alot. problem solved. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.