hi for all the expert here, i am now facing a minor problem on retrieving data here. MySQL do contain all the variables defined with data inside it. However, i juz need to know how to loop through the array to get all the data in a row?
Below is the php code i used:

<?php
mysql_connect("localhost", "root", "1234") or die(mysql_error());
mysql_select_db("userlist") or die(mysql_error());
//session_start();
$query = "SELECT * FROM user"; 
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
if($_POST['name'] == $row['username'])
{
	echo "Thanks for logging in.";
	header("http://www.google.com");
}
else
{
	echo "Sorry, your username and password does not match.";
	//header("http://www.yahoo.com");
}
$_SESSION['name']=$_POST['name'];
$_SESSION['password']=$_POST['password'];
$_SESSION['status']=$_POST['status'];
?>

especially at the function, i dunnoe why i can see the Thanks for login even no username entered. Can anyone help? By the way, php redirect cannot use more than once? Thanks for help....

Recommended Answers

All 11 Replies

Instead of $row = mysql_fetch_array($result) use, while($row = mysql_fetch_array($result)) {
AND

 echo "Thanks for logging in.";
 header("http://www.google.com");

will give you an error because you can't output anything before header function. Either echo a message or redirect the user. :)

thanks for the solution, however i still facing the problem using while loop...
it keeps showing this on the page:
Sorry! Wrong name!!Sorry! Wrong name!!Sorry! Wrong name!!
when i use while loop..
below is the modified code:

<?php
mysql_connect("localhost", "root", "1234") or die(mysql_error());
mysql_select_db("userlist") or die(mysql_error());
$query = "SELECT * FROM user"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
if ($_POST['name']==$row['username'] && $_POST['password']==$row['password'])
	{
	echo "Login successfully!!!";
}
else
{
	echo "Sorry! Wrong name!!";
}
}
$name=$_POST['name'];
$password=$_POST['password'];
$status=$_POST['status'];
?>

and it only manage to get the 1st row of the data...any silly mistakes i'd done?

When the user types username and password, why not check if that exists in the table, rather than get all records and then check ?

$name=$_POST['username'];
$password=$_POST['password'];
Ie., $query = "select * from user where username='$name' and password='$password'";

Then check if this returns more than 1 row. If it does, then the username and password is valid. Else, invalid username and pass.

well, using simple script, i can get all the data within the table, the table now consists of 3 rows of data, but i dunnoe why there exist three times of echo ...and the variable do contain data input by the user...

It displays thrice because its looping three times. You can do it this way.

<?php
mysql_connect("localhost", "root", "1234") or die(mysql_error());
mysql_select_db("userlist") or die(mysql_error());
$name=$_POST['name'];
$password=$_POST['password'];
$query = "SELECT * FROM user where username='$name' && password='$password'"; 
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) > 0){
	echo "Success!";
}
else
{
	echo "Sorry! Wrong name!!";
}
?>

thanks man....it solved...
reputation given...

:) cool !

by the way...got any php script tht can redirect the viewer after display a message like:

Thank You....You'd logged in sucessfully...

After 5 seconds redirect the page..possible?

yeah!!! useful links..thanks..
however it is in javascript....php will be preferable...thanks...:)

you can do the same thing by putting echo :)

echo "success!";
echo "<meta http-equiv=\"refresh\" content=\"2;url=redirectpage.php\">";

This will redirect the script to redirectpage.php after 2 seconds.

More about meta refresh/redirect here.

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.