...

View Full Version : Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource



accog
09-03-2009, 10:23 AM
Hi, i'm very new to this whole business and i need a little help...
i was following a tutorial at http://www.oxyscripts.com/item-408.html and i have run into a problem.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/edit_process.php on line 12

Here is the code

<?php

/* edit_process.php */
include "config.php";

$db = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");
/* We have now connected, unless you got an error message */
/* We now select one news post, and bring it into some text boxes */
$query = "SELECT title, news FROM news WHERE id = $_GET[id]";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
/* This bit sets our data from each row as variables, to make it easier to display */
$title=$r["title"];
$news=$r["news"];
/* Now lets display the titles */
echo "<form name='edit_process.php' method='post' action='edit_save.php?id=$_GET[id]'>
<p>Title :
<input type='text' name='title' value='$title'>
</p>
<p>News :</p>
<p>
<textarea name='news' cols='40' rows='6'>$news</textarea>
</p>
<p>
<input type='submit' name='Submit' value='Save'>
</p>
</form>";
}
mysql_close($db);

?>

The problem appears to be on line twelve so i added in
or die ('Error: '.mysql_error ()); after it to see if i could pinpoint the problem. I was writing this all by hand to try and cram it into my brain so i tried copy/pasting to see if i made an error... same problem.
After adding in the above code it gave me a new error

Parse error: syntax error, unexpected ';' in /var/www/edit_process.php on line 12

I am thoroughly confused, any help would be greatly appreciated

Zangeel
09-03-2009, 11:03 AM
Try:



<?php

/* edit_process.php */
include "config.php";

$db = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");
/* We have now connected, unless you got an error message */
/* We now select one news post, and bring it into some text boxes */
$query = "SELECT title, news FROM news WHERE id = {$_GET['id']}";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
/* This bit sets our data from each row as variables, to make it easier to display */
$title=$r["title"];
$news=$r["news"];
/* Now lets display the titles */
echo "<form name='edit_process.php' method='post' action='edit_save.php?id={$_GET['id']}'>
<p>Title :
<input type='text' name='title' value='$title'>
</p>
<p>News :</p>
<p>
<textarea name='news' cols='40' rows='6'>$news</textarea>
</p>
<p>
<input type='submit' name='Submit' value='Save'>
</p>
</form>";
}
mysql_close($db);

?>

accog
09-03-2009, 11:07 AM
Same error :(

Zangeel
09-03-2009, 11:12 AM
The problem lies in the MySql resource.


$query = "SELECT `title`, `news` FROM `news` WHERE `id` = {$_GET['id']}";

Remember to use backsticks, because mysql has keywords that will confuse it with you syntax.

accog
09-03-2009, 11:25 AM
Thanks for the replies. I swapped that code for mine but i am still getting the same error :confused:

[vengeance]
09-03-2009, 11:50 AM
Try this:


$query = "SELECT `title`, `news` FROM `news` WHERE `id` = '".$_GET['id']."'";
$result = mysql_query($query) or die(mysql_error());
while($r = mysql_fetch_array($result))

You should remember to escape your $_GET['id'] too btw. Look up mysql_real_escape_string(); :)

accog
09-03-2009, 12:40 PM
;861054']Try this:


$query = "SELECT `title`, `news` FROM `news` WHERE `id` = '".$_GET['id']."'";
$result = mysql_query($query) or die(mysql_error());
while($r = mysql_fetch_array($result))

You should remember to escape your $_GET['id'] too btw. Look up mysql_real_escape_string(); :)

Great! It worked! Thanks so much!
I'll look into that.
Also, mind explainig what exactly was wrong and how it was fixed?

[vengeance]
09-03-2009, 06:04 PM
Great! It worked! Thanks so much!
I'll look into that.
Also, mind explainig what exactly was wrong and how it was fixed?

Well actually, I'm not much of a fancy coder. I just try out stuff until my errors go away. But like the code I provided you, I always mark the database columns with ` and the values of the input with '.

And that has always worked for me, so I don't know what else to tell you. :P

And I'm glad it worked for you!

accog
09-03-2009, 11:22 PM
Thanks for all the feedback!

Zangeel
09-03-2009, 11:43 PM
It concatenated the variable outside of the string. This was really a problem of poor syntax, always make sure to use backsticks, concatenate variables, and so on. When referencing arrays like the $_GET array, use single quotes to reference the index, like $_GET['lalala']



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum