Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Php + Mysql Question!, While inserting data into MySQL, how can I know if the data I'm in
GaiaZone
post Jan 12 2008, 12:44 AM
Post #1


Newbie [Level 2]
**

Group: Members
Posts: 25
Joined: 8-January 08
From: PR
Member No.: 55,961



Basically, I want to know if the Data I'm inserting through a Form is already there or not. Sort of a Username registration page.

I have this, but it doesn't appear to work...

CODE
$result = mysql_query("SELECT * FROM users WHERE username='$username'");
if($result == 1)
    {
    echo '<h1>ERROR!</h1>The username you have chosen already exists!';
    }
Go to the top of the page
 
+Quote Post
sonesay
post Jan 12 2008, 12:55 AM
Post #2


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 642
Joined: 20-June 07
From: Auckland
Member No.: 45,102



$result will equal a resource object I beleive so comparing it to 1 will always result in false.

You can use
CODE


$count = mysql_num_rows($result);



Then compare $count == 1 in your if statement.

This post has been edited by sonesay: Jan 12 2008, 01:12 AM
Go to the top of the page
 
+Quote Post
GaiaZone
post Jan 12 2008, 02:13 AM
Post #3


Newbie [Level 2]
**

Group: Members
Posts: 25
Joined: 8-January 08
From: PR
Member No.: 55,961



It works perfectly.

Thank you. =]
Go to the top of the page
 
+Quote Post
slu
post Jan 12 2008, 11:12 AM
Post #4


Newbie [Level 2]
**

Group: Members
Posts: 27
Joined: 22-June 05
Member No.: 8,557



...or let MySQL do the counting:

CODE

$result = mysql_query("SELECT count(*) as count FROM users WHERE username='$username'");
$count = mysql_result($result, 0, "count");


and I would change the compare to $count > 0 in the if statement, just to be sure.
Go to the top of the page
 
+Quote Post
galexcd
post Jan 13 2008, 03:55 AM
Post #5


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
*********

Group: [HOSTED]
Posts: 974
Joined: 25-September 05
From: The dungeon deep below the foundation of trap17
Member No.: 12,251



QUOTE(slu @ Jan 12 2008, 03:12 AM) *
...or let MySQL do the counting:

CODE

$result = mysql_query("SELECT count(*) as count FROM users WHERE username='$username'");
$count = mysql_result($result, 0, "count");


and I would change the compare to $count > 0 in the if statement, just to be sure.


That would result in an error if there were no rows matching that query. mysql_num_rows is the best function to use.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Problem On Mysql "order By"(5)
  2. Increment A Mysql Column(6)
  3. Can You Add Images Into A Mysql Database?(20)
  4. Creating Profiles In Php/mysql ?(7)
  5. Php Search Engine Script For Mysql Database(11)
  6. How Good Is This Data Cleaning Function?(2)
  7. Mysql Query(2)
  8. What Does This Do?(4)
  9. Simple Mysql Query Limiting(9)
  10. Some Mysql Basics(4)
  11. Php, Mysql, Apache(4)
  12. Php And Mysql Applications(3)
  13. Getting Php 5 To Work With Mysql(0)
  14. Problem With A Mysql Join(2)
  15. Using Multiple Selection Array In Table To Order Data(1)
  1. The Artists Tutorials :mysql Basic Commands(0)
  2. Php Email Validation(1)
  3. [mysql]get Id Of Loged In User?(7)
  4. [mysql/php]need Som Basic Help(13)
  5. [php/mysql]id Trouble [resolved](3)
  6. Mysql Won't Update(4)
  7. Tools Needed!(9)
  8. Best Sites For Learning Php-mysql(4)
  9. Php And Mysql Programming(2)
  10. Best Php And Mysql Editor For Noobs(6)
  11. Html Form!(4)
  12. Mysql Error(3)
  13. Create Table - Mysql Code - Help(1)


 



- Lo-Fi Version Time is now: 24th July 2008 - 12:05 PM