computer






 

Question by  lupiggio (0)

What is the php code for testing if a nickname exists in a database?

 
+7

Answer by  rover (53)

Assuming your database has a nickname field, you can query the database using PDO as such: $dsn = 'mysql:dbname=mydb;host=127.0.0. 1'; /*using mysql, for example*/ $user = 'yourusername'; $password = 'yourpass'; $dbh = new PDO($dsn, $user, $password); $sql = "SELECT COUNT(*) FROM mytable WHERE nickname =? "; $stmt = $dbh->prepare($sql); $result = $stmt->execute(array('nicknametofind')); $result->fetch(); if ($result[0] > 0){ //the nickname exists }

 
+5

Answer by  RandomDan (489)

Not tested, but this should work. I'm simply querying the database for the username and running an if statement based on the number of rows returns. If it returns a row, then the the name exists. 0){ echo "Hello ". $username; }else{ echo "You are not a user"; }? >

 
+5

Answer by  Lakshmi65 (715)

use if check login() to find the existence of the username in PHP code. it checks whether the info matches the username, nickname or the password.

 
+5

Answer by  gigo (1706)

The code for the test can look like this: $query = sprintf("SELECT nickname FROM Users WHERE nickname = %s", $nickname); $result = mysql_query($query); if(! $result) { 'add user } else { 'user message: choose other nickname }

 
You have 50 words left!