PDO error: no database selected - php

I'm making a register script and using PDO for the first time, but I get this error when trying to use it. The defines works fine when use MySQLi.
My code gives me this error:
Array ( [0] => SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected )
This is the code: (/register.php)
try{
$input_password_hash = password_hash($input_password, PASSWORD_DEFAULT);
$stmt = $PDO_new->prepare("SELECT user_name FROM users WHERE user_name = :username");
$stmt->bindParam(":username",$input_username);
$stmt->execute();
echo "code after exec";
//checks if user already exist in database.
if($stmt->rowCount()>0){
$error[] = "Username already exist";
echo "user is there";
}
else{
echo "user not there";
$insert = "INSERT INTO users(user_name, user_password_hash, user_email) VALUES($input_username,$input_password_hash, $input_email)";
}
} catch(PDOException $e){
$error[] = $e->getMessage();
}
This is the file for connecting to the databse: ((config/db_connect.php)
<?php
require_once($_SERVER['DOCUMENT_ROOT']."/config/db.php");
$PDO_new = new PDO("mysql:host=" . DB_HOST . ";DBName=" . DB_NAME, DB_USER, DB_PASS);
$PDO_new->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
And this is the file where I define the info for the database: (/config/db.php)
define("DB_HOST", "localhost");
define("DB_NAME", "username_databasename");
define("DB_USER", "username_admin");
define("DB_PASS", "password");

A quick test reveals that a PDO connection is case-sensitive in how it parses attributes. You use DBName when you should be using the lowercase dbname. This is not explicitly stated in the PDO::__construct() docs, as far as I can tell, but I was able to verify it testing in my own environment.
$PDO_new = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
//--------------------------------------------^^^^^^^^^^
It is unclear why you are also establishing a connection via MySQLi, as MySQLi and PDO are comparable but incompatible APIs. You generally need one or the other, not both (unless you are combining code from 2 sources that use opposite APIs).
It is recommended to wrap the new PDO() in a try/catch to catch connection errors before setting ERRMODE_EXCEPTION on it.
try {
$PDO_new = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
} catch (PDOException $e) {
// handle a connection error error
}
Finally, I would be remiss if I did not point out that you have plain variables in the INSERT statement rather than bound parameters. You should be binding values with placeholders here as you did in the first SELECT statement.
// Use placeholders please!
$insert = "INSERT INTO users(user_name, user_password_hash, user_email) VALUES(:input_username,:input_password_hash, :input_email)";
// prepare() then execute()...

Related

Does the PHP's define() not work with PDO connection string?

I was using the PHP's define() to define constants for my PDO connection string, ie. in mysqli. However, it just didn't seem to work. I kept getting the error: Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO) when I used the code below:
The PDO connection string would work when I didn't use PHP's define() function to pass in my connection variables. I'm using PHP 7, MySQL 8, and Apache 2.4.
Problem code below:
error_reporting(E_ALL); //check all type of errors
ini_set('display_errors', 1); // display those if any happen
//Database Connection Constant
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'root');
define('DB_NAME', 'gallery_db');
echo DB_PASS;
//phpinfo();
//$conn = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
try {
$dbc = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . "," . DB_USER, DB_PASS);
// set the PDO error mode to exception
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($dbc) {
echo "connected";
}
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Working Connection Code:
$servername = 'localhost';
$username = 'root';
$password = 'root';
$dbn = 'gallery_db';
//phpinfo();
try {
$dbc = new PDO("mysql:host=$servername;dbname=$dbn", $username, $password,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => false
));
// set the PDO error mode to exception
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($dbc) {
echo "connected";
}
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Why does PHP define() not work in PDO's connection string?
In the first instance you try this:
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.",". DB_USER, DB_PASS);
This only has two arguments, but PDO needs 3:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>
https://php.net/manual/en/pdo.connections.php
What happens is that your password constant works but went into the username part of the connection.
Fix:
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
Change
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.",". DB_USER, DB_PASS);
to
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);

Undefined property: PDO::$connect_error

