DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource (http://www.daniweb.com/forums/thread121796.html)

Borderline Apr 30th, 2008 2:26 pm
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
 
Hi There

I'm a newcomer to php trying to develop a basic photo album. I have created a simple template in htm, and am trying to use php to show 15 thumbnails per page. However, I am experiencing the error message: "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/fhlinux177/e/equinefocus.co.uk/user/htdocs/personal/gallery.php on line 26"

$db = mysql_connect($hostname, $username, $password) or die ("Unable to connect to MySQL");
$sql = "SELECT url, thumb, ref, info FROM others LIMIT 15";

$result = mysql_query($sql, $db);

$table_template = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/style/gallery_template.htm');

while ($data = mysql_fetch_assoc($result)) {
  $mytable = $table_template;
  foreach ($data as $key => $value) {
    $mytable = str_replace("[".strtoupper($key)."]", $value, $mytable);
  }
}

?>

LINE 26 is while ($data = mysql_fetch_assoc($result)) {

Can anyone point me in the right direction with this problem, please? What is causing it, and how can I correct it?

phper Apr 30th, 2008 2:38 pm
Re: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
 
I would use the following code:

<?php
$db = mysql_connect($hostname, $username, $password) or die ("Unable to connect to MySQL");
$sql = "SELECT url, thumb, ref, info FROM others LIMIT 15";

$result = mysql_query($sql);

$table_template = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/style/gallery_template.htm');

while ($data = mysql_fetch_assoc($result)) {
  $mytable = $table_template;
  foreach ($data as $key => $value) {
    $mytable = str_replace("[".strtoupper($key)."]", $value, $mytable);
  }
}

?>

phper Apr 30th, 2008 2:39 pm
Re: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
 
Also use "LIMIT 0,15"

Borderline Apr 30th, 2008 2:50 pm
Re: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
 
Thanks for the prompt response.

The code now looks like this: http://www.equinefocus.co.uk/personal/gallery.pdf

I am getting the error message:

Quote:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/fhlinux177/e/equinefocus.co.uk/user/htdocs/personal/gallery.php on line 25

phper Apr 30th, 2008 6:51 pm
Re: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
 
Try using this code:

while ($data = mysql_fetch_array($result, MYSQL_ASSOC)) {

Just replace your while statment with the one above.

Let me know how it goes!!

Regards,
Alex

Borderline May 2nd, 2008 1:00 pm
Re: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
 
Really sorry, it hasn't worked. Error message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/fhlinux177/e/equinefocus.co.uk/user/htdocs/personal/gallery.php on line 25

phper May 2nd, 2008 5:34 pm
Re: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
 
Your not selecting a database in your code.

Try adding

mysql_select_db('databasename');

khess May 2nd, 2008 9:32 pm
Re: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
 
Be sure that your column names are correct, including capitalization, and that the table name is correct. You can also do a SELECT * FROM others; for testing until you figure out what the problem is.

Borderline May 3rd, 2008 1:34 pm
Re: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
 
Many thanks for your assistance - adding the line regarding the database removed the error message!

Unfortunately, now, I have a blank page, not the photo gallery I was hoping for. Does anyone have any theories on this?

http://www.equinefocus.co.uk/personal/gallery.pdf

I appreciate your help, thank you.

phper May 3rd, 2008 2:08 pm
Re: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
 
replace your while loop with this:

while ($data = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo '<img src="'.$row['url'].'" /><br/>';
}

This should loop through the images and display them. Obviously edit the <IMG> tag to suit e.g width / height / border, etc.


All times are GMT -4. The time now is 8:34 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC