$query  = "SELECT * FROM image_share WHERE to='$username'";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
    echo '<tr> 
<td width="100%" align="left" valign="top"> 
<div style="border-bottom:1px solid #6F6F6F;"><span style="color:#C0B184;"><a href=""><img src="'.$row['image_sharelink'].'" width="150" height="137"/></a></span><br/>
<br/> 
</td>
</tr>';
}

cant figure out why it wont work.
I got my error display on E_ALL

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

Dani AI

Generated

The error "mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource" means the value you passed into mysql_fetch_assoc() is not a valid result resource (usually because mysql_query() returned FALSE). As noted, first confirm a working DB connection and check the query result and error message from MySQL.

Use explicit error checking so you see why the query failed:

$result = mysql_query($query) or die('MySQL error: '.mysql_error());

Echo the generated SQL and run it directly in your MySQL client — that will show syntax problems. Common causes here are unescaped input (if $username contains a quote), or identifier issues (avoid ambiguous names or wrap column/table names in backticks). Changing the fetch function (as suggested) will not fix a failed query; mysql_fetch_array still needs a valid result resource.

Also consider moving to prepared statements (safer and avoids manual escaping). Example using mysqli:

$mysqli = new mysqli($host,$user,$pass,$db);
$stmt = $mysqli->prepare("SELECT image_sharelink FROM image_share WHERE `to` = ?");
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($link);
while ($stmt->fetch()) {
    echo '<img src="'.htmlspecialchars($link).'" width="150" height="137">';
}

Extra checks: var_dump($result) right after mysql_query() shows false vs resource; verify image paths actually exist; sanitize DB output with htmlspecialchars() for HTML attributes. The old mysql_* extension is deprecated/removed in modern PHP — use mysqli or PDO for new code (see mysqli prepared statements). For reference: mysql_query manual and mysqli::prepare.

Recommended Answers

All 2 Replies

Are you connecting to the database with a mysql_connect()?

You can echo out your $query variable, copy the SQL command and run the query directly against the database. This will return a specific error if there is a problem with the SQL statment.

You should also check the validity of the mysql_query() $result

if ($result = mysql_query($query)) {
  //
  // ToDo: Process Result Set
  //
} else {
  //
  // ToDo: Report Error
  //
}

Instead of "mysql_fetch_assoc" ...try using "mysql_fetch_array"
It's better to use the code this way....
.....
Also

$query  = "SELECT * FROM image_share WHERE to='$username'";
$result = mysql_query($query);
 
while($row = mysql_fetch_array($result))
{
    echo "<tr> 
<td width='100%' align='left' valign='top'> 
<div style='border-bottom:1px solid #6F6F6F;'><span style='color:#C0B184;'><a href=''><img src='".$row['image_sharelink']."' width='150' height='137'/></a></span><br/>
<br/> 
</td>
</tr>";
}
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.