I try to do php artisan migrate and the error is
In Connection.php line 664:
could not find driver (SQL: select * from information_schema.tables where t
able_schema = stock_management and table_name = migrations)
In Connector.php line 67:
could not find driver
Connection.php line 664
try {
$result = $callback($query, $bindings);
}
// If an exception occurs when attempting to run a query, we'll format the error
// message to include the bindings with SQL, which will make this exception a
// lot more helpful to the developer instead of just the database's errors.
catch (Exception $e) {
-->line 664 throw new QueryException(
$query, $this->prepareBindings($bindings), $e
);
}
Connector.php line 67
protected function createPdoConnection($dsn, $username, $password, $options)
{
if (class_exists(PDOConnection::class) && ! $this->isPersistentConnection($options)) {
return new PDOConnection($dsn, $username, $password, $options);
}
--> line 67 return new PDO($dsn, $username, $password, $options);
}
Related
I have a database which has a semicolon in the name e.g. a;a. I can't rename the database. In SQL I would use the backticks to escape the name, but how can I do it in PDO DSN?
$db = 'a;a';
$dsn = "mysql:host=localhost;dbname=$db;charset=utf8mb4";
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int) $e->getCode());
}
Since ; is used as a separator in the DSN, PDO thinks my database is called a and I get this error:
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1049] Unknown database 'a'
I solved it by using double semicolon.
$db = 'a;;a'; // <-- means the database is called `a;a`
$dsn = "mysql:host=localhost;dbname=$db;charset=utf8mb4";
I just start learning about PDO, I need some help, I Installed PHPStorm, and I just start using it too, I already have a datbase on phpMyAdmin, I made this code, but it gives me an error
<?php
try {
$handler = new PDO ('mysql:localost;dbname=Database', 'root', 'password');
$handler -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOExeption $e)
{
die('Sorry, Database problem');
}
$query = $handler->query('select * from users');
while($r= $query->fetch())
{
echo $r['name'];
}
?>
here is the error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in C:\Users\user1\PhpstormProjects\PDO\pdo.php:11 Stack trace: #0 C:\Users\user1\PhpstormProjects\PDO\pdo.php(11): PDO->query('select * from u...') #1 {main} thrown in C:\Users\user1\PhpstormProjects\PDO\pdo.php on line 11.
Any help?
thanks in advance :).
Please make sure your database actually exists and that there are no spelling mistakes in your connection code.
Edit 1
I also noticed this issue with your connection I'm not sure if this is what is causing it but you need mysql:host= not just mysql:localhost. Also you have a spelling mistake localost.
Change this,
$handler = new PDO ('mysql:localost;dbname=Database', 'root', 'password');
To,
$handler = new PDO('mysql:host=localhost;dbname=myDb', $username, $password);
replace the handler declaration for this:
$handler = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
where myDatabase is the one you will be using as default..
I have this code
try {
$dbh = new PDO('mysql:host=localhost;dbname=db_informations', 'root', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e->getMessage();
}
And it gives me the exception message:
SQLSTATE[HY000] [1049] Unknown database 'db_informations'
Because the correct name of my database is db_information only.
My question is, even if I don't include the line:
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
I still get the same exception and I think it's not necessary to use it? Is it?
This is simply because that's the behaviour of PDO::__construct() as you can read in the manual:
PDO::__construct() throws a PDOException if the attempt to connect to the requested database fails.
But if you don't set the error mode to Exception and you do:
try {
$dbh = new PDO('mysql:host=localhost;dbname=db_informations', 'root', '');
$dbh->query("SELECT * FROM aTableWhichDoesNotExists");
} catch(PDOException $e) {
echo $e->getMessage();
}
You won't get any excpetion message or error, because you didn't set the error mode. So you need to do this:
try {
$dbh = new PDO('mysql:host=localhost;dbname=db_informations', 'root', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->query("SELECT * FROM aTableWhichDoesNotExists");
} catch(PDOException $e) {
echo $e->getMessage();
}
To receive an exception, which you then can catch:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.atablewhichdoesnotexists' doesn't exist
Also if you just think logically:
setAttribute() needs to be used with ->, which means you need an instance of the class to call that method. So how would you be able to call that method, if the instance couldn't be created correctly?
(So that would mean setAttribute() would have to bee static, so that you can set something/call it before you take the instance of the class)
private function _connect(){
try {
$this->con = new PDO(''.$this->dbdriver.':host='.$this->dbhost.';dbname='.$this->dbname.'', $this->dbuser, $this->dbpass);
$this->con->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
return TRUE;
} catch (PDOException $e){
$reg = registry::_getInstance();
$reg->offsetSet('R_errors', $reg->offsetGet('R_errors').'</br>'.$e->getMessage());return false;
}
}
I am using the above code to connect to the database but am getting the following errors:
Fatal error: spl_autoload(): Class PDO could not be loaded in /home/tahidihomes/public_html/lib/core/pdo_mysql.core.php on line 72
What might be the problem?
Retry after PDO installation. If you get same error try again with the below code in top of index page
spl_autoload_extensions('.php, .class.php');
spl_autoload_register();
I have a db.php file that has the following in it:
// db.php file
// creates connection to database
// DATABASE CONNECTION FUNCTION
function sql_con(){
try{
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
// log error to file and display friendly error to user
ExceptionErrorHandler($e);
exit;
}
}
I have a signup page and i want to check if username already exists, so i call my sql_con(); function beforehand to connect to database then do the below query
// connect to database
sql_con();
$stmt = $dbh->prepare("SELECT `user_login` FROM `users` WHERE `user_login` = ? LIMIT 1");
$stmt->execute(array($username));
if ( $stmt->rowCount() > 0 ) {
$error[] = 'Username already taken';
}
I'm very new to PDO and with the above i get the following errors:
Notice: Undefined variable: dbh in C:\wamp\www\signup.php on line 64
Fatal error: Call to a member function prepare() on a non-object in
C:\wamp\www\signup.php on line 64
Probably something very silly and I seem to confuse myself with PDO as I'm at the beginner stages. Could anyone tell me what I am doing wrong? Also I am not sure if this is the correct way as I'm new to PDO so if there's a more efficient way to do the username query check then please let me know.
This is because the $dbh object is limited to inside the try catch block and your sql_con() function due to scope.
The correct solution would be to remove the try catch block, and return the $dbh variable at the end of the sql_con() function.
Then:
try {
$dbh = sql_con();
$stmt = $dbh->prepare("SELECT `user_login` FROM `users` WHERE `user_login` = ? LIMIT 1");
$stmt->execute(array($username));
if ( $stmt->rowCount() > 0 ) {
$error[] = 'Username already taken';
}
}
catch (PDOException $e) {
//Do stuff with $e
}
The $dbh variable is destroyed once the function is done executing.
You could try returning the handle:
function sql_con() {
// your connection code
return $dbh;
}
Then in your signup page:
$dbh = sql_con();
Depending on your needs, a better alternative would be to employ a DI container.
You are defining pdo in function an ist not visible out it.
function sql_con(){
try{
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh; //this
} catch (PDOException $e) {
// log error to file and display friendly error to user
ExceptionErrorHandler($e);
exit;
}
}
$dbh = sql_con();
It will be better if you create a class for pdo abstraction.
$dbh = DB::getInstance();