By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

How to check if a record exists in a MySQL database

P: 6
hello

i have some problem..
actually i make a userscreen using php and problem is that i have check the username is already exists aur not...
i used the query
like this...
Expand|Select|Wrap|Line Numbers
  1. $con = mysql_connect("localhost","root");
  2. if(!$con)
  3. {
  4.     die('Could not connect:'.mysql_error());
  5. }
  6.     mysql_select_db("demo",$con);
  7.     $obj1=mysql_query("select count(u_Username) as test from user where u_Username='$_POST[username]'");
  8.     echo ($obj1);
  9.     $sql="Insert into user(u_id, u_Fname, U_Lname, u_Email, u_Username, u_password, u_Cpassword) VALUES
  10.     (NULL, '$_POST[fname]', '$_POST[lname]', '$_POST[email]', '$_POST[username]', '$_POST[password]', '$_POST[repassword]');";
  11.     $objRecordSet = mysql_query($sql);
  12.     $errorNo       = mysql_errno();
  13.     if($errorNo == 0)
  14.     {
  15. ?>
  16.     <script type="text/javascript">
  17.         alert("Record Saved");
  18.     </script>
  19. <?php
  20.     }
  21. mysql_close($con);
  22.  
this is my code...
i want the no of records in the $obj1;;;
but this coding return me the Resource id #3

so anyone can solve my problem....
actually i am totally new in php....
so plz help...
thanks
Sep 12 '07 #1
Share this Question
Share on Google+
5 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Please enclose any code within the appropriate code tags. See Posting Guidelines .

Actually variable $obj1 is not the result of the query but the resource handle for the result. You must fetch your result data using that handle. The number of rows can be extracted in variable $no with
[php]$no = mysql_num_rows($obj1);[/php]
Ronald
Sep 12 '07 #2

Atli
Expert 5K+
P: 5,058
Hi.

I've edited the thread title to make it a little bit more clear.
Sep 12 '07 #3

P: 36
$obj1 is a mysql resource id. Please use mysql_fetch_assoc or mysql_fetch_object. Example below
[php]

$row = mysql_fetch_assoc($obj1);
[/php]

However for what you need just run
[php]
if(mysql_num_rows($obj1) > 0){
//name exists
}else{
//Name doesn't exist
}
[/php]
Sep 13 '07 #4

P: 6
thanks ronald.....

to solve my problem....

i solve this problem with ur help.

today i have posted new problem please solve this one also....


Please enclose any code within the appropriate code tags. See Posting Guidelines .

Actually variable $obj1 is not the result of the query but the resource handle for the result. You must fetch your result data using that handle. The number of rows can be extracted in variable $no with
[php]$no = mysql_num_rows($obj1);[/php]
Ronald
Sep 13 '07 #5

P: 6
thanks Weisbartb


to solve my problem...
thanks once again...

$obj1 is a mysql resource id. Please use mysql_fetch_assoc or mysql_fetch_object. Example below
[php]

$row = mysql_fetch_assoc($obj1);
[/php]

However for what you need just run
[php]
if(mysql_num_rows($obj1) > 0){
//name exists
}else{
//Name doesn't exist
}
[/php]
Sep 13 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.