desjardins2010 Posted March 7, 2014 Share Posted March 7, 2014 can somone point out why this returns 1 for the count when I'm confident there is nothing in the database?? $query = "SELECT * FROM `testing`"; foreach ($dbconnect->query($query) as $row) { echo "USERNAME: " . $row['username'] . "-" . "PASSWORD: " . $row['password'] . "<br/>"; } $query2 = $dbconnect->query("SELECT * FROM `testing` WHERE `email` = '$email'"); $count = count($query2); echo $count; if ($count !=0) { echo 'Mate Your Already In The Database'; }else { $dbconnect->exec("INSERT INTO `testing` (id,username,password,email) VALUES ('','$username','$password','$email')"); echo 'Successfully Entere Into Database'; } Quote Link to comment https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/ Share on other sites More sharing options...
gizmola Posted March 7, 2014 Share Posted March 7, 2014 When using PDO, you're getting a PDO Statement object back from the $dbconnect->query(). See http://us2.php.net/manual/en/class.pdostatement.php To get the count of rows you should use $count = $query2->rowCount(); Quote Link to comment https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/#findComment-1471809 Share on other sites More sharing options...
denno020 Posted March 7, 2014 Share Posted March 7, 2014 (edited) Firstly, I like the message to the user if they're already in the database, but please, update "Your" to be "You're".. Anyway, to your problem. If you var_dump $query2, you'll see that it is actually false, which means you're doing count(false). which gives 1. Therefore, your if condition should be as such if ($query2 !== false) { } //OR if (!$query2) { } Hope that helps. Denno Edited March 7, 2014 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/#findComment-1471810 Share on other sites More sharing options...
ginerjm Posted March 7, 2014 Share Posted March 7, 2014 Besides the other comments, your first 'query' is confusing. You have a query call inside a foreach - not sure that is a valid thing to do. Normally one calls the query and gets a result var and THEN uses the looping to process the records in the result var. Quote Link to comment https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/#findComment-1471812 Share on other sites More sharing options...
mac_gyver Posted March 7, 2014 Share Posted March 7, 2014 not sure that is a valid thing to do. it is. the PDOStatement object is traversable using foreach (as of php5.4 the mysqli result object is too.) Quote Link to comment https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/#findComment-1471814 Share on other sites More sharing options...
ginerjm Posted March 7, 2014 Share Posted March 7, 2014 That seems to be a very contrary syntax. The first time into the foreach the query is executed, and the following times it is not? Not very intuitive.... Quote Link to comment https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/#findComment-1471816 Share on other sites More sharing options...
.josh Posted March 7, 2014 Share Posted March 7, 2014 That seems to be a very contrary syntax. The first time into the foreach the query is executed, and the following times it is not? Not very intuitive.... Why is this not intuitive? If your mate handed you a grocery list and asked you to recite each item on the list, are you going to read the first line, hand the list back to him, then have him hand it back to you, then you read the 2nd line, then you hand it back to them, wash rinse and repeat for every line of the list, or are you just going to take the list once and keep it until you're done reading through the list? a foreach loop is not like other loops, where a condition is evaluated each iteration. Well, there is, but it's internal. Internally, php checks to see if there's another element for the pointer to move to, and stops when there isn't. But that's not the same as evaluating a condition written into the code to be evaluated on each iteration, like with other loops. Quote Link to comment https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/#findComment-1471817 Share on other sites More sharing options...
ginerjm Posted March 7, 2014 Share Posted March 7, 2014 Because when I see the call to a query function I expect it to be executed every time thru which is definitely not something I would want to do each time thru a loop. Quote Link to comment https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/#findComment-1471818 Share on other sites More sharing options...
.josh Posted March 7, 2014 Share Posted March 7, 2014 Because when I see the call to a query function I expect it to be executed every time thru which is definitely not something I would want to do each time thru a loop. well that's not how foreach loops work. They aren't like other loops. See my edit. Quote Link to comment https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/#findComment-1471819 Share on other sites More sharing options...
Augury Posted March 8, 2014 Share Posted March 8, 2014 (edited) The foreach should be a while statement. foreach of each of that. public function dbSelect($table, $fieldname=null, $id=null) { $this->conn(); $sql = "SELECT * FROM `$table` WHERE `$fieldname`=:id"; $stmt = $this->db->prepare($sql); $stmt->bindParam(':id', $id); $stmt->execute(); $row = $stmt->fetchAll(PDO::FETCH_ASSOC); return $row; } while ($sam = $db->dbSelect('testing') { if(isset($sam)): echo "then sam I am"; } Edited March 8, 2014 by Augury Quote Link to comment https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/#findComment-1471820 Share on other sites More sharing options...
.josh Posted March 8, 2014 Share Posted March 8, 2014 umm.. no? using a while statement like that will cause the query to be executed every time. Come on people.. understanding how loops work is php 101... Quote Link to comment https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/#findComment-1471834 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.