i am trying to run this code but i am not getting anything from the mysql_fetch_assoc($result)
it is just blank can anyone help me
thanks in advance

<html>
<body>
<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="hayden"; // Mysql password 
$db_name="ecng3020"; // Database name 
$tbl_name="students"; // Table name
$tbl_name_1="staff"; // Table name

$sql="SELECT * FROM $tbl_name WHERE id_no='$username' and password='$password='";

if (mysql_connect("$host","$username","$password"))
	echo "<p>connected to mysql</p>";
else
	echo "<p>could not connect</p>";

if (mysql_select_db("$db_name"))
	echo "<p>selected db</p>";
else
	echo "<p>could not select db</p>";
	


if ($result = mysql_query($sql))
	echo "<p>success</p>";
else
	echo "<p>fail</p>";

while ($row = mysql_fetch_assoc($result))
{
	echo "<p>$row[0] $row[1] $row[2] $row[3] $row[4] $row[5]";
}


?>
</body>
</html>

Dani AI

Generated

Short summary for : the fetch loop is blank because the query is returning no rows (so there is nothing for mysql_fetch_assoc to return). Two likely root causes in the posted script are (1) the SQL string is malformed and mixes the script's DB-login variables with the intended form input, and (2) the script prints numeric indexes while using an associative fetch. spotted the malformed SQL and pointed at associative vs numeric indexing — both are relevant.

Quick, practical checklist to debug and fix the immediate problem:

  • Confirm which variables hold the database credentials and which hold the user input; use distinct names (for example, $dbUser / $dbPass vs $formUser / $formPass) so values do not get mixed.
  • Output the complete constructed SQL during development to verify it looks as expected, then run that same query directly in the database client to see whether it returns rows.
  • After a query, check for errors from the database driver (for the old extension use mysql_error(), for mysqli/PDO use the appropriate error method).
  • Check the result row count before looping (so you know whether zero rows or a fetch error).
  • Inspect the returned row keys (use functions such as array_keys or var_dump on one fetched row) so you know the exact column names to use when reading values.

Prefer a modern, secure approach instead of the old mysql_* extension: use prepared statements (mysqli or PDO) and store passwords with password_hash/password_verify. A concise PDO-style pattern (illustrative) is shown below.

$pdo = new PDO($dsn, $dbUser, $dbPass);
$stmt = $pdo->prepare('SELECT password_hash FROM students WHERE id_no = ?');
$stmt->execute([$submittedId]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row && password_verify($submittedPass, $row['password_hash'])) {
    // authenticated
}

For migration and secure-password guidance see the PHP docs on PDO (PDO manual) and on password handling (password_hash, password_verify).

Recommended Answers

All 8 Replies

Looks like you probably have an extra = after password

try this

$sql="SELECT * FROM $tbl_name WHERE id_no='$username' and password='$password'";

well i haven't really tried using mysql_fetch_assoc(), coz. i prefer mysql_fetch_array(). but from what i know about mysql_fetch_assoc() it returns associative arrays of your recordset. So your array index and values are dependent on your recordset. try print_r() to see the array values.

oh yeah... i think you got syntax error

$sql="SELECT * FROM $tbl_name WHERE id_no='$username' and password='$password='";

you put = after the password, it may be the result why you got no results coz. the query doesn't satisfy the condition.

seriously vaultdweller123, you are copying all of my posts and reposting them. stop.

what? copying you? u got the nerve to say that? why should i copy you what to i get from that? im here to help and learn. Your too full of yourself! if i would copy someone, i would select a more smarter person rather than you. i would prever copy ardav over to you. >:(

what? copying you? u got the nerve to say that? why should i copy you what to i get from that? im here to help and learn. Your too full of yourself! if i would copy someone, i would select a more smarter person rather than you. i would prever copy ardav over to you. >:(

wow. grow up. and stop copying my posts

You would probably check your sql statement and one more thing you should get the retrieved data with this way:

while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

instead of

while ($row = mysql_fetch_assoc($result)) {
    echo $row[0];
    echo $row[1];
    echo $row[2];
}

click here for more information: mysql_fetch_assoc()

wow. grow up. and stop copying my posts

sorry but i don't copy on dumb person... ur just a feeler... ahaha

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.