Fetchall Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn
Php Fatal Error: Call To A Member Function Fetchall() On A Non-object
more about Stack Overflow the company Business Learn more about hiring developers or pdo fetchall posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow call to a member function fetchall() on null Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Fatal error: Call to a member function fetchAll() on
Call To A Member Function Fetchall() On A Non-object Pdo
a non-object in pdo up vote 1 down vote favorite I'm new to pdo just tried the following and getting fatal error. $pdo = new pdo('mysql:localhost;widget_corp;charset=utf-8', 'root', ''); $query = $pdo->query("SELECT * FROM 'users'"); $result_array = $query->fetchAll(PDO::FETCH_ASSOC); php pdo share|improve this question asked Sep 7 '12 at 10:21 Yousuf Memon 1,66952348 does pdo have to be uppercase? –think123 Sep 7 '12 at 10:22 host=localhost;dbname=widget_corp
Pdo::fetch_assoc
just added this and worked. Want to know the reason. –Yousuf Memon Sep 7 '12 at 10:29 1 Actually, there are several issues: 1. dbname= in the DSN 2. check for failed queries and display the error using errorCode and you should not use apostrophe ' but rather the backticks ` –apfelbox Sep 7 '12 at 10:30 This should be put into an SQL debugging book somewhere on planet Earth. –think123 Nov 23 '12 at 5:31 add a comment| 4 Answers 4 active oldest votes up vote 5 down vote accepted Change: mysql:localhost;widet_corp to mysql:host=localhost;dbname=widget_corp Also in your posted code: $query = $pdo->query("SELECT * FROM 'users'"); you have the table name inside single quotes. In Mysql you should use a backtick instead for tables and columns. $query = $pdo->query("SELECT * FROM `users`"); share|improve this answer edited Sep 7 '12 at 10:37 answered Sep 7 '12 at 10:22 Fluffeh 25.8k144774 If this is the error, an exception will be thrown. –xdazz Sep 7 '12 at 10:26 host=localhost;dbname=widget_corp just added this and worked. Want to know the reason. –Yousuf Memon Sep 7 '12 at 10:28 1 @YousufIqbal Your connection string needs to contain certain info
Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers fatal error: call to a member function fetch() on boolean in Security Introduction General considerations Installed as CGI binary Installed as
Php Pdo Query
an Apache module Session Security Filesystem Security Database Security Error Reporting Using Register Globals User pdo select Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection http://stackoverflow.com/questions/12316185/fatal-error-call-to-a-member-function-fetchall-on-a-non-object-in-pdo handling Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Credit Card Processing Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and http://php.net/manual/en/pdostatement.fetchall.php Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box PDOStatement::fetchColumn » « PDOStatement::fetch PHP Manual Function Reference Database Extensions Abstraction Layers PDO PDOStatement Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug PDOStatement::fetchAll (PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)PDOStatement::fetchAll — Returns an array containing all of the result set rows Description public array PDOState
Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security http://php.net/manual/en/pdo.errorinfo.php Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting Using Register Globals User Submitted http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling call to Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Credit Card Processing Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character call to a Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box PDO::exec » « PDO::errorCode PHP Manual Function Reference Database Extensions Abstraction Layers PDO PDO Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug PDO::errorInfo (PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)PDO::errorInfo — Fetch extended error information associated with the last operation on the database handle Description public array PDO::erro
Insert Id 5 Running Simple INSERT, UPDATE, or DELETE statements 6 Running Statements With Parameters 6.1 Named Placeholders 6.2 INSERT, DELETE, UPDATE Prepared Queries 6.3 Preparing Statements using SQL functions 6.4 Executing prepared statements in a loop 7 Transactions 8 See Also 9 External links Why use PDO? mysql_* functions are getting old. For a long time now mysql_* has been at odds with other common SQL database programming interfaces. It doesn't support modern SQL database concepts such as prepared statements, stored procs, transactions etc... and it's method for escaping parameters with mysql_real_escape_string and concatenating into SQL strings is error prone and old fashioned. The other issue with mysql_* is that it has had a lack of attention lately from developers, it is not being maintained... which could mean things like security vulnerabilities are not getting fixed, or it may stop working altogether with newer versions of MySQL. Also lately the PHP community have seen fit to start a soft deprecation of mysql_* which means you will start seeing a slow process of eventually removing mysql_* functions altogether from the language (Don't worry this will probably be awhile before it actually happens!). PDO has a much nicer interface, you will end up being more productive, and write safer and cleaner code. PDO also has different drivers for different SQL database vendors which will allow you to easily use other vendors without having to relearn a different interface. (though you will have to learn slightly different SQL probably). Instead of concatenating escaped strings into SQL, in PDO you bind parameters which is an easier and cleaner way of securing queries. Binding parameters also allow for a performance increase when calling the same SQL query many times with slightly different parameters. PDO also has multiple methods of error handling. The biggest issue I have seen with mysql_* code is that it lacks consistent handling, or no handling at all! With PDO in exception mode, you can get consistent error handling which will end up saving you loads of time tracking down issues. PDO is enabled by default in PHP installations now, however you need two extensions to be able to use PDO: PDO, and a driver for the database you want to use like pdo_mysql. Installing the MySQL driver is as simple as installing the php-mysql package in most distributions. Connecting to MySQL old way: