Member Avatar for Member #321605

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/ 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?

Dani AI

Generated

The mysql_fetch_assoc() warning means the fetch function received a non-resource (mysql_query returned false). Selecting the database, as recommended, removed that immediate error for . A subsequent blank page usually means the script produced no output or another error is being suppressed. The checklist below focuses on the concrete, verifiable causes that commonly follow this sequence.

Enable error output and check the query and template load right after the query. Example diagnostics (run temporarily on a development copy):

ini_set('display_errors', 1);
error_reporting(E_ALL);

if ($result === false) {
  die('Query failed: ' . mysql_error());
}

Confirm the template was actually read (file_get_contents returns false on failure) and that the processed template is echoed to the browser — building a string and never printing it will give a blank page. Also watch for variable-name mismatches (using $row in one place and $data in another) which silently prevent output when the wrong array is referenced.

Follow ’s hint about names: ensure template tokens exactly match the column names after any normalization your code does (case, spelling, brackets). For thumbnails, inspect the page source or the browser Network panel to see the generated image src values and try opening one directly — relative vs absolute paths, wrong upload location or file-permission issues are the usual culprits for “images not showing.”

Longer term, migrate to mysqli or PDO for clearer error handling and compatibility with current PHP versions (mysql_* is deprecated/removed in modern PHP). Short term, keep error reporting on and add per-query checks and template-read checks; once the query, template path, token names and output step line up, the gallery will render.

Recommended Answers

All 11 Replies

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

?>

Also use "LIMIT 0,15"

Member Avatar for Member #321605

Thanks for the prompt response.

The code now looks like this:

I am getting the error message:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/fhlinux177/e/ on line 25

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 for Member #321605

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/ on line 25

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.

Member Avatar for Member #321605

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?

I appreciate your help, thank you.

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 for Member #321605

Another step forward - thank you!

Result:

gallery.php code:

gallery_template.htm code:

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]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.