0

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>

Edited by Ezzaral: Added code tags. Please use them to format all code that you post.

4
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by vaultdweller123
0

Looks like you probably have an extra = after password

try this

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

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.

-2

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.

Edited by Ezzaral: Fixed broken code tag.

1

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

-3

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. >:(

0

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

0

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()

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.