Jump to content

simple user_exists function using PDO connect to mysql


junkomatic
Go to solution Solved by mac_gyver,

Recommended Posts

Hi, im still in the early learning stages, banging my head against walls looking for clues. Iv been reading the manual to no avail.

 

im building a user log in system based on the phpAcadamy tutorial 'Register & Login'. They use mysql_connect in the tutorial, but I am using a PDO connection to mysql.

 

i am making my first function, user_exists, which queries the username column of my table for names that match the POSTed $username, then returns a true or false if it is found.

 

here is my testing code:

<?php

	$host		= "localhost";
	$username	= "mholberg_skroovy";
	$password	= "*omitted*";
	$dbname		= "mholberg_skroovytest";


$db = new PDO("mysql:host={$host};dbname={$dbname};", $username, $password);


function user_exists($username) {
	$query = $db->query("SELECT `users`.`username` FROM `users` WHERE `username` = '$username'");
	return($query, 0) == 1) ? true : false;				//???
}



if (user_exists('junkomatic') === true) {
	echo 'exists';
}
die();
?>

The if statement at the bottom should test the function above it. The line with ??? is the line that is obviously wrong. Im getting an error on the ','

Any input/explanation would be hugely appreciated.      

Edited by junkomatic
Link to comment
Share on other sites

revised:

the function is supposed to look up the user_id value, which is a column in my table that auto increments.

so the query line on the function is actually

$query = $db->query("SELECT `users`.`user_id` FROM `users` WHERE `username` = '$username'");

I still need to know how to get it to return true or false.

there error im getting is Fatal error: Call to a member function query() on a non-object on line 13

Edited by junkomatic
Link to comment
Share on other sites

the function has its own local program scope and the main program's $db variable doesn't exist inside of the function.

 

you have two choices -

 

1) pass the instance of the database class in $db into the function as a call time parameter, i.e. if (user_exists($db, 'junkomatic') === true) {

 

2) create a class and use dependency injection to get the instance of the database class in $db into the instance of your class.

Link to comment
Share on other sites

 

1) pass the instance of the database class in $db into the function as a call time parameter, i.e. if (user_exists($db, 'junkomatic') === true) {

 

 

ok, heres what i tried, but with this i just get a blank page instead of the echo 'exist', and no errors.  the username does exist though, its in the username column of the users table of that db. it has a user_id of 1.

 

<?php
	$host		= "localhost";
	$username	= "mholberg_skroovy";
	$password	= "omitted";
	$dbname		= "mholberg_skroovytest";


$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

function user_exists(PDO $db, $username) {
	$stmt = $db->prepare('SELECT COUNT(1) FROM `users` WHERE `username` = ?');
	$stmt->bindParam(1, $username);
	return (bool) $stmt->fetchColumn();
}

if (user_exists($db, 'junkomatic')) {
	echo 'exists';
}
?>

 

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.