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…

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’;

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

$pdo = new PDO('mysql:host=localhost;dbame =*****', user*****', 'pass*****');

Which I believe should be

$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.

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.

this is the error message from the PDOException… SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

Seeing as the the exception is coming from the first setting:

This line looks slightly wrong:


$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);

The 2nd argument contains an extra space after double colon, Should be


$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

There is another typo here:


$pdo = new PDO('mysql:host=localhost;dbame=*****', 'user*****', 'pass*****');  

Should be dbname


$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 :D.

Only because we both steadfastly worked through a list of typos - and then I cheated by searching on the error message :slight_smile:

@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.