SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Querying a table using PDO

    Hello,

    Looking for a bit of help if possible. I'm using PDO for the first time to connect to a database and then query it. Now the PDO connection I have created is like so:

    PHP Code:
    try {
      
    $dbh 'mysql:host=ddddddddddd;dbname=sssssssssssss';
      
    $dbc = new PDO($dbh'iiiiiiiiiiiiiii','xxxxxxxxxxxxxx');
      
    $dbc ->setAttribute(PDO::ATTR_EMULATE_PREPARESTrue);
      
    $dbc ->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
      echo 
    "sucessfully connected";
      
    } catch (
    PDOException $e) {
      echo 
    'Failed: ' .$e->getMessage();
    }

    ?> 
    Now I know this connection is working as I echo "sucessfully connected" in the try/catch block to my page.

    However on a page where I include my connection and query a database like so:

    PHP Code:
    //Include PDO connection
    include('connect.php');

    $result mysql_query("SELECT * from db");
       
    $num mysql_num_rows($result);
       
    $n 0;    
         
        while(
    $row mysql_fetch_array($result)){
           echo 
    ''.$row['name'].''';  
        } 
    It then shows a load of 'access denied' messages, A link to the server could not be established etc when I run that query?

    Any ideas?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You're using mysql_* functions to query the database, you need to use the methods provided to you by the PDO object. In your case, $dbc.

    See, PDO::Query and PDO::Prepare.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, had a look at that before, but don't quite figure how to get it working with a while loop:

    PHP Code:
    //Include PDO connection
    include('connect.php');

    $SQL "SELECT * from db";

    foreach (
    $dbh->query($SQL) as $ROW){
           echo 
    ''.$ROW['name'].''';  
        } 
    But get the message Call to a member function query() on a non-object

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I would guess it's because you're using $dbh and not $dbc.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Sorry for the dumb questions! Doh!!

    One last thing i'm probably being thick about. I'm getting no error messages now, but the '.$ROW['name'].' is not outputting on the page?

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hey, no worries.

    Try var_dump'ing $row to see what it contains.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    first impulse: Unless you've named your database table 'db', your query's not quite right. Perhaps posting a schema of that table would be a good idea too.
    Second impulse: too many quotation marks on the echo line.

  8. #8
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Magic - worked it out thanks!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •