PDA

View Full Version : mysql_fetch_array error


zetsumei
December 21st, 2006, 12:28 AM
I'm getting this mysql_fetch_array error in my php script and I can't figure out what it is.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /opt/lampp/htdocs/quotes.php on line 13

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","************");

//select which database you want to edit
mysql_select_db("otaku_quotes");

//select the table
$result = mysql_query("select * from quotes");

//grab all the content
while($r=mysql_fetch_array($result))
{
//the format is $variable = $r["nameofmysqlcolumn"];
//modify these to match your mysql table columns

$title=$r["title"];
$message=$r["message"];
$who=$r["who"];

//display the row
echo "<div class='quote'>$message</div>
<div class='quote_author'>$who</div>";
}
?>

invalid
December 21st, 2006, 12:35 AM
Try$result = mysql_query("select * from `quotes`");

zetsumei
December 21st, 2006, 12:40 AM
that didnt work...i still get the error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /opt/lampp/htdocs/quotes.php on line 13

invalid
December 21st, 2006, 12:52 AM
Hm.. are you sure the table is populated?
Try this and see what you getecho mysql_num_rows($result);
Also, try changing to this:$conn = mysql_connect("localhost","root","************");
mysql_select_db("otaku_quotes", $conn);

zetsumei
December 21st, 2006, 12:55 AM
nope i added $conn = mysql_connect( ); and the $conn into the select db part and its still giving me the same error.

invalid
December 21st, 2006, 01:08 AM
The only other thing I can suggest are a series of debug statements$conn = mysql_connect("localhost","root","************");
if (!$conn) {
die('mysql_connect error : ' . mysql_error());
}

//select which database you want to edit
$db = mysql_select_db("otaku_quotes", $conn);
if (!$db) {
die('mysql_select_db error : ' . mysql_error());
}

//select the table
$result = mysql_query("select * from `quotes`") or die('mysql_query error : ' . mysql_error());

zetsumei
December 21st, 2006, 01:11 AM
I fixed that last error, I had a o instead of 0...But now it just says $random_row instead of pulling the quote from the db

$random_row

<?
$cnx = mysql_connect("localhost","root","**************");
mysql_select_db("otaku_quotes", $cnx);

$sql = mysql_query("select * from quotes") or die (mysql_error());

while($row = mysql_fetch_array($sql)) {
$row_array[] = $row['message'];
}

mysql_close($cnx);

$random_row = $row_array[rand(0, count($row_array) -1)];
echo '<div class="quotes">$random_row</div>';
?>

invalid
December 21st, 2006, 01:14 AM
I think you mean for 'o' to be '0'.

zetsumei
December 21st, 2006, 01:15 AM
i noticed that I edited my last post with what happens now -_-, sigh php/mysql hates me -_-

invalid
December 21st, 2006, 01:17 AM
But now it just says $random_row instead of pulling the quote from the db

Change echo '<div class="quotes">$random_row</div>';toecho "<div class=\"quotes\">$random_row</div>";

zetsumei
December 21st, 2006, 01:21 AM
Finally It Works

thanks for the help