Jump to content

Query using PDO Undefined variable error


echosara

Recommended Posts

Hi i have created a query where i want some columns to be retrieved. i get these 2 errors and i dont understand why.

 

Notice: Undefined variable: db

Fatal error: Call to a member function query() on a non-object in 

 

 

this is my query

function get_products_all() { 

$connect = get_connected_db();

try {
	  $results = $db->query("                                  ***** error on this line****
                SELECT  id, product, description, price, picc
                FROM products ORDER BY id DESC
				");
		
    } catch (Exception $e) {
        echo "Data could not be retrieved from the database.";
        exit;
    }

    $result = $results->fetchALL(PDO::FETCH_ASSOC);
	
	return $result;
}

what am i doing wrong?

Link to comment
Share on other sites

your right it was supposed to be $connect. i changed that i still get the second error on the same line.

 

Fatal error: Call to a member function query() on a non-object in 

function get_products_all() { 

$connect = get_connected_db();

try {
	  $results = $connect->query("
                SELECT  id, product, description, price, picc
                FROM products ORDER BY id DESC
				");
		
    } catch (Exception $e) {
        echo "Data could not be retrieved from the database.";
        exit;
    }

    $result = $results->fetchALL(PDO::FETCH_ASSOC);
	
	return $result;
}
Link to comment
Share on other sites

So what is the value of $connect then? What does your get_connected_db() function look like? Remember that we don't have access to your server, so if you want us to help you, you need to give us the relevant information.

 

Also, please get rid of this terrible try-catch stuff. Do not catch exceptions unless you actually know how to solve the problem. It does not make sense to catch it only to display some meaningless error message on the screen. Now you've lost all the important information like what exactly happened, where it happened and in which context it happened. And what's the point of telling your users that you have issues with your database? They cannot help you with this, can they?

Edited by Jacques1
Link to comment
Share on other sites

this is my get_connected_db function

function get_connected_db(){
	try {
	$db = new PDO("mysql:host=localhost;dbname=test", "root", "");
	$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
	$db->exec("SET NAMES 'utf8'");
} catch(Exception $e){
	echo "could not connect to the database.";
	exit;
	
	}
	
}

And i took off the Try and Catch here to make it more cleaner

function get_products_all() { 

$connect = get_connected_db();

	  $results = $connect->query("
                SELECT  id, product, description, price, picc
                FROM products ORDER BY id DESC
				");
    $result = $results->fetchALL(PDO::FETCH_ASSOC);
	
	return $result;
}

still same error:  Fatal error: Call to a member function query() on a non-object in 

Link to comment
Share on other sites

The function doesn't return anything.

 

Besides that, never use SET NAMES in an application. Not sure where you even got this from, but it's a major security vulnerability. This query silently changes the character encoding while PDO still thinks you're using the old encoding. As a result, critical functionalities like escaping may break entirely, because they simply don't know the actual encoding. Even worse: Since you haven't turned off “emulated prepared statements” (PDO::ATTR_EMULATE_PREPARES), this problem will affect you even if you think you're using prepared statements.

 

The character encoding must be specified in the DSN string with the charset attribute.

<?php

$database = new PDO('mysql:host=localhost;charset=utf8;dbname=SOME_DATABASE', 'SOME_USER', 'SOME_PASSWORD', array(
	// important: turn off "emulation" of prepared statements
	PDO::ATTR_EMULATE_PREPARES => false,
	// turn on exceptions
	PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
	// fetch associative arrays by default
	PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
));

And of course you should never connect to the database as the superuser, except maybe at a very early stage of development.

Link to comment
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.