1,105,399 Community Members

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

Member Avatar
Borderline
Junior Poster in Training
61 posts since Apr 2008
Reputation Points: 1 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
phper
Posting Whiz in Training
209 posts since Nov 2006
Reputation Points: 9 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
  }
}

?>
Member Avatar
phper
Posting Whiz in Training
209 posts since Nov 2006
Reputation Points: 9 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

Also use "LIMIT 0,15"

Member Avatar
Borderline
Junior Poster in Training
61 posts since Apr 2008
Reputation Points: 1 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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:

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

Member Avatar
phper
Posting Whiz in Training
209 posts since Nov 2006
Reputation Points: 9 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Borderline
Junior Poster in Training
61 posts since Apr 2008
Reputation Points: 1 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
phper
Posting Whiz in Training
209 posts since Nov 2006
Reputation Points: 9 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

Your not selecting a database in your code.

Try adding

mysql_select_db('databasename');
Member Avatar
khess
Master Poster
798 posts since Apr 2008
Reputation Points: 81 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 9 [?]
 
0
 

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.

Member Avatar
Borderline
Junior Poster in Training
61 posts since Apr 2008
Reputation Points: 1 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
phper
Posting Whiz in Training
209 posts since Nov 2006
Reputation Points: 9 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Borderline
Junior Poster in Training
61 posts since Apr 2008
Reputation Points: 1 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Another step forward - thank you!

Result: http://www.equinefocus.co.uk/personal/gallery.php

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

gallery_template.htm code:
http://www.equinefocus.co.uk/personal/gallery_template.pdf

My issue now, is the error message at the bottom of the gallery.php page, and also the thumbnail issues, which is presumably caused by the template.htm page.

This was the code given to me in another forum, but I don't know how to make the two pages interact correctly. I am assuming the [REF] etc sections are causing the problems, but don't know what they are supposed to be replaced with to pull the relevant info from the database. Any thoughts?

Many thanks for assisting this bumbling amateur - it was given to me as a simple solution for what I wanted, but I'm not finding it that at this stage!

Member Avatar
yashmistrey
Newbie Poster
1 post since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 



HI AGAIN


';

$template_bottom = '


- - -

Our company

e-mail: [email]x@y.com[/email]

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article