zippy21
asked on
MySQL problems with mysql_result() & mysql_num_rows()
When I try using either one of those functions I get this type of error: supplied argument is not a valid MySQL result resource
mysql_result(): supplied argument is not a valid MySQL result resource
I get the same thing for mysql_num_rows()
Any ideas?
mysql_result(): supplied argument is not a valid MySQL result resource
I get the same thing for mysql_num_rows()
Any ideas?
ASKER
This is with the mysql_num_rows method:
?
session_start();
if ($userid && $password)
{
// if the user has just tried to log in
$db_conn = mysql_connect("localhost")
mysql_select_db("dbauthori zation", $db_conn);
$query = "select * from tblvalidation "
."where name='$userid' "
." and pass=password('$password') ";
$result = mysql_query($query, $db_conn);
if (mysql_num_rows($result) >0 )
{
// if they are in the database register the user id
$valid_user = $userid;
session_register("valid_us er");
}
}
?>
This is with the count method:
<?
session_start();
if (isset($userid) && ($password))
{
// if the user has just tried to log in
$db_conn = mysql_connect("localhost") ;
mysql_select_db("dbauthori zation", $db_conn);
$query = "select Count(*) from tblvalidation"
."where username='$userid' "
." and pass=password('$password') ";
$result = mysql_query($query, $db_conn);
$count=mysql_result($resul t,0,"count (*)");
if ($count > 0)
{
// if they are in the database register the user id
$valid_user = $userid;
session_register("valid_us er");
}
}
?>
?
session_start();
if ($userid && $password)
{
// if the user has just tried to log in
$db_conn = mysql_connect("localhost")
mysql_select_db("dbauthori
$query = "select * from tblvalidation "
."where name='$userid' "
." and pass=password('$password')
$result = mysql_query($query, $db_conn);
if (mysql_num_rows($result) >0 )
{
// if they are in the database register the user id
$valid_user = $userid;
session_register("valid_us
}
}
?>
This is with the count method:
<?
session_start();
if (isset($userid) && ($password))
{
// if the user has just tried to log in
$db_conn = mysql_connect("localhost")
mysql_select_db("dbauthori
$query = "select Count(*) from tblvalidation"
."where username='$userid' "
." and pass=password('$password')
$result = mysql_query($query, $db_conn);
$count=mysql_result($resul
if ($count > 0)
{
// if they are in the database register the user id
$valid_user = $userid;
session_register("valid_us
}
}
?>
The error you are getting indicates that your query FAILED.
Try this...
$result = mysql_query($query, $db_conn)
or die(mysql_error());
Try this...
$result = mysql_query($query, $db_conn)
or die(mysql_error());
Is "password()" a MySQL function?
> Is "password()" a MySQL function?
Yes, it returns a hash for the string it gets as an argument. Passwords in user-table of mysql-database are stored as hashes, not plain text, to increase security.
SELECT password("Foo")
returns
72ec97820a2efefb
When user tries to log in, hashes are compared, not passwords themselves. Eg.
SELECT password("Foo") AND '72ec97820a2efefb'
returns 1 (true).
Yes, it returns a hash for the string it gets as an argument. Passwords in user-table of mysql-database are stored as hashes, not plain text, to increase security.
SELECT password("Foo")
returns
72ec97820a2efefb
When user tries to log in, hashes are compared, not passwords themselves. Eg.
SELECT password("Foo") AND '72ec97820a2efefb'
returns 1 (true).
> SELECT password("Foo") AND '72ec97820a2efefb
Sorry, this should of course be:
SELECT password("Foo") = '72ec97820a2efefb
Otherwise it would return true always as long as one (or both) of the strings are '0'
Sorry, this should of course be:
SELECT password("Foo") = '72ec97820a2efefb
Otherwise it would return true always as long as one (or both) of the strings are '0'
The function mysql_result() returns the contents of one cell from a MySQL result set. It cannot be used with multiple cells.
so you should use as following:
$query = "select Count(*) as user_count from tblvalidation"."where username='$userid' "." and pass='$password'";
$result = mysql_query($query, $db_conn);
$count=mysql_result($resul t,0,"user_ count");
if ($count > 0)
{
// if they are in the database register the user id
$valid_user = $userid;
session_register("valid_us er");
}
I hope this will make you satisfy.
best regards
so you should use as following:
$query = "select Count(*) as user_count from tblvalidation"."where username='$userid' "." and pass='$password'";
$result = mysql_query($query, $db_conn);
$count=mysql_result($resul
if ($count > 0)
{
// if they are in the database register the user id
$valid_user = $userid;
session_register("valid_us
}
I hope this will make you satisfy.
best regards
ASKER
does not work, I still get the same error:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\chapter 20\passpro tect.php on line 13
is something not set up right on my machine? I am running iis 5.0 and win2000....
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\chapter
is something not set up right on my machine? I am running iis 5.0 and win2000....
Does the query fail? Have you added the ...
or die("Query failed")
?
or die("Query failed")
?
Your SQL-query propably has an error. Try this one:
$qresult = mysql_query($query) or print(mysql_error());
$qresult = mysql_query($query) or print(mysql_error());
Theril, see the the first reply AND Hamlet's reply.
Same thing.
Same thing.
If your sql is okay the problme may be even further back...
Try checking for errors on you connect and select db...
$db_conn = mysql_connect("localhost")
or die('connect failed');
mysql_select_db("dbauthori zation", $db_conn)
or die('select db failed');
I also noticed that there was NO SEMICOLON at the end of...
$db_conn = mysql_connect("localhost")
Did you copy and paste the code directly? The reason I ask is that I don't see any reason the sql would fail, unless either the table name or one of the field names is wrong. Please double check these.
Try checking for errors on you connect and select db...
$db_conn = mysql_connect("localhost")
or die('connect failed');
mysql_select_db("dbauthori
or die('select db failed');
I also noticed that there was NO SEMICOLON at the end of...
$db_conn = mysql_connect("localhost")
Did you copy and paste the code directly? The reason I ask is that I don't see any reason the sql would fail, unless either the table name or one of the field names is wrong. Please double check these.
ASKER
the query works fine...do you think it could be how I defined the pass field in my database? it is just pass varchar(255)
I know there is data in there because I wrote a php page to view the data in the database.
I know there is data in there because I wrote a php page to view the data in the database.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Normally, you would do something like this ...
<?php
/* Connecting, selecting database */
$link = mysql_connect("mysql_host"
or die("Could not connect");
print "Connected successfully";
mysql_select_db("my_databa
/* Performing SQL query */
$query = "SELECT * FROM my_table";
$result = mysql_query($query) or die("Query failed");
/* Printing results in HTML */
print "<table>\n";
while ($line = mysql_fetch_array($result,
print "\t<tr>\n";
foreach ($line as $col_value) {
print "\t\t<td>$col_value</td>\n
}
print "\t</tr>\n";
}
print "</table>\n";
/* Free resultset */
mysql_free_result($result)
/* Closing connection */
mysql_close($link);
?>
(Straight from the PHP manual).
Richard.