Jump to content

mysql_fetch_array(): supplied argument is not a valid MySQL result resource


yogen

Recommended Posts

<?php
include"scripts/connect_to_mysql.php";

$id = "";
$thought = "";

$thoughts = '<table border="0" align="center" cellpadding="4">
              <tr>  ';

$sql = mysql_query("SELECT * FROM thoughts ORDER BY DESC LIMIT 1");
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$thought = $row["thoughts"];


$thoughts .= '<td><div align="center" style="font-family: Arial, Helvetica, sans-serif;"><font size="-2">' . $thought . '</font><br /></td>';
}

$thoughts .= '              </tr>
            </table>  ';

?>

try this, I'm guessing thoughts is not the table name. See what the error reads.

 

<?php
include"scripts/connect_to_mysql.php";

$id = "";
$thought = "";

$thoughts = '<table border="0" align="center" cellpadding="4">
              <tr>  ';

$sql = mysql_query("SELECT * FROM thoughts ORDER BY DESC LIMIT 1") or die ('Error: '.mysql_error ());
while($row = mysql_fetch_array($sql)){
$id = $row['id'];
$thought = $row['thoughts'];


$thoughts .= '<td><div align="center" style="font-family: Arial, Helvetica, sans-serif;"><font size="-2">' . $thought . '</font><br /></td>';
}

$thoughts .= '              </tr>
            </table>  ';

?>

You should always check your queries succeed and return results before using them. The typical syntax is:

 

$sql = "SELECT foo FROM bar";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    // $result is now good to use
  } else {
    // no results returned - handle error
  }
} else {
  // query failed - handle error
}

 

I would watch your variable naming conventions as well. mysql_query() does not return SQL, it returns a result set or false.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.