User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 375,225 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,225 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 751 | Replies: 10
Reply
Join Date: Apr 2008
Posts: 18
Reputation: Borderline is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Borderline Borderline is offline Offline
Newbie Poster

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

  #1  
Apr 30th, 2008
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?
Last edited by Borderline : Apr 30th, 2008 at 2:27 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2006
Location: South Wales
Posts: 159
Reputation: phper is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
phper's Avatar
phper phper is offline Offline
Junior Poster

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

  #2  
Apr 30th, 2008
I would use the following code:

  1. <?php
  2. $db = mysql_connect($hostname, $username, $password) or die ("Unable to connect to MySQL");
  3. $sql = "SELECT url, thumb, ref, info FROM others LIMIT 15";
  4.  
  5. $result = mysql_query($sql);
  6.  
  7. $table_template = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/style/gallery_template.htm');
  8.  
  9. while ($data = mysql_fetch_assoc($result)) {
  10. $mytable = $table_template;
  11. foreach ($data as $key => $value) {
  12. $mytable = str_replace("[".strtoupper($key)."]", $value, $mytable);
  13. }
  14. }
  15.  
  16. ?>
If you find my post useful please add to my reputation!! Thanks!

ajtrichards web solutions
http://www.ajtrichards.co.uk
Reply With Quote  
Join Date: Nov 2006
Location: South Wales
Posts: 159
Reputation: phper is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
phper's Avatar
phper phper is offline Offline
Junior Poster

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

  #3  
Apr 30th, 2008
Also use "LIMIT 0,15"
If you find my post useful please add to my reputation!! Thanks!

ajtrichards web solutions
http://www.ajtrichards.co.uk
Reply With Quote  
Join Date: Apr 2008
Posts: 18
Reputation: Borderline is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Borderline Borderline is offline Offline
Newbie Poster

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

  #4  
Apr 30th, 2008
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
Reply With Quote  
Join Date: Nov 2006
Location: South Wales
Posts: 159
Reputation: phper is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
phper's Avatar
phper phper is offline Offline
Junior Poster

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

  #5  
Apr 30th, 2008
Try using this code:

  1. 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
If you find my post useful please add to my reputation!! Thanks!

ajtrichards web solutions
http://www.ajtrichards.co.uk
Reply With Quote  
Join Date: Apr 2008
Posts: 18
Reputation: Borderline is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Borderline Borderline is offline Offline
Newbie Poster

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

  #6  
May 2nd, 2008
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
Reply With Quote  
Join Date: Nov 2006
Location: South Wales
Posts: 159
Reputation: phper is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
phper's Avatar
phper phper is offline Offline
Junior Poster

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

  #7  
May 2nd, 2008
Your not selecting a database in your code.

Try adding

  1. mysql_select_db('databasename');
If you find my post useful please add to my reputation!! Thanks!

ajtrichards web solutions
http://www.ajtrichards.co.uk
Reply With Quote  
Join Date: Apr 2008
Location: Tulsa
Posts: 34
Reputation: khess is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Staff Writer
khess's Avatar
khess khess is offline Offline
Light Poster

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

  #8  
May 2nd, 2008
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.
Ken Hess
Linux Blogger/Columnist
Reply With Quote  
Join Date: Apr 2008
Posts: 18
Reputation: Borderline is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Borderline Borderline is offline Offline
Newbie Poster

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

  #9  
May 3rd, 2008
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.
Reply With Quote  
Join Date: Nov 2006
Location: South Wales
Posts: 159
Reputation: phper is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
phper's Avatar
phper phper is offline Offline
Junior Poster

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

  #10  
May 3rd, 2008
replace your while loop with this:

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

This should loop through the images and display them. Obviously edit the <IMG> tag to suit e.g width / height / border, etc.
If you find my post useful please add to my reputation!! Thanks!

ajtrichards web solutions
http://www.ajtrichards.co.uk
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 3:48 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC