Jump to content

[SOLVED] Available Name


Ken2k7

Recommended Posts

Say I have created a database table named member with columns - id, name, age, sex - and say someone registers and I want to see if that name already exists.

 

I assume I can do this:

 

<?php

$name = "Ken2k7"
$sql = "SELECT name FROM member WHERE name='$name'";

?>

 

I think that would work, but if the name doesn't exist what value would $sql have? What value would $sql have if the name does exist?

Link to comment
https://forums.phpfreaks.com/topic/66301-solved-available-name/
Share on other sites

$query = mysql_query("SELECT * FROM `member` WHERE `name` = '$name' LIMIT 1");
if(mysql_num_rows($query) == 0) {
    //No name exists, they can register!
}else {
    echo("Username already exists, please pick another.");
}

 

If you are trying to retrieve the values of your query:

 

$query = mysql_query("SELECT * FROM `member` WHERE `name` = '$name' LIMIT 1");
$result = mysql_fetch_array($query);
echo($result["name"]); // would echo the name that is in the database
echo($result["age"]); //age

 

If there are no results, then php will simply display nothing.

Link to comment
https://forums.phpfreaks.com/topic/66301-solved-available-name/#findComment-331670
Share on other sites

XaeroDegreaz, I don't think that was what he was after.. he wanted to know.. will it implement $name is the variable $name doesn't exist?

 

Try it.. it doesn't have to be a $sql

 

$name = "matthew";
$sentence = "my name is $name";
echo $sentence;

// Outputs : my name is matthew

//$name = "matthew";
$sentence = "my name is $name";
echo $sentence;

// Outputs : ??

 

I always regard it bad programming to do that though, and prefer to explicitly concatenate my strings:

 

$name = "matthew";
$age = 16;
$sentence = "My name is ".$name." and I am ".$age." years old.";
echo $sentence;

Link to comment
https://forums.phpfreaks.com/topic/66301-solved-available-name/#findComment-331671
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.