Driver not found - PDO - AMPPS - php

I was using MAMP for a few months now, but today I installed AMPPS.
I am going to work with databases, but it doesn't work.
I get the following error:
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /Applications/AMPPS/www/functions.php:13 Stack trace: #0 /Applications/AMPPS/www/functions.php(13): PDO->__construct('mysql:host=loca...', 'root', 'usbw') #1 /Applications/AMPPS/www/functions.php(22): dbConn() #2 /Applications/AMPPS/www/test.php(18): imageDropdown('s') #3 {main} thrown in /Applications/AMPPS/www/functions.php on line 13
Code:
// database connection
function dbConn() {
$username = "root";
$password = "usbw";
$dbh = new PDO(
'mysql:host=localhost; port=3306; dbname=webshop',
$username,
$password
);
return $dbh;
}
// dropdown met afbeeldingen
function imageDropdown($zoek) {
$dbh = dbConn();
$sth = $dbh -> prepare("
SELECT afbeelding
FROM Afbeelding
WHERE afbeelding LIKE '%:zoek%'
ORDER BY afbeelding
");
$sth -> bindValue(":zoek", $zoek, PDO::PARAM_STR);
$sth -> execute();
$result = $sth -> fetchAll(PDO::FETCH_ASSOC);
print("<pre>");
print_r($result);
print("</pre>");
}
?>
(functions.php)
<?php
include "functions.php";
imageDropdown("s");
?>
(test.php)
There is a driver missing, but how do I install it?
I'm on Mac.

In user interface - PHP tab - php extension. Check if required extensions are enabled. Like pdo_mysql and pdo

Related

getting an error to connecting mysql db in php

I am working on a new project and I am getting an error when I connect with my DB I don't know why it is happening my DB name and user name is all correct but why I am getting this error
Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\treeview\treeview.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\treeview\treeview.php on line 9
Code:
<?php
mysql_connect('localhost', 'root');
mysql_select_db('test');
$qry = "SELECT * FROM treeview_items";
$result = mysql_query($qry);
$arrayCategories = array();
while ($row = mysql_fetch_assoc($result)) {
$arrayCategories[$row['id']] = array("parent_id" => $row['parent_id'], "name" => $row['name']);
}
I'll assume you are on PHP 7+, which means that mysql_query is deprecated. Please use PDO or mysqli.
More info on that function's man page: https://www.php.net/manual/en/function.mysql-query.php
You can obtain the connection to your database through:
$connection = new mysqli($hostname, $db_username, $password, $db_name);
if ($connection->connect_error){
$error = "Failed to connect to db: " . $connection->connect_error;
// Do whatever you want with $error
}
$connection->query("SET NAMES utf8mb4"); // mostly, people use utf8 instead

Connecting to AWS RDS database using beanstalk and PHP

In PHP 7, As I try to connect to the Beanstalk I have set up, I receive the error:
Fatal error: Uncaught Error: Class 'mysql' not found in C:\Apache24\htdocs\php_file.php, Stack trace: #0 {main} thrown in C:\Apache24\htdocs\php_file.php on line 41.
This server uses AWS RDS running MYSQL, and I am using apache 2.4 to localhost (for testing).
The code I'm using is:
$servername = "MY BEANSTALK CONNECTION";
$username = "Username";
$password = "PSSWD";
$dbname = "DBNAME";
$conn = new mysql($servername, $username, $password, $dbname);
$sql = "SELECT * FROM column";
$result = $conn->query($sql);
$conn->close();
My updated code uses mysqli, but is still getting the same error.
From PHP documentation mysql class is deprecated since version 5.5 and removed in version 7. that's why you're receiving the class not found error.
Try using mysqli. The PHP documentation has good examples on this. Look at example 2. I've pasted part of the example in this post for you.
https://www.php.net/manual/en/function.mysql-connect.php
<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
$res = $mysqli->query("SELECT 'choices to please everybody.' AS _msg FROM DUAL");
$row = $res->fetch_assoc();
echo $row['_msg'];
?>
Use mysql_connect($servername, $username, $password) instead.

MySQL won't load on localhost

I can't get mysql table to load on localhost. below i used PDO and the table worked fine. no errors...the table works fine in phpmyAdmin...please help driving me nuts. i imported table from my web server to local host from a earlier version..
<?php
$user = "root";
$pass = "root";
try {
$dbh = new PDO('mysql:host=localhost;dbname=iosLeads',$user,$pass);
foreach($dbh->query('SELECT * from Customer') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error! :" . $e->getMessage()."<br/>";
die();
}
?>
but when i used this code it show a blank screen, no errors..i tried same code with other tables and it works fine.
<?php
// set up the connection variables
$db_name = 'iosLeads';
$hostname = 'localhost';
$username = 'root';
$password = 'root';
// connect to the database
$dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
// a query get all the records from the users table
$sql = 'SELECT * FROM Customer';
// use prepared statements, even if not strictly required is good practice
$stmt = $dbh->prepare( $sql );
// execute the query
$stmt->execute();
// fetch the results into an array
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
// convert to json
$json = json_encode( $result );
// echo the json string
echo $json;
?>
I'm getting this in my php error.log
[17-Dec-2014 03:39:55 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10
[17-Dec-2014 03:40:00 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10
[17-Dec-2014 03:40:23 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10
If the array in $result has strings other than UTF-8 encoded, the function json_encode() may fail and will return false (see PHP manual json_encode). To check this, dump the value of $result before using json_encode():
var_dump($result);
If you have an array in $result but no other return value than false from json_encode(), you must set the correct character encoding for the database connection.

Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]

I converted the following working code:
<?php
include('config.php');
$link = mysql_connect($db_host, $username, $password);
mysql_select_db($db_name);
$id= $_POST["uniqi"];
$comments= $_POST["comments"];
$comments= mysql_real_escape_string($comments);
$comments = strip_tags($comments);
$update = "UPDATE mastertable SET comments = '$comments' WHERE id_pk= '$id'";
mysql_query($update, $link);
mysql_close();
header('Location: ccccc.com/pabrowser/… Updated');
?>
to PDO:
<?php
$id= $_POST["uniqi"];
$comments= $_POST["comments"];
$conn = new PDO("mysql:host=$db_host;dbname=$db_name", $username, $password);
$sql = "UPDATE mastertable SET comments=? WHERE id_pk=?";
$q = $conn->prepare($sql);
$q->execute(array($comments, $id));
header('Location: ccccc.com/pabrowser/… Updated');
?>
Which gave me
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)' in /home/content/58/9508458/html/pabrowser/comsumcompro.php:4 Stack trace: #0 /home/content/58/9508458/html/pabrowser/comsumcompro.php(4): PDO->__construct('mysql:host=;dbn...', NULL, NULL) #1 {main} thrown in /home/content/58/9508458/html/pabrowser/comsumcompro.php on line 4
You've forgotten include('config.php');
Your $db_host or $db_name value are wrong or you are supplying invalid credentials.
Please add
include('config.php');
Your connection string is wrong or the MySQL server is not responding. Use a try ... catch construction, like this:
include('config.php');
$id = $_POST["uniqi"];
$comments = $_POST["comments"];
try {
$conn = new PDO("mysql:host=$db_host;dbname=$db_name", $username, $password);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
And if you will see "Connection failed" message, you understand what's wrong.

PDOException not being caught?

I'm getting the following error in PHP:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'localhost' (10061)' in C:\xampp\htdocs\project\Service\Database.class.php:26 Stack trace: #0 C:\xampp\htdocs\project\Service\Database.class.php(26): PDO->__construct('mysql:host=loca...', 'root', '', Array) #1 C:\xampp\htdocs\project\Service\Database.class.php(54): Service\Database::initialize() #2 C:\xampp\htdocs\project\index.php(15): Service\Database::getHandler() #3 {main} thrown in C:\xampp\htdocs\project\Service\Database.class.php on line 26
The error itself is not the problem, I intentionally terminated the MySQL service in Windows to see what happened (I'm using XAMPP). The problem is that I'm unable to catch the exception that the PDO object throws and I don't know why.
try {
$host = "localhost";
$dbname = "project";
$userName = "root";
$password = "";
$charset = "utf8";
$dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
$driverOptions = array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES $charset"
);
// This is the line that supposedly throws the exception (LINE 26):
$dbh = new PDO($dsn, $userName, $password, $driverOptions);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::setHandler($dbh);
} catch (PDOException $e) {
die("CATCHED"); // This line is never reached
} catch (Exception $e) {
die("CATCHED"); // nor this one.
}
What am I missing here?
The only thing I can think of is if you're inside a namespaced class, and should use \PDOException instead of PDOException.
Turn on error_reporting and check the errors.
ini_set('display_errors', true);
error_reporting(E_ALL);
May be there is a fatal error, before that line, or maybe PDO is not available.

Categories