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

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.

Related

I cannot connect MySQL web server

I added a database to my cPanel, but I cannot connect it.
try{
$db = new
PDO('mysql:host=markus.veridyen.com;dbname=sosyalki_pratiki1_ipss;charset=utf8','sosyalki','password');
}catch(PDOException $e){
echo 'Hata: '.$e->getMessage();
}
When I try to connect my database, it gives me this error message:
Hata: SQLSTATE[HY000] [2002] The connection could not be established because the target machine actively refused.
Notice: Undefined variable: db in C:\xampp\htdocs\ipss\survey.php on line 55
You still can use localhost as server name on a live server.
I'll assume you are using cpanel.
<?php
$servername = "localhost";
$username = "username"; //your cpanel username
$password = "password"; //your cpanel password
try {
$conn = new PDO("mysql:host=$servername;dbname=DatabaseName", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>

Dreamhost php sql error 'could not find driver'

I have made numerous attempts, most returning the odd error of 'could not find driver' when using PDO. I've confirmed multiple times with customer support that my credentials are correct. I've gone over the code looking for bugs, and found none. I built some simple tests, two examples below, one returning the error of 'Call to undefined function mysqli_connect()', the other returning the aforementioned ''could not find driver'.
I've been reading and googling and have yet to find a solution which is able to provide a working connection. Hoping for guidance on how to proceed
test 1: produces 'driver not found' error
$host = 'notmyHOST';
$db = 'notmyDATABASE';
$password = 'notmyPASSWORD';
$user = 'notmyUSERNAME';
$dsn = "mysqli:host=$host;dbname=$db;charset=UTF8";
try {
$pdo = new PDO($dsn, $user, $password);
if ($pdo) {echo "Connected to the $db database successfully!";}
} catch (PDOException $e) {
echo $e->getMessage();
}
//test 2: produces 'driver not found' error
//This test is a straight line-for-line copy of the example connect code
//provided by Dreamhosts knowledge-base
$hostname = "mysql.example.com"; //hostname created when creating the database
$username = "yourusername"; //username specified for database
$password = "yourpassword"; //password specified for database access
$database = "databasename"; //database name specified at creation
$link = mysqli_connect($hostname, $username, $password, $database);
if (mysqli_connect_errno()) {
die("Connect failed: %s\n" + mysqli_connect_error());
exit();
}
/*
test 3: produces 'Fatal error: Uncaught Error: Class "mysqli" not found in
/home/*****/*****/a___connectTEST4.php:25 Stack trace: #0 {main} thrown in
/home/*****/*****/a___connectTEST4.php on line 25' error
*/
$hostname = 'myHOST';
$database = 'myDATABASE';
$password = 'myPASSWORDS';
$username = 'myUSERNAME';
// Create connection
$conn = new mysqli($hostname, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
To recap, I'mooking for advice/solution/how to proceed to resolve connection error in any of the tests provided. In all tests I checked all credentials multiple times, they are correct; Haven't found a solution which maps to my problem on Stack Overflow, but I've looked at similar issues without success.

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

Invalid Data Source Name - Connection to MySQL with php, PDO and DSN

This script
<?php
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysqldg';
$user = 'odbc_dg';
$password = '999999999';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
gives the error *Connection failed: invalid data source name*
I have created an entry in /etc/odbc.ini as follows:
[mysqldg]
Description = DGDB
Driver = mysql537
Database = dg1
Servername = 99.99.99.99
UID = odbc_dg
PWD = 999999
SSLKeyFile = /etc/mysql/ssl/ck.pem
SSLCertFile = /etc/mysql/ssl/cc.pem
SSLCAFile = /etc/mysql/ssl/c1.pem
/etc/odbcinst.ini has the following entry:
[mysql537]
Description = MySQL driver for Plesk
Driver = /usr/lib/odbc2/lib/libmyodcb5w.so
Setup = /usr/lib/odbc2/lib/libmyodbc5w.so
The entry in odbcinst.ini works with a non-DSN connection.
I'm obviously missing something, can anyone help? Thanks.
UPDATED....
I have tried Your Common Sense's code as follows:
<?php
$host = '46.99.199.199';
$db = 'dg';
$user = 'odbc_dg';
$pass = '999999';
$charset = 'utf8mb4';
$options = array(
PDO::MYSQL_ATTR_SSL_KEY => '/etc/mysql/ssl/ck.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/etc/mysql/ssl/cc.pem',
PDO::MYSQL_ATTR_SSL_CA => '/etc/mysql/ssl/c1.pem'
);
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
... but I get a connection fail message - Connection failed: SQLSTATE[HY000] [2002]
I think the problem is that the keys I have supplied only work with ODBC
For example this code, which uses odbc_connect works....
<?php
ini_set ('error_reporting', E_ALL);
ini_set ('display_errors', '1');
error_reporting (E_ALL|E_STRICT);
$user = "odbc_dg";
$pass = "99999";
$connection = "Driver= {mysql537};Server=46.99.199.199;Database=dgdb;UID=dgdb;PWD=999999;sslca=/etc/mysql/ssl/c1.pem;sslkey=/etc/mysql/ssl/ck.pem;sslcert=/etc/mysql/ssl/cc.pem";
$con = odbc_connect($connection, $user, $pass);
$sql="SELECT Id from stk_item";
$rs=odbc_exec($con,$sql);
if (!$rs) {
exit("Error in SQL");
}
echo "<table><tr>";
echo "<th>Companyname</th>";
echo "<th>Contactname</th></tr>";
while (odbc_fetch_row($rs)) {
echo odbc_result($rs, "Id"), "\n";
}
odbc_close($con);
echo "</table>";
?>
My problem is that I want to connect to the remote database via pdo because that's the only type of connection allowed in the Drupal synchronising code.

Data Loaded: <br /> <b>Fatal error</b>: Uncaught exception 'PDOException' with message

I have searched for solutions but nothing seems to be working. I have added the port 3307 (web africa) south africa.but I still get this error
Data Loaded: Fatal error: Uncaught exception
'PDOException' with message 'SQLSTATE[HY000] [2002] Connection timed
out' in /home/sea503/public_html/phpsqlinfo_addrow.php:11 Stack trace:
0 /home/sea503/public_html/phpsqlinfo_addrow.php(11):
PDO->__construct('mysql:host=mysq...', NULL, NULL) 1 {main}
thrown in /home/sea503/public_html/phpsqlinfo_addrow.php on
line 11
My code looks like this
<?php
require("dbinfo.php");// database connections
// Get parameters from URL
$lat = $_GET["lat"];
$lng = $_GET["lng"];
$name = $_GET["name"];
$time = $_GET["time"];
$description = $_GET["description"];
$sighting = $_GET["sighting"];
//Connect to database
$dbh = new PDO("mysql:host=mysql.spri.co.za;port=3307;dbname=kruger_park_live",$sean_sql,$########);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
// Prepare INSERT statement new sigting
$stmt3 = $dbh->prepare("INSERT INTO tbl_maps (ID, map_client_name, client_time, client_lat, client_lng, client_description, client_sighting) VALUES (NULL, ?, ?, ?, ?, ?, ?)");
// Assign parameters
$stmt3->bindParam(1,$name);
$stmt3->bindParam(2,$time);
$stmt3->bindParam(3,$lat);
$stmt3->bindParam(4,$lng);
$stmt3->bindParam(5,$description);
$stmt3->bindParam(6,$sighting);
$stmt3->execute();
$data[] = array("name"=>$name, "lat"=>round($lat,6), "lng"=>round($lng,6),"time"=>$time,"description"=>$description,"sighting"=>$sighting);
//Data for success
echo json_encode($data);
}
catch(PDOException $e) {
echo "Error Message.". $e->getMessage() ;// Remove or modify after testing
file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", phpsqlinfo_addrow.php, ". $e->getMessage()."\r\n", FILE_APPEND);
}
//Close the connection
$dbh = null;
?>
and the dbinfo.php like this
<?php
$host= "****;port=3307"; $username="****"; $password="****"; $database="****"; $dbname="****";
?>
I have made "" to single ''
$dbh = new PDO("mysql:host=myhost;port=xxxx;dbname=db_name",$sean_sql,$########);
to
$dbh = new PDO('mysql:host=myhost;port=xxxx;dbname=db_name','$username','$password');
You are doing
$dbh = new PDO("mysql:host=...;dbname=...,$s..,$P..);
// I removed the actual values for your security
but your variables are
$host= "";
$username="...";
$password="...";
$database="...";
I believe you want to do
$dbh = new PDO("mysql:host=$host;dbname=$database",$username,$password);
you set your variable, but were using the values as the variable names
this is apparent when looking at this line in the error message -
PDO->__construct('mysql:host=mysq...', NULL, NULL)
^^^^ ^^^^

Categories