+ Reply to Thread
Results 1 to 6 of 6

Thread: PDO Connection then SELECT result or results

  1. #1
    djalam is offline x10Hosting Member djalam is an unknown quantity at this point
    Join Date
    Jan 2008
    Posts
    71

    Question PDO Connection then SELECT result or results

    Hi, need some help getting column keys for a query, setting them as constant
    and WHILE its getting rows it echo's each $row["$column1"] and ["$column2"]
    I don't know how else to explain it so here is the script with and details in the comment section.
    PHP Code:
    <?php
    class PDO_connection {

        function 
    do_query($getme) {
        
            
    //random do stuff connecting
            
    try {
            
                    
    $this->dbh = new PDO("mysql:host=localhost;dbname=test_db"userpassword);

                    
    /*** set the error reporting attribute ***/
                    
    $this->dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
                    
    $STH $this->dbh->query($getme);
                    
    $STH->setFetchMode(PDO::FETCH_ASSOC);

                    
    //PAUSE - this is where we get results like echo $row['id']
                    
    while($row $STH->fetch()) {  
                    
    //*
                    
    The Problem here is that sometimes I want to query only one colum, or sometimes different column
                    the $getme variable above only bring in a statement like 
                    SELECT id FROM table1 ORDER BY updated_date DESC LIMIT 5
                    after 
    while $row $STH->fetch(), i want to be able to get the array results and get keys so I can 
                    
    do something like $row['id'] or if I'm getting 2 column results for a query then get those 2 keys
                    and set them up like echo $row['
    id'] .'is for'. $row['firstsname']. this function will only be used to get
                    different select statements.
                    *//
                    }
                    
            }  //End of Try


            catch(PDOException $e) {  
            echo "Currently Having Problems, try again later."; 
            }  //End of Catch
            }

    }

    $query_one = '
    SELECT id FROM table1 ORDER BY updated_date DESC LIMIT 5';
    $query_two = '
    SELECT idfirstname FROM table1 WHERE lastname=jordan ';
    $test = new PDO_connection();
    echo $test->caller($se);

    ?>

  2. #2
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: PDO Connection then SELECT result or results

    Basic idea:

    PHP Code:
    $row $STH->fetch();
    $column_names array_keys$row );
    foreach( 
    $column_names as $name ){
        
    $value $row$name ] ;
        
    # you now have value and column name.


  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PDO Connection then SELECT result or results

    The design looks potentially muddled. What is PDO_connection responsible for? What, exactly, do you want to do within the loop in do_query? Do you want to output anything within do_query? Make sure you keep DB access and display separate.

    If that's a fair representation of the code, creating a new PDO object with each query is wasteful.

    As for looping, you can iterate over both query results (since PDOStatement supports the Traversable and array keys & values (and object fields & values, while you're at it) using foreach.

    PHP Code:
    // $things is a PDOStatement
    foreach ($things as $item) {
      foreach (
    $item as $field => $value) {
        ...
      }

    Last edited by misson; 07-31-2010 at 01:56 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    djalam is offline x10Hosting Member djalam is an unknown quantity at this point
    Join Date
    Jan 2008
    Posts
    71

    Re: PDO Connection then SELECT result or results

    Quote Originally Posted by misson View Post
    If that's a fair representation of the code, creating a new PDO object with each query is wasteful.

    As for looping, you can iterate over both query results (since PDOStatement supports the Traversable and array keys & values (and object fields & values, while you're at it) using foreach.

    PHP Code:
    // $things is a PDOStatement
    foreach ($things as $item) {
      foreach (
    $item as $field => $value) {
        ...
      }

    Yea what i wanted to do was create a seperate file with the class that does query and gives me the results, instead of building a new type of pdo object for differnt type of results. The Problem for me is I don't know how to make it universal
    So if i threw a query at this class that says
    Select count(*) from Table .... Which would throw out 13

    Or I throw antoher query into this class like
    Select firstname, lastname, addresss from Table ... which would throw out an array

    i created an internal function to the class that would check to see if its an array
    PHP Code:
    private function is_assoc_array($array) {
                return (
    is_array($array) && !== count(array_diff_key($arrayarray_keys(array_keys($array)))));
            } 
    an if it was an array it would arrange the results each row.
    by getting array keys then arranging the result according to the key value.

    Thanks to dlukin and mission, i think i got the overview of what i should try to do next.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PDO Connection then SELECT result or results

    Quote Originally Posted by djalam View Post
    Yea what i wanted to do was create a seperate file with the class that does query and gives me the results, instead of building a new type of pdo object for differnt type of results.
    What does this class do that PDO and PDOStatement don't? What's PDO_connection's role in the overall design? It doesn't appear to offer enough to justify the added complexity.

    Quote Originally Posted by djalam View Post
    So if i threw a query at this class that says
    Select count(*) from Table .... Which would throw out 13

    Or I throw antoher query into this class like
    Select firstname, lastname, addresss from Table ... which would throw out an array
    Do you mean that you want to return a scalar in the first instance and an array in the second? Functions that sometimes return arrays and other times return scalars usually result in messy code.

    If you need to distinguish between single-value results, single-row results and multiple-row results, you should be using different classes or methods, assuming you need to use them at all. You could instead use PDOStatement::fetchColumn, a single PDOStatement::fetch and loop (foreach ($result as $row)) for multiple result rows. Either option should be used in the same layer where the statements are defined, in order to reduce coupling and increase cohesion.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    djalam is offline x10Hosting Member djalam is an unknown quantity at this point
    Join Date
    Jan 2008
    Posts
    71

    Re: PDO Connection then SELECT result or results

    Functions that sometimes return arrays and other times return scalars usually result in messy code.
    That pretty much sums it up. The wiki articles were a good read (although it went over my head on the first read). I'm going to be correcting it soon, so thanx for taking the time to help.

+ Reply to Thread

Similar Threads

  1. Replies: 5
    Last Post: 04-15-2010, 05:25 AM
  2. is this the final result?
    By gokhan214 in forum Free Hosting
    Replies: 1
    Last Post: 03-31-2010, 12:08 PM
  3. mysql_query(select) result improperly empty
    By lostcommander in forum Programming Help
    Replies: 12
    Last Post: 06-21-2009, 07:30 PM
  4. First result doesn't show.
    By sarvar in forum Programming Help
    Replies: 2
    Last Post: 12-08-2008, 01:47 PM
  5. 404 results in 404 (lol)
    By galaxyAbstractor in forum Free Hosting
    Replies: 2
    Last Post: 12-10-2007, 07:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers