Jump to content

Recommended Posts

I am new and just learning PDO and already having some problems with very simple stuff :(

 

<?php

$hostname = "localhost";

$dbname = "PDO";

$username = "root";

$password = "";

 

try

{

$dbh = new PDO("mysql:$hostname;dname=$dbname",$username,$password);

$sql = "SELECT * FROM users";

foreach($dbh->query($sql) as $row)

{

echo $row['id'] . $row['name'] . $row['email'];

}

echo "Number of effected rows:" . $count;

}

catch(PDOExecption $e)

{

echo $e->getMessage();

}

?>

 

Can you tell me what in that code is making me get the error:

 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\xampp\PDO\select_query.php

 

and what I would need to alter to fix the problem.

Link to comment
https://forums.phpfreaks.com/topic/261727-pdo-help/
Share on other sites

You can use the manual for problems like this.

 

Foreach expects an array, you're supplying it with the return value from PDO->query(); According to the manual...

http://php.net/manual/en/pdo.query.php

... PDO->query will either return FALSE, or a PDOStatement object, neither of which are arrays.

 

PDOStatement->fetch or PDOStatement->fetchAll might help you out :)

Link to comment
https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341227
Share on other sites

You can use the manual for problems like this.

 

Foreach expects an array, you're supplying it with the return value from PDO->query(); According to the manual...

http://php.net/manual/en/pdo.query.php

... PDO->query will either return FALSE, or a PDOStatement object, neither of which are arrays.

 

PDOStatement->fetch or PDOStatement->fetchAll might help you out :)

 

I looked at that example and they do the same thing? I used this guide http://phpro.org/tutorials/Introduction-to-PHP-PDO.html to learn and I don't see any difference in that one either?

Link to comment
https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341231
Share on other sites

I looked at that example and they do the same thing? I used this guide http://phpro.org/tutorials/Introduction-to-PHP-PDO.html to learn and I don't see any difference in that one either?

 

Did you create the database and table being used in the query?  I'd guess your query is failing and ->query is returning FALSE rather than the statement object.

 

Link to comment
https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341236
Share on other sites

I looked at that example and they do the same thing? I used this guide http://phpro.org/tutorials/Introduction-to-PHP-PDO.html to learn and I don't see any difference in that one either?

 

Did you create the database and table being used in the query?  I'd guess your query is failing and ->query is returning FALSE rather than the statement object.

 

That is what I originally thought but then I tipple checked everything and the query shouldn't fail. Since I am just selecting all from the table.

Link to comment
https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341237
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.