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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.