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>  ';

?>

Link to comment
Share on other sites

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>  ';

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.