Jump to content

pdo issue


purencool

Recommended Posts

hi phpfreaks,

 

I have been using learning the pdo library. But I am really stuck on this one query. The above sql statement works if in phpadmin I get three results but for the life of me I can't get them with the code below. I have tried I am trying to return all columns in an array some thing like

 

array (0=>array(0=>business blah, etc

 

)

 

I have been playing with the fetch mode to try and achieve this but I can't get any results  not even an obj but I am not getting any errors. Is the code below wrong?

	$query = "SELECT Business_Name,First_Name,Last_Name,Username,Email,Phone,Mobile,Social_Media,Web_Site,User_Language,Admin_Level FROM customer WHERE Admin_Level ='Admi'";
  try {
            //$this->dataObj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
             $this->dataObj->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
            $sth = $this->dataObj->prepare($query);
            $stmt = $this->dataObj->query($query);
            $stmt->setFetchMode();
            $result = $stmt->fetchAll();
        } catch (PDOException $e) {

            $e->getMessage();
        }

        return $result;
    
}

 

Link to comment
Share on other sites

I have been trying your ideas and have modified the code a little but still not luck my results

 

-trying a smaller query. In phpadmin works perfectly three results.

-admi is correct thanks for idea.

-removed fetch mode and made no difference still get the same result.

-added PDO::FETCH_ASSOC to fetch all.

 

current code as with changes any other ideas?

private function QryFtchClmns($query){
	$query = "SELECT Admin_Level FROM customer WHERE Admin_Level ='Admi'";
  try {
            //$this->dataObj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
             $this->dataObj->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
            $stmt= $this->dataObj->prepare($query);
            $stmt = $this->dataObj->query($query);
            //$stmt->setFetchMode();
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {

            $e->getMessage();
        }

        return $result;
    
}

Link to comment
Share on other sites

The way to use prepare is demonstrated on the manual page here: http://www.php.net/manual/en/pdo.prepare.php

 

$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':calories' => 150, ':colour' => 'red'));

 

But your code should work with just the query, even though the prepare is not doing anything.

 

Does your code return an empty array?  If so, try without the "where" condition.

Link to comment
Share on other sites

ok I modified the code with a place holder as below and got this

 

Array ( [0] => [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 4 [8] => 2 [9] => 1 )

 

private function QryFtchClmns($query){
	$sql = "SELECT Admin_Level FROM customer WHERE Admin_Level = ?";
	$query = 'Admi';

  try {
            //$this->dataObj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
             $this->dataObj->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
            $stmt= $this->dataObj->prepare($sql);
            $stmt = $this->dataObj->query($query);
            //$stmt->setFetchMode();
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {

            $e->getMessage();
        }

        return $result;
    
}

Link to comment
Share on other sites

:D :D :D :D :D :D :D

 

thanks for all your help. I had two issues on is my pdo syntax and the public get method was not calling the private method I have online was it was calling another pdo method in same class and it had similar issues. As so as I called the method I have here on display the errors came thick and fast.

 

Thanks for your help again everyone.

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.