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?

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

?>

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

Your not selecting a database in your code.

Try adding

mysql_select_db('databasename');

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.

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.

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!



HI AGAIN


';

$template_bottom = '


- - -

Our company

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

Edited 4 Years Ago by Dani: Code

This article has been dead for over six months. Start a new discussion instead.