Jump to content

trying to understand PDO..


desjardins2010

Recommended Posts

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';
}

Link to comment
https://forums.phpfreaks.com/topic/286804-trying-to-understand-pdo/
Share on other sites

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

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.

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.

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

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.