I keep getting the following error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 17.

I'm trying to figure out if there's some problem with my SQL query statement, or what. When I try to print $result, nothing is displayed.

I've included my code below. Any help would be greatly appreciated. Thanks!

<?php
    require("include/db_functions.inc.php");
	get_db();
	
	beginSession();
	
	include 'template/header.php';
	echo '<h2>Member List</h2>';
	
	
	$sql = "SELECT MemberName, FirstName, LastName, Website, Location, Timezone, Bio FROM t_Members, t_MemberBio WHERE t_Members.MemberName=t_MemberBio.MemberName ORDER BY MemberName ASC";
	$result = mysql_query($sql, $con);	echo '999999999'.$result;
	$numEntries = 0;

	// Print member list
	echo "<ul class=\"memberlist\">";
	while ($row = mysql_fetch_array($result)) {
		$numEntries += 1;
		echo "<li>$row[MemberName]";
			echo '<ul>';
			echo "<li>$row[FirstName] $row[LastName]</li>";
			echo "<li>$row[Website]</li>";
			echo "<li>$row[Location]</li>";
			echo "<li>$row[Timezone]</li>";
			echo "<li>$row[Bio]</li>";
			echo '</ul>';
		echo '</li>';
	}
	echo '</ul>';
	
	if ($numEntries == 0)
		echo '<p><i>There are no members</i></p>';

    mysql_close($con);
?>

<p>Blog Links
<br />
<a href="show.php">See Blog</a>
<br/>
<a href="write_entry.php">Write Blog Entry</a></p>

<?php include 'template/footer.php'; ?>

Dani AI

Generated

The immediate cause in this thread was an invalid result resource returned by mysql_query. later discovered an ambiguous column name and resolved it; ’s suggestion to surface SQL errors was spot on, and both and pointed toward clearer JOINs and fully-qualified column names. Below are concise, practical next steps and a safe modern example you can apply now.

Check and reproduce the failure first

  • Verify the query return value before calling any fetch function; log the SQL string and run it directly in the database console to see MySQL’s response.
  • Distinguish “no rows” (empty result set) from “query failed” (query returns FALSE). Use the DB API’s error functions to capture the underlying error rather than relying on the fetch warning alone.
  • Confirm the DB connection variable is valid and that the right database/schema is selected.

Move to a safer API and avoid future headaches

  • The old mysql_* extension is removed in modern PHP. Migrate to PDO or mysqli to get prepared statements, better error handling, and clearer APIs.
  • Use explicit JOIN syntax and alias duplicate column names so result keys are unique and unambiguous.

Example (PDO) showing safe query + fetch:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=YOUR_DB;charset=utf8mb4', 'user', 'pass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$sql = "SELECT m.MemberName AS member_name, m.FirstName, m.LastName, b.Bio AS bio
        FROM t_Members AS m
        JOIN t_MemberBio AS b ON m.MemberName = b.MemberName
        ORDER BY m.MemberName";
$stmt = $pdo->query($sql);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // use $row['member_name'], $row['FirstName'], etc.
}
?>

Extra tips

  • Turn on error reporting during development and write errors to logs in production.
  • If still stuck, post the exact SQL and the DB error text (not just the fetch warning) so others can reproduce and help. For migration notes see the PHP docs: and mysqli connections.

Recommended Answers

All 4 Replies

Change this "$result = mysql_query($sql, $con); echo '999999999'.$result;" TO $result = mysql_query($sql,$con) or die(mysql_error()); this should tell you if you have a problem in the sql

Thanks! It turns out I needed to specify MemberName in the SELECT area, since the column is in both tables. It should have been:

$sql = "SELECT t_Members.MemberName, FirstName, LastName, Website, Location, Timezone, Bio FROM t_Members, t_MemberBio WHERE t_Members.MemberName=t_MemberBio.MemberName ORDER BY t_Members.MemberName ASC";
Member Avatar for Member #120589

Can you use an "INNER JOIN ... ON ..." instead?


//EDIT:

Sorry you must have posted while I still had the editor open.

Thanks! It turns out I needed to specify MemberName in the SELECT area, since the column is in both tables. It should have been:

$sql = "SELECT t_Members.MemberName, FirstName, LastName, Website, Location, Timezone, Bio FROM t_Members, t_MemberBio WHERE t_Members.MemberName=t_MemberBio.MemberName ORDER BY t_Members.MemberName ASC";

hi..once try to use table name before all fields which u want yo fetch... like...

$sql = "SELECT t_Members.MemberName, t_Members.FirstName, t_Members.LastName, t_Members.Website,t_Members. Location, t_Members.Timezone,t_Members. Bio FROM t_Members, t_MemberBio WHERE t_Members.MemberName=t_MemberBio.MemberName ORDER BY t_Members.MemberName ASC";

or

the above one of our frnd said us joins....it is the better method...

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.