Solved

Call to a member function fetchAll() on a non-object

Posted on 2014-03-07
14
4,196 Views
Last Modified: 2014-03-09
hi,

i have the following function which i call upon logging in and it keep giving me the call on non-object what am i missing here. if i replace the prepare and just use ->query it seems to be fine but i read that if i do it that way i would run the risk of injection. any help is greatly appreciated

$conn= is my database connection

function get_session_data($userid){
	
	global $conn;
	//SELECT THE ITEMS FROM THE DATABASE YOU WANT
	$items="*";
	$senstive=array("password", "uuk","secret_q1","secret_q2","secret_ans1","secret_ans2");
	
	//GET THE INFORMATION FROM THE DATABASE
	$sql="SELECT ".$items." FROM bb_users WHERE uid=(:uid) LIMIT 1";	
	$res=$conn->prepare($sql);
	$data=$res->execute(array(":uid"=>$userid));
	$row = $data->fetchAll(PDO::FETCH_ASSOC);
	if($row){	
		foreach($row as $col =>$info){
			//DO NOT STORE THE PASSWORD OR OTHER SENSITIVE DATA
			if(!in_array($col,$senstive)){
			//SET EACH DATA VARIABLE AS A SESSION VARAIBLE
		
				$_SESSION[$col] =$info;	
			}
		}
	}else{
		//QUERY FAILED
		$warning="SESSION ERROR WITH SESSION".$conn->errorCode();
		print_r($warning);
		die;	
		
	}
	
}

Open in new window

0
Comment
Question by:M. Jayme Nagy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
14 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39913907
The most frequent cause of this message is a failing query.  The script did not test for query success and tried to use the result resource as if it were valid.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39913913
Try removing the colon from this statement.  It's used for the placeholder signal in the query string, but the array needs to use the field name without the colon.  I don't know why they designed it this way -- it's confusing and it would be easy enough to ignore leading colon.  But alas...

$data=$res->execute(array(":uid"=>$userid));

Change to:

$data=$res->execute(array("uid"=>$userid));

Some working examples are available in this article:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

If you look carefully at the way errors and exceptions are handled you will find ways to get PHP to tell you what went wrong.

HTH, ~Ray
0
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 39913917
so i

var_dump($data) = bool(true)

$data=$res->execute(array(":uid"=>$userid));
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 39913925
Oh i have been bouncing back and forth between that page, php.net and my project

 i changed

$data=$res->execute(array("uid"=>$userid));

same thing

i also tried
$row = $data->fetch(PDO::FETCH_ASSOC);
0
 
LVL 58

Expert Comment

by:Gary
ID: 39913926
Try this amendment

...
	$data=$res->execute(array(":uid"=>$userid));
	$row_count=$data->rowCount()
	$row = $data->fetchAll(PDO::FETCH_ASSOC);
	if($row_count>0){
...

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39913927
Just edited my earlier comment - refer back :-)
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39913933
Scrap the above.

...
	$res=$conn->prepare($sql);
	$res->execute(array(":uid"=>$userid));
	$row = $res->fetchAll(PDO::FETCH_ASSOC);
...

Open in new window


Notice you just execute the statement, no assignment.
0
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 39913941
i tried both ways no luck and when i do the row count

 Call to a member function rowCount()
0
 
LVL 58

Expert Comment

by:Gary
ID: 39913945
The colons is a preferential thing, you don't need them as pdo understands what you mean but it is the documented method.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39913947
This might work better.  It's not a complete solution but at least it will give you some hope of getting an error message that means something.  Obviously it's untested code because we do not have your data set, but it may be worth a try.

function get_session_data($userid)
{
    // A PDO OBJECT
    global $conn;
    var_dump($conn);
    
    // SELECT THE ITEMS FROM THE DATABASE YOU WANT
    $items = "*";
    
    // LIST THE COLUMN NAMES YOU DO NOT WANT
    $senstive = array("password", "uuk","secret_q1","secret_q2","secret_ans1","secret_ans2");
    
    // QUERY TO GET THE INFORMATION FROM THE DATABASE
    $sql  = "SELECT $items FROM bb_users WHERE uid=:uid LIMIT 1";
    
    // PREPARE THE QUERY
    try 
    {     
        $pdos = $conn->prepare($sql); 
    } 
    catch(PDOException $exc) 
    { 
        var_dump($exc);
        trigger_error($exc->getMessage(), E_USER_ERROR);
    }

    // BIND THE VARIABLE AND TRY THE QUERY
    $pdos->bindParam(':uid', $userid, PDO::PARAM_STR);

    try
    {
        $pdos->execute();
    }
    catch(PDOException $exc)
    {
        var_dump($exc);
        trigger_error($exc->getMessage(), E_USER_ERROR);
    }

    // ONLY ONE ROW WILL BE RETURNED
    $row = $pdos->fetchAll(PDO::FETCH_OBJ);
    
    // SHOW THE DATA
    var_dump($row);
}

Open in new window

If that gives you sensible results, you can add the rest of the logic back in and remove the calls to var_dump().
0
 
LVL 58

Expert Comment

by:Gary
ID: 39913948
0
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 39913951
how is your method different than mine

original
$sql="SELECT * FROM bb_users WHERE uid=(:uid) LIMIT 1";	
	$res=$conn->prepare($sql);
	$data=$res->execute(array(":uid"=>$userid));
	$row = $data->fetchAll(PDO::FETCH_ASSOC);

Open in new window


than yours - i thought mine just broke it out into more variables
$sql="SELECT ".$items." FROM bb_users WHERE uid=(:uid) LIMIT 1";	
	$res=$conn->prepare($sql);
	$res->execute(array(":uid"=>$userid));
	$row = $res->fetchAll(PDO::FETCH_ASSOC);

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39913956
Parentheses do not belong in the WHERE clause of this query.  I'll bet if there is error visualization that will be one of the things that SQL objects to.

To anyone coming across this question and answer in the future, please stop what you're doing and instead read the article linked below, carefully, for understanding.  The basics have all been covered in detail, with well-commented code examples that teach safe and predictable ways of getting the results you need.  If you want to be a programmer you have to understand this stuff!  Code fragments without any test data are a sure way to get confused and stay confused.  Don't waste your time guessing.   The article maps familiar SQL operations onto various data base extensions, including MySQL, MySQLi, and PDO (MySQL).

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
A responsive image gallery using flexbox 6 80
Do not understand error message 3 46
How can I send attachment with mail 5 26
Considerations PHP using multiple locations 9 34
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question