Click to See Complete Forum and Search --> : Need help in PHP


Aero2004
January 29th, 2005, 07:58 PM
Hiii
I got a problem in my php application, the problem is the same code can run in one server (lycos.co.uk) but cannot run in another server using PHP 4.3.9 and MySQL 4.0.22-standard...
Below is the code:
1 <?
2 $user = $_POST['user'];
3 $password = $_POST['password'];
4
5 include "db.inc.php";

6$query = "select * from pwdtable
7 where user = '$user'";
8
9$result = mysql_query($query);
10
11while ($row = mysql_fetch_row($result)) {
12 if ($password == $row[1]) {
13 session_start();
14 $user_session = $password;
15 session_register("user_session");
16 header("location:main.php");
17 }
18}
19
20echo("Login fails");
21?>
and the error tat i got is
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/eugene2/public_html/ben/checkid.php on line 12

Thank you in advance... Really frustrating becoz i tink my code is correct...
Really appreciate if anyone can kindly tell me wats wrong...

khp
January 29th, 2005, 08:18 PM
Your problem is almost cirtainly that your are failing to connect to the database.

You could have caught this error much much earlier in your script if you would bother to put up some integrity checks.

IE. instead of $result = mysql_query($query); it's better to write $result = mysql_query($query) or die("MYSQL query: $query failed:" . mysql_error());

I assume you are connecting to the database in the file db.inc.php.
Which probably contains a line like
mysql_connect($databaseserver, $databaseuser, $databasepassword);
This could likewise be improved by changeing it to something like
mysql_connect($databaseserver, $databaseuser, $databasepassword) or die("Could not connect to $databaseserver, Error: " . mysql_error());

Which would most likely tell you that you don't have permission to access the database or that no database is running on the specified server.

Aero2004
January 30th, 2005, 06:57 AM
Yooozzz
Thanks alot.... :)))))

Aero2004
January 30th, 2005, 07:40 PM
Sorry... I still have another question...
Below is my code
//checksession.php
<?
session_start();
if(!session_is_registered("user_session") ) {
echo("Access denied<br>Please login");
exit;
}
?>

and i register session using
while ($row = mysql_fetch_row($result)) {
if ($password == $row[1]) {
session_start();
$user_session = $password;
session_register("user_session");
header("location:main.php");
}
}

why this code cannot run under certain computer, although I have given the correct password and username, however, error message of "Access denied" resulted from checksession.php always appears...

Thank again...