Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write PHP PDO queries as `dbName`.`tableName` as opposed to `tableName` - why?

Tags:

php

mysql

pdo

I'm writing a user registration function in PHP PDO, and I have found that my query will only run fine if it is written like this:

<?php
    $dbHost="localhost";
    $dbName="project";
    $dbUser="admin";
    $dbPassword="abcd";
    $dbh=new PDO("mysql:host=$dbHost;dbName=$dbName", $dbUser, $dbPassword);
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $query=$dbh->prepare("INSERT INTO project.users (userName, userEmail) VALUES (?,?)");
.....

On the other hand, it will not run if I write:

...
$query=$dbh->prepare("INSERT INTO users (userName, userEmail) VALUES (?,?)");
...

In that case, I get the following error message:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in C:\wamp\www\Tests\Test03\Index.php:11 Stack trace: #0 C:\wamp\www\Tests\Test03\Index.php(11): PDOStatement->execute() #1 {main} thrown in C:\wamp\www\Tests\Test03\Index.php on line 11

Why is it that I need to precise project.users? Why isn't it enough to enter the table name, given that the db name itself is already in the PDO object?

Thank you!

JDelage

UPDATE Please see accepted answer below. Replacing dbName=$dbName with dbname=$dbName solves this problem.

like image 758
JDelage Avatar asked Jun 09 '11 23:06

JDelage


People also ask

What is the advantage of using PDO PHP data objects over MySQLi in PHP?

Both MySQLi and PDO have their advantages: PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. So, if you have to switch your project to use another database, PDO makes the process easy.

What is the advantage of PDO comparing to MySQLi?

The main advantage of PDO over MySQLi is in the database support. PDO supports 12 different database types, in opposition to MySQLi, which supports MySQL only. When you have to switch your project to use another database, PDO makes the process simpler.

Which of the following best describes the purpose of PDO :: prepare in PHP?

Also, calling PDO::prepare() and PDOStatement::execute() helps to prevent SQL injection attacks by eliminating the need to manually quote and escape the parameters.

What is PDO query?

PDO (PHP Data Objects) is an abstraction layer for your database queries and is an awesome alternative to MySQLi, as it supports 12 different database drivers. This is an immense benefit for people and companies that need it.


1 Answers

Apparently PDO was unable to set active database to be "project" and therefore you need to specify it every time.

Try to modify your line to look like this:

$dbh=new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPassword);

The only difference is that dbname is spelled all lower-case instead of yours dbName.

Alternatively, execute this SQL command after successfully establishing a connection: USE project;, e.g.

$dbh->exec('USE project;');
like image 182
LazyOne Avatar answered Nov 15 '22 03:11

LazyOne