Page 1 of 1

[CLOSED] How to check if user already exists

Posted: Thu Jan 29, 2015 11:28 am
by CryptAlchemy
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!

Code: Select all

    $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;

Re: How to check if user already exists, if not, then insert

Posted: Thu Jan 29, 2015 11:34 am
by Celauran
Did you maybe post the wrong code, or only part of it? The code you posted only sets up the database connection and has nothing to do with what you're trying to achieve.

Re: How to check if user already exists, if not, then insert

Posted: Thu Jan 29, 2015 11:38 am
by CryptAlchemy
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)
}

Re: How to check if user already exists, if not, then insert

Posted: Thu Jan 29, 2015 11:41 am
by Celauran
No. You're using PDO as per the snippet of code in your first post. mysql_ anything is not going to work. Do you have any additional code already in place? I don't know anything about your DB and really don't have anything to work off.

Re: How to check if user already exists, if not, then insert

Posted: Thu Jan 29, 2015 11:47 am
by CryptAlchemy
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".

Re: How to check if user already exists, if not, then insert

Posted: Thu Jan 29, 2015 12:06 pm
by Celauran
CryptAlchemy wrote:my database is called codeforage

Code: Select all

$conn = new PDO("mysql:host=$servername;dbname=database", $username, $password);
Once you've got that corrected, something like this should work.

Code: Select all

$query = "SELECT id FROM steam_users WHERE user = :user";
$stmt = $conn->prepare($query);
$result = $stmt->execute([':user' => $steamprofile['steamid']]);

if ($result) {
	$rows = $stmt->fetchAll();
	if (!empty($rows)) {
		echo 'User exists';
	}
}

Re: How to check if user already exists, if not, then insert

Posted: Thu Jan 29, 2015 12:10 pm
by CryptAlchemy
Yes,
$conn = new PDO("mysql:host=$servername;dbname=codeforage", $username, $password);
this needs the database.
I have replaced this, but I am unsure of how to make the if command.