colap Posted June 16, 2015 Share Posted June 16, 2015 $dbh = mysql_connection(); $sql = "select * from posts"; $stmt = $dbh->prepare($sql); $rowCount = $stmt->rowCount(); $result = $stmt->fetchAll(); formatted_value($result); $stmt->execute(); ?> It returns empty array, What can be the reason? Quote Link to comment https://forums.phpfreaks.com/topic/296842-php-pdo-mysql-returns-zero-result-why/ Share on other sites More sharing options...
Solution requinix Posted June 16, 2015 Solution Share Posted June 16, 2015 How do you expect to get any rows before you've execute()d the query? Quote Link to comment https://forums.phpfreaks.com/topic/296842-php-pdo-mysql-returns-zero-result-why/#findComment-1514020 Share on other sites More sharing options...
mac_gyver Posted June 16, 2015 Share Posted June 16, 2015 (edited) until you execute() the query, there's nothing for rowCount() or fetchAll() to use and are likely throwing php errors. also, why are you using a prepared query when the sql statement doesn't contain any input values? did you actually write this code or copy it from somewhere? i ask that because i reviewed two of your older threads that also use PDO statements and you previously got the prepare/execute/rowcount statements in the correct order for a SELECT query. programming requires that you actually learn what each statement does so that you can put them together in a meaningful way each time you use them. Edited June 16, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/296842-php-pdo-mysql-returns-zero-result-why/#findComment-1514021 Share on other sites More sharing options...
colap Posted June 16, 2015 Author Share Posted June 16, 2015 (edited) How do you expect to get any rows before you've execute()d the query? It is working now. <?php $dbh = mysql_connection(); $sql = "select * from posts"; $stmt = $dbh->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(); formatted_value($result); ?> Edited June 16, 2015 by php-coder Quote Link to comment https://forums.phpfreaks.com/topic/296842-php-pdo-mysql-returns-zero-result-why/#findComment-1514044 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.