SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    28
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Help me with this PHP and MYSQL code

    am having little problem with SELECT statement in my PHP and MYSQL tutorial....when I connect to my database and I want to use PHP try-catch to SELECT the database content with the code below butI always get a PDOException error message that say "no database selected"


    try
    {
    $sql = 'SELECT booktext FROM book';
    $result = $pdo->query($sql);
    }
    catch (PDOException $e)
    {
    $error = "error fetching books: " . $e->getMessage();
    exit();
    }

    while ($row = $result->fetch());
    {
    $jokes[] = $row['booktext'];
    }

    help me out....

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,067
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    You should have a statement that defines $pdo, can you show it to us (replace your username, password, etc with XXXXXX)
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    28
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    You should have a statement that defines $pdo, can you show it to us (replace your username, password, etc with XXXXXX)
    Thanks for getting back to me....This is the code am trying to run but each time I run the script I get the error message that says "no datebase selected".


    <?php
    try

    {

    $pdo = new PDO('mysql:host=localhost;dbame =*****', user*****', 'pass*****');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);
    $pdo->exec('SET NAMES "utf8"');

    }
    catch (PDOException $e)
    {
    $error = "unable to connect to database server";
    include 'error.html.php';
    exit();
    }

    try
    {
    $sql = 'SELECT booktext FROM book';
    $result = $pdo->query($sql);

    }
    catch (PDOException $e)
    {
    $error = "error fetching books: " . $e->getMessage();
    include 'error.html.php';
    exit();
    }

    while ($row = $result->fetch());
    {
    $books[] = $row['booktext'];
    }

    include 'books.html.php';

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,067
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Not sure if it is a typo when placed on the forum, or if it really is a typo in your code, but I see a few small things (though I'm not sure if they are the cause yet)

    You have
    PHP Code:
    $pdo = new PDO('mysql:host=localhost;dbame =*****'user*****', 'pass*****'); 
    Which I believe should be
    PHP Code:
    $pdo = new PDO('mysql:host=localhost;dbame=*****''user*****''pass*****'); 
    I removed an extra space after dbname (dobut this makes a difference), but it might
    Added a ' quote in front of user*****'

    Also as a future FYI, if you place your code inside [ php ] and [/ php ] (without the extra spacing in between the [ ], it will format it for you on the forum.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    28
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I have done what you ask me to do and am still getting the same error message...no database selected...help me with what am doing wrong.

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    28
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    this is the error message from the PDOException...... SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Seeing as the the exception is coming from the first setting:

    This line looks slightly wrong:
    PHP Code:
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO:: ERRMODE_EXCEPTION); 
    The 2nd argument contains an extra space after double colon, Should be
    PHP Code:
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION); 

  8. #8
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    28
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Seeing as the the exception is coming from the first setting:

    This line looks slightly wrong:
    PHP Code:
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO:: ERRMODE_EXCEPTION); 
    The 2nd argument contains an extra space after double colon, Should be
    PHP Code:
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION); 
    Cups am still getting same error message after the correction....

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    There is another typo here:
    PHP Code:
    $pdo = new PDO('mysql:host=localhost;dbame=*****''user*****''pass*****'); 
    Should be dbname
    PHP Code:
    $pdo = new PDO('mysql:host=localhost;dbname=*****''user*****''pass*****'); 

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,067
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    There is another typo here:
    PHP Code:
    $pdo = new PDO('mysql:host=localhost;dbame=*****''user*****''pass*****'); 
    Should be dbname
    PHP Code:
    $pdo = new PDO('mysql:host=localhost;dbname=*****''user*****''pass*****'); 
    ding ding ding, we have a winner! I think you found it! Good catch @Cups ; even I missed that .
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Only because we both steadfastly worked through a list of typos - and then I cheated by searching on the error message

    @aosworks ; When faced with problems like this read it, re-read it and if it still does not work type it out again, get used to doing that because it is part of the nature of the business.

    We read things and re-read them but our brains tend to interpret what it thinks should be there, its an occupational hazard we have to bear. Using a good IDE which recognises PHP syntax will help a bit, but not in all cases.

  12. #12
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    28
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Am happy that you guys are here, I thank you so much..@Cups, @cpradio big thanks....hope to see you there again on issues..thanks to everyone...more power to PHP...the database is now selected..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
  •