Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

anomalousc0d3r
anomalousc0d3r
1,432 Points

How to check if the user is already in the database.

Hello, I have this code that I will include below and I am not sure on how to create an if statement for it to check whether $steamprofile['steamid'] is already in the table called steam_users and a row called steamid and if it is, for it to return an echo saying "Already registered" and if not for it to insert the $steamprofile['steamid'] into a new row for steamid.

I have already looked for this on other threads, but everything that I have tried either gave me an error, or just kept inserting the steamid into the database each time I refreshed my page. I would appreciate some help. Thanks!

[syntax=php] $servername = "localhost"; $username = "root"; $password = "";

include (ROOT_PATH . 'steamauth/userInfo.php');

try {
    $conn = new PDO("mysql:host=$servername;dbname=database", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

$conn = null;[/syntax]

The above code is the base of the code, I am unsure how to set up the if statement. I have an idea of what it would look like, but it does not seem to work for me:

if(mysql_num_rows($sql)>=1) { echo"name already exists"; } else { echo"does not exist, creating entry"; // database insert code in here (unsure how to do this) }

I do not have any additional code, but my database is called codeforage and it has a table called steam_users. In the steam_users table, there is a column called user. I have a value of $steamprofile['steamid'] which I would like the 'if' statement to check for a duplicate for. I cannot figure out how to make it so that the 'if' statement checks for the same data that is in the database, and if the same data is there, then it would echo "name already exists".

2 Answers

Henrik Hansen
Henrik Hansen
23,176 Points

When using MySql you should consider setting the field to UNIQUE when creating the table:

$query = "CREATE TABLE Users (
uid int NOT NULL AUTO_INCREMENT,
name varchar(100),
steam_id varchar(100),
PRIMARY KEY (uid),
UNIQUE (steam_id)
)";

// To alter existing: 
$query = "ALTER TABLE Users ADD UNIQUE (steam_id)";

Finding values in the database:

$sql= "SELECT uid FROM Users WHERE steam_id = :other_steam_id LIMIT 1";
$statement = $conn->prepare($sql);
$statement->bindParam(':other_steam_id', $the_value_from_user, PDO::PARAM_STR);

$result = $stamtent->execute();

if ( !$result->fetch() ) {
// no result, steam_id is unique
}
anomalousc0d3r
anomalousc0d3r
1,432 Points

Can I just put unique key commands into the SQL query input dialog?

Henrik Hansen
Henrik Hansen
23,176 Points

I suppose that would work. It works in PhpMyAdmin at least. Otherwise just run the code once, and the table should be altered.

anomalousc0d3r
anomalousc0d3r
1,432 Points

Okay, awesome! Thanks a lot for your help!