i'm trying to populate a select list from a mysql database. my line numbers on the code are 90 to 104. the error i'm getting is

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\capstone\admin\admin_curriculum.php on line 99

my code is:

$con = mysqli_connect("localhost","root","");
$sql = "SELECT * FROM PROGRAM";
$result = mysqli_query($con, $sql);
if (!$con)
{
	die('Could not connect: ' . mysql_error());
}
else {
	mysqli_select_db($con, "IT_SOL_CTR");
	while($row = mysql_fetch_assoc($result))
	{
		echo "<option value=\"{$row['PROGRAM_ID']}\">
                          {$row['PROGRAM_ID']}</option>";
								  
	}
}

Dani AI

Generated

The warning means the value handed to the fetch call was not a valid result set. In this thread the immediate causes were: the query ran before a database was selected and the old mysql_* and mysqli_* APIs were mixed. ’s suggestion to select the DB before running the query fixed the problem for . For long-term safety, use one API (mysqli or PDO) consistently — mysql_* was removed in PHP 7.

A concise, correct procedural mysqli example (different from the original code) that selects the DB up-front, checks for errors, and escapes output:

$con = mysqli_connect('localhost', 'root', '', 'IT_SOL_CTR');
if (!$con) {
    die('Connect error: ' . mysqli_connect_error());
}

$sql = "SELECT PROGRAM_ID FROM PROGRAM";
$result = mysqli_query($con, $sql);
if (!$result) {
    die('Query error: ' . mysqli_error($con));
}

while ($row = mysqli_fetch_assoc($result)) {
    echo '<option value="'.htmlspecialchars($row['PROGRAM_ID']).'">'
         .htmlspecialchars($row['PROGRAM_ID']).'</option>';
}

mysqli_free_result($result);
mysqli_close($con);

Troubleshooting tips: always check the return value of mysqli_query() and use mysqli_error() when it fails; var_dump($result) helps diagnose whether you got false. Enable strict mysqli error reporting during development with mysqli_report(...). Prefer prepared statements or PDO for production to avoid SQL injection, and avoid mixing mysql_* and mysqli_* calls. See the PHP manual for mysqli_connect and mysqli_fetch_assoc for details and examples.

Recommended Answers

All 2 Replies

You're performing the query on line 3 but you don't select a database until line 9. You must select the db before you perform the query.

thanks, that got it working

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.