...

View Full Version : Error - Warning: mysql_fetch_array():



Msuth
08-02-2012, 02:48 PM
Here is my code: http://pastebin.com/QuxX11fP
Here is my site: http://msuth.co.uk/simpsons/index2.php

I have this working Locally but when I tried to get it to work on an external server I get hit with this error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/msuthco/public_html/simpsons/index2.php on line 32

Basically, all I want my site to do is display a new Quote everytime the page is refreshed!

Arcticwarrio
08-02-2012, 02:55 PM
change this

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

to this


while ($row = mysql_fetch_array($result) {

Msuth
08-02-2012, 03:00 PM
Nope...


Parse error: syntax error, unexpected '{' in /home/msuthco/public_html/simpsons/index2.php on line 32

AndrewGSW
08-02-2012, 03:21 PM
Assuming you've modified your connection to not refer to localhost.. try


$result = mysql_query($query, $link);

or remove mysql_selectdb and place the database detail in the original connection $link.

Msuth
08-02-2012, 04:10 PM
Hi Andrew, When I spoke to my host (Zyma) they told me to leave the host as localhost.

I'll have a look into that aspect thanks!

Arcticwarrio
08-02-2012, 04:35 PM
make a file called db.php and put this in it



<?php
// Database File
function Q($query) {
$Username = 'INFO';
$Password = 'INFO';
$Database = 'rand_text';
$Host = 'localhost';
global $queries, $queries_time, $queries_count;
$queries_count++;
mysql_connect($Host, $Username, $Password) or die ( 'Unable to connect to the server.<br>' );
mysql_select_db($Database) or die ( 'Unable to select the database.<br>' );
$queries .= 'Line <b>'. __LINE__ . '</b> of file <b>' .__FILE__ . '</b> requested <b>' . $query . '</b>';
$time_overall = round(microtime(),4);
if (!$result = mysql_query($query)) {
echo '<pre>Error on line <b>'. __LINE__ . '</b> of file <b>' . __FILE__ . '</b> requesting <b>' . $query . '</b></pre><br>';
$queries .= ' <b>ERROR OCCURED</b><br>';
} else {
$queries .= '<br>';
}
$time2_overall = round(microtime(), 4);
$gen_overall = $time2_overall - $time_overall;
$queries_time = substr($queries_time + $gen_overall,0,6) ;
mysql_close() or die ( 'Unable to disconnect from the server.<br>' );
return $result;
}
?>




then in your page put this at the top:



include ("db.php");
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


now try this:



$Q1 = Q("SELECT * FROM quote ORDER BY Rand() LIMIT 1");

// Display records from the table
echo "<table>";
while ($row = mysql_fetch_array($Q1)) {
echo "<tr><td>".$row[0]."</td></tr><tr><td>".$row[1]."</td></tr>";
}
echo "</table>";


you can also put this at the bottom of your page if you want it:


<?php echo "<b>$queries_count</b> Database queries in <b>$queries_time</b> seconds"; ?>

Msuth
08-02-2012, 05:10 PM
Thank you so much!

Full site can be seen at http://msuth.co.uk/hosted/simpsons/

ECoode
08-02-2012, 10:20 PM
Thank you so much!

Full site can be seen at http://msuth.co.uk/hosted/simpsons/

You should add button which generates another random quote :)

Arcticwarrio
08-02-2012, 10:48 PM
You should add button which generates another random quote :)

F5 already does generate a new quote ;)

Arcticwarrio
08-02-2012, 10:53 PM
p.s not that it really matters but just for your info:
when your only dealing with 1 row removing the while statement only uses the 1st row



$Q1 = Q("SELECT * FROM quote ORDER BY Rand() LIMIT 1");
$row = mysql_fetch_array($Q1); // removed while

// Display 1 record from the table
echo "<table><tr><td>".$row[0]."</td></tr><tr><td>".$row[1]."</td></tr></table>";

Arcticwarrio
08-03-2012, 01:39 PM
looks good :)

if you want people to retrieve a specific quote you could do this too:


<?php
if (isset($_GET['Quote'])){
$Q1 = Q("SELECT * FROM quote WHERE `Id` = ".$_GET['Quote']);
}else{
$Q1 = Q("SELECT * FROM quote ORDER BY Rand() LIMIT 1");
}
$row = mysql_fetch_array($Q1);
// Display record from the table
echo "<form><table>";
echo "<tr><td><input name=\"Quote\" type=\"text\" size=\"3\"/></td><td><input name=\"Submit\" type=\"Submit\" value=\"Get Quote\"/></td></tr>";
echo "<tr><td>".$row[0]."</td></tr><tr><td>".$row[1]."</td></tr>";
echo "</table></form>";
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum