Re: php and MySQL



comp_guy wrote:
hey guys, i have been working on a simple form which validates if a
user is valid or not. i am a newbie and just want to deny unauthorised
access to a 'members' page. I wish to compare the password entered by
the user with that they entered into their submitted registration
form.. however i keep getting a mySQL error message 'query was empty'.
i was hope someone would know my failings! here is my code:

<?php

$connection = mysql_connect("sentinel.cs.cf.ac.uk","scm5sjc","my
password here");

$password=$_POST['password'];

mysql_select_db("sjcdb",$connection) or die("failed!");

$sql = mysql_query("SELECT * FROM users WHERE password = '$password'");

$result = mysql_query($sql)or die(mysql_error());

$rows = mysql_num_rows($result);

if ($rows){

if ($password == $row[9]){

header("Location:members.html");
}
else
{
header("Location:register.html");
exit;
}
}
mysql_close();

?>


A couple of observations...

This:
$sql = mysql_query("SELECT * FROM users WHERE password = '$password'");

sets $sql to be the result set of the query...
while this:
$result = mysql_query($sql)or die(mysql_error());

tries to do another query using the result set. That's just not right.

I suggest you do something like:
$sql = "select count(*) from users where password = '$password'";
$result = mysql_query($sql, $connection);

$row = mysql_fetch_row($result);
if( $row[0] ) {
...

mysql_free_result($result);
mysql_close($connection);

Also, your second comparison to $row[9] is not needed. The password match is already accounted for in the where clause of the SQL query.

-david-

.



Relevant Pages

  • Re: Query to compare subtables
    ... To perform this, you could query for everything in Set A. Then, through ... Does not contain each element of the compare set. ... Suppose that another set had only members A1 and A3. ... > I have a master table and a details table. ...
    (microsoft.public.access.queries)
  • Re: php and MySQL
    ... i am a newbie and just want to deny unauthorised ... access to a 'members' page. ... I wish to compare the password entered by ... however i keep getting a mySQL error message 'query was empty'. ...
    (comp.lang.php)
  • Re: Compare Records & Move to new table in VBA
    ... If i write an sql query, can the field be a variable pulled from somewhere ... So i need to know how to compare in VB, because the criteria in a query is ... If it's called tblClient, Access will alias the 2nd one as tblClient_1. ...
    (microsoft.public.access.modulesdaovba)
  • Re: Union query?
    ... I used that information to build the union query I proposed. ... FROM [qry Normal Lines Compare] as C LEFT JOIN ... Leslie Isaacs wrote: ... Compare] with any matching record data from [qry changed basics]. ...
    (microsoft.public.access.queries)
  • Re: Union query?
    ... Leslie Isaacs wrote: ... I will try your proposed query when I'm back in the office, and will post back here to let you know. ... FROM [qry Normal Lines Compare] as C LEFT JOIN ... The first section query returns all records from with any matching record data from [qry changed basics]. ...
    (microsoft.public.access.queries)