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? Link to comment https://forums.phpfreaks.com/topic/296842-php-pdo-mysql-returns-zero-result-why/ Share on other sites More sharing options...
requinix Posted June 16, 2015 Share Posted June 16, 2015 How do you expect to get any rows before you've execute()d the query? 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 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. 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 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); ?> 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
Archived
This topic is now archived and is closed to further replies.