Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to know the record was found? 1

Status
Not open for further replies.

technisup

Programmer
Sep 28, 2007
41
US
Hi, I'm new with php and I'd like to know if a record was found or not?

This is my code:
Code:
<?php
  
 if($_POST['status']=='ok'){
     //Valida la conexión a la base de datos
 	 include("Conn.php");
 	 $link=Conectarse();
 	 $result=mysql_query("select * from admin where user=".$_POST['user']." and pwd=".$_POST['pwd'],$link); 
	 echo('el valor es '.$result);
 }
?>
 
Code:
if($_POST['status']=='ok'){
     //Valida la conexión a la base de datos
      include("Conn.php");
      $link=Conectarse();
      $result=mysql_query("select * from admin where user=".$_POST['user']." and pwd=".$_POST['pwd'],$link);
      $row = mysql_fetch_assoc($result);
      if (count($row) > 0) {
            record exists
      } else {
            doesnt exist
      }


-Geeeeeeeeeeeeeeeeeeeeeeee-
 
Just for completeness and because I was lazy earlier, the better way to do it is...

if (mysql_num_rows($result) != 1)

or

if (mysql_num_rows($res) > 0)

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
Geee (Programmer)
15 Oct 07 9:04
Just for completeness and because I was lazy earlier, the better way to do it is...

if (mysql_num_rows($result) != 1)

Well, what if he has 2 rows?
Code:
if (mysql_num_rows($result) != 1)

Then this will return true?

I know he said
Hi, I'm new with php and I'd like to know if a record was found or not?

But I dont know if he meant 1 record, when saying "a record".

The other one, > 0 or =>1 though, is better IMO

Olav Alexander Mjelde
Admin & Webmaster
 
I presumed he only wanted to check for one row because his POST variables are "user" and "pwd".

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
with a user-pwd combo you would as gee... points out, expect a unique constraint. so the method expressed is not too inefficient.

but from previous conversations on this board and on the mysql forum, i'd recommend instead that you use count.

Code:
function validLogon($username, $pwd){
$sql = "select count(*) from table where username = '".mysql_escape_string(trim($username))."' and pwd = '".mysql_escape_string(trim($pwd))."'";
$result = intval(mysql_result(mysql_query($sql), 0,0));
return ($result === 1);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top