junkomatic Posted April 29, 2013 Share Posted April 29, 2013 (edited) 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 April 29, 2013 by junkomatic Quote Link to comment Share on other sites More sharing options...
junkomatic Posted April 29, 2013 Author Share Posted April 29, 2013 (edited) 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 April 29, 2013 by junkomatic Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 29, 2013 Share Posted April 29, 2013 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. Quote Link to comment Share on other sites More sharing options...
junkomatic Posted April 29, 2013 Author Share Posted April 29, 2013 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'; } ?> Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted April 29, 2013 Solution Share Posted April 29, 2013 you need to execute the prepared query before you can fetch anything from the result set. Quote Link to comment Share on other sites More sharing options...
junkomatic Posted April 29, 2013 Author Share Posted April 29, 2013 you need to execute the prepared query before you can fetch anything from the result set. i added $stmt->execute(); between lines 13 and 14 and i got my "exists" result! thanks a bunch, now im further along the path of understanding. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.