I am trying to use $dbc->connect_error to check if any error occurs while trying to connect to my databease. I always get an error page saying:
Notice: Undefined property: PDO::$connect_error in
C:\xampp\htdocs\add_products_processing.php on line 7
I am using Windows7 with XAMPP v3.2.2. The full code is shown below. I am sure that the username and the password are correct. Any advice?
<?php
$dsn = 'mysql:host=localhost;dbname=technoglance';
$username = 'root';
$password = 'password';
$dbc = new PDO($dsn, $username, $password);
if ($dbc->connect_error) {
die("Connection failed: " . $dbc->connect_error);
}
$main_class =filter_input(INPUT_POST, 'main_class');
$brand =filter_input(INPUT_POST, 'brand');
$model =filter_input(INPUT_POST, 'model');
$description =filter_input(INPUT_POST, 'description');
$quantity =filter_input(INPUT_POST, 'quantity');
$adding_date =filter_input(INPUT_POST, 'adding_date');
$sell_price =filter_input(INPUT_POST, 'sell_price');
$buying_price =filter_input(INPUT_POST, 'buying_price');
if(!empty($main_class)){
try{
$query = "INSERT INTO products (main_class, brand, model, description, quantity, adding_date, sell_price, buying_price ) VALUES ('$main_class', '$brand', '$model', '$description', '$quantity', now(),'$sell_price', '$buying_price' );";
// set the PDO error mode to exception
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbc->exec($query);
echo "Thank you. The record has been sent successfully.<br><br>";
}
catch(PDOException $e){
echo $query . "<br>" . $e->getMessage()."<br><br>";
}
}
else{
echo '<h1>Please use the contact form or don\'t leave an empty field!</h1>';
}
?>
Here is what your code should be
<?php
$dsn = 'mysql:host=localhost;dbname=technoglance;charset=utf8';
$username = 'root';
$password = 'password';
$dbc = new PDO($dsn, $username, $password);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// filtering omitted
if(!empty($main_class)){
$query = "INSERT INTO products (main_class, brand, model, description, quantity, adding_date, sell_price, buying_price ) VALUES (?,?,?,?,?,?,?,?);";
$data = [$main_class,$brand,$model,$description,$quantity,$adding_date,$sell_price,$buying_price];
$dbc->prepare($query)->execute($data);
echo "Thank you. The record has been sent successfully.<br><br>";}
else{
echo '<h1>Please use the contact form or don\'t leave an empty field!</h1>';
}
PDO will report it's errors already, without any extra code required.
and you should be using prepared statements
If we have a look at the PDO manual and look up for the PDO class there is no connect_error property anywhere. But if we check mysqli manual we see it right there. You have to choose a database library and stick to it, they cannot be mixed.
I always recommend to configure PDO to throw exceptions as you already do (although connection errors in particular will always through an exception no matter your settings) and not care to catch them unless you want to do something specific with them.
There is no connect_error. You should use exception:
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Or if you do not want exception you can try errorCode and errorInfo

"Undefined method PDO::execute()" despite using prepare

This code has been working before, but I recently added a database class. I get the instance and connection from it:
$connection = MYSQLDatabase::getInstance()->getConnection();
$connection->prepare("INSERT INTO users etc etc.......
$insertArray = $connection->execute(array(
":username" => $_POST["username"]
));
getInstance() returns the database instance. getConnection() returns the connection property which contains:
new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
So the error occurs when doing $connection->execute despite that $connection contains the database object, the connection, and the prepared statement.
How can this be?
Create a variable to store your prepared statement then execute that.
$connection = MYSQLDatabase::getInstance()->getConnection();
$statement = $connection->prepare("INSERT INTO users etc etc.......");
$insertArray = $statement->execute(array(
":username" => $_POST["username"]
));

PHP PDO Doesn't insert new records

I do not understand what my problem seem to be.
I got the following PHP code:
include_once 'db.inc.php';
try
{
$db = new PDO(DB_INFO, DB_USER, DB_PASS);
}
catch(PDOException $e)
{
echo 'Connection failed: ', $e->getMessage();
exit();
}
$title = htmlentities($_POST['title']);
$entry = htmlentities($_POST['entry']);
$sql = "INSERT INTO entries (title, entry) VALUES (?, ?)";
$stmt = $db->prepare($sql);
$stmt->execute(array($title, $entry));
$stmt->closeCursor();
I do not receive any error of any kind and the script seem to have worked however it does not insert anything into the database.
No matter what I try it doesn't do anything.
edit
Sorted :)
I didn't know about $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);.
It gave me "Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected'".
Turns out I wrote mysql:host=127.0.0.1;db_name=test1 instead of mysql:host=127.0.0.1;dbname=test1 in my config file.
Thank you very much for help!
Set PDO to throw exceptions when execution fails
$db = new PDO(DB_INFO, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Reusing variables when working with PDO

I have to pull data from at least 3 databases, is there anything wrong with reusing my PDO objects?
$dbh = new PDO('mysql:host=' . $host . ';dbname=' . $db_name, $user, $password);
$sth = $dbh->prepare($query1);
// do something
$dbh = new PDO('mysql:host=' . $host2 . ';dbname=' . $db_name2, $user2, $password2);
$sth = $dbh->prepare($query2);
//do something else
Sorry for the edit but here's another consideration. With each of these I obviously should check whether or not the connection was successful and throw an exception if it wasn't:
if (!$dbh) {
$err=$dbh->errorInfo();
throw new Exception('Could not connect: ' . $err[2]);
}
I don't suppose there's a way to avoid this, unless I create all the connections at the same time and do if (!dbh1|!dbh2) { ... }. Just something else to consider.
When you assign $dbh to a new PDO() you are not technically reusing your pdo object. You are creating a new PDO instance and assigning it to a variable that you have previously used. There is nothing wrong with doing this as long as you understand what is occurring in your program.
EDIT:
I'm editing my answer to address the new question you've added to your previous question.
With each of these I obviously should check whether or not the connection was successful and throw an exception if it wasn't:
You can surround your connection attempt in a try catch which is a typical strategy for dealing with connection errors:
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=databaseName', $userName, $password);
foreach($dbh->query('SELECT * from TableName') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $ex) {
print "Error!: " . $ex->getMessage() . "<br />";
die();
}
?>
You can do that, but ... don't. Use another variable name (they are cheap) it will make your code much easier to understand. Heck you can (should) even use variable names that will clue you in on what database your object is connecting to, ie:
// connection to data warehouse
$dbh_dataWH = new PDO('mysql:host=' . $host . ';dbname=' . $db_name, $user, $password);
// connection to crm
$dbh_crm = new PDO('mysql:host=' . $host2 . ';dbname=' . $db_name2, $user2, $password2);
This way you will be able to re-use your connection objects, and the code in my very humble opinion will be much clearer for you or anyone else that has to maintain it.

Categories