Jump to content

Queries not working with PDO


3raser

Recommended Posts

I've been playing around with PDO lately, and I've been trying to get use to the basic functions as I've mentioned in some of my other posts/questions. Recently I switched to the ? bind/token (can't remember it's called), so I reformatted my processQuery. I've also tried to add in a fetching option in the method, but I can't seem to get anything to work.

 

I've looked up several internet tutorials but it seems like I'm doing everything the same. Yet, the query doesn't seem to run. :/

 

$database->processQuery2("INSERT INTO test (test, testa) VALUES (?, ?)", array('noob', 'newb2'), false);

 

Now for my method:

 

public function processQuery2($query, array $binds, $fetch)
    {
        $query_handle = $this->dbc->prepare($query);
        
        if(count($binds) > 0)
        {
            $i = 1;
            $value = array();
            
            foreach($binds as $bind)
            {
                $query_handle->bindParam($i, $value[$i]);
                $value[$i] = $bind;
                $i++;
            }
        }
        $query_handle->execute();
        
        //if($fetch == true)
        //{
            //return $query_handle->fetchAll();
        //}
    }

 

Any reasons to why it's not working? No errors are outputted, either.

Link to comment
Share on other sites

$query_handle->bindParam($i, $value[$i]);

 

should be:

 

$query_handle->bindParam($i, $binds[$i]);

 

The reason you are not getting any errors is because you don't have error reporting set to On (at the very least you would have gotten something like `Undefined index` since $value is empty) or display errors have been turned Off.

 

error_reporting(E_ALL);
ini_set('display_errors', 1);

 

For best effect these should be turned on in your php.ini

Link to comment
Share on other sites

Thanks for the replies guys. I did alter my code like so:

 

public function processQuery2($query, array $binds, $fetch)
    {
        $query_handle = $this->dbc->prepare($query);
        $query_handle->execute($binds);
        
        if($fetch == true)
        {
            return $query_handle->fetchAll();
        }
    }

 

Yet, no success. :/

Link to comment
Share on other sites

As ignace stated, you likely don't have error_reporting/display_errors set so that you would be getting any php detected errors. $this->dbc might not be a valid PDO instance. Your prepare() might be failing due to an error in the query statement.

 

What have you done to debug what your code is doing? Using var_dump on $this->dbc and on $query_handle might tell you what is working and what is not.

 

Link to comment
Share on other sites

I turned error reporting on, but no errors are outputted/returned. But I do do var_dump for both my PDO dbc object and the query_handle:

 

object(PDO)#2 (0) { } 
object(PDOStatement)#3 (1) { ["queryString"]=> string(53) "INSERT INTO users VALUES (id, ?, ?, ?, ?, ?, ?, ?, ?)" }

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.