PDO

Hello I’m trying to make a registration system but I’m still a newbie with php. I would like to know if this is correct :

I did put $errormsg=$nbusers; (in red) because I have the database already but with nothing in it. So I assume if this code would be working I would have an error message telling me that there is 0 users with that username. Of course when this will be working I’ll replace this message with code adding the member to the database. But I’d like this to be working in the first place. I put the relevant code in yellow and the code I think is the issue in red. Also I’m not sure how to close a PDO connection: mysql_close(); ? thanks for the help.

[php] <?php

if ($_POST[‘registerbtn’]) {

$getuser=$_POST['username'];
$getpass=$_POST['password'];
$getretypepass=$_POST['retypepassword'];
$getemail=$_POST['email'];

if ($getuser) {
	if ($getemail)  {
		if ($getpass)  {
			if ($getretypepass)  {
				if ($getpass === $getretypepass) {
					if (strlen($getemail) && strstr($getemail, "@") && strstr($getemail, ".")) {
						require("connectmysql.php");
						
						$reponse = $bdd->query("SELECT * FROM users WHERE username='$getuser'");
					[color=red]	$nbusers = $response ->rowCount();[/color]
							if ( $nbusers == 0) {
						[color=red]	$errormsg=$nbusers;[/color]
							
							}
							else
								$errormsg="This username is already taken";
							
		[color=red]				mysql_close();[/color]
					
					
					}
					else
						$errormsg="You must enter a valid email address.";
					
					
				}
				else
					$errormsg="Your passwords did not match.";
	
	
			}
			else
				$errormsg= " You must retype your password.";			
	
		}
		else
			$errormsg= " You must enter your password.";		
	
	}
	else
		$errormsg= " You must enter your email.";

}
else
	$errormsg= " You must enter your username.";

}

$form = "

$errormsg
Username :
Password :
Retype your password :
Email :
";

echo $form;
?>[/php]

connectionmysql.php =

[php]<?php
try
{
$bdd = new PDO(‘mysql:host=localhost;dbname=test’, ‘root’, ‘’);
}
catch (Exception $e)
{
die('Erreur : ’ . $e->getMessage());
}
?>[/php]

Ho i just noticed that colors don’t apply to php code on this forum.

So the part that I guess it the problem is this one :

$reponse = $bdd->query(“SELECT * FROM users WHERE username=’$getuser’”);
$nbusers = $response ->rowCount();
if ( $nbusers == 0) {
$errormsg=$nbusers;

							}
							else
								$errormsg="This username is already taken";
							
						mysql_close();

I made a topic already but I changed my code totally so I’m making a new one.

[php] $response = $bdd->query(“SELECT id FROM users WHERE username=’$getuser’”);
if ($response->fetchColumn())
{
$errormsg=“You cannot use this username.”;
}[/php]

I’ve the error that fetchColumn has been applied on a non-object.

Sponsor our Newsletter | Privacy Policy | Terms of Service