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.
Related
I am trying to implement a login system on my website and therefore I need to connect a php file to a mySQL database. One problem, I keep getting this error:
Connection failed: Access denied for user 'username'#'servername' (using password: NO)
Does anyone know how to fix this error? Or can anyone tell me what I'm doing wrong?
I know this question has been asked like a million times already, but none of those answers have worked for me. They all are solutions for a local server and database, where mine is at a webhost so i think this works different.
Here's my code:
<?php
$servername = "servername";
$dBUsername = "db username";
$dBPassword = "db passwordd";
$dBName = "db name";
$dBPort = "db port";
$conn = new mysqli($servername, $dBUsername, $dPassword, $dBName, $dBPort);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
The variable name you are using in the password parameter slot is not the same as the one you defined above ($dBPassword vs $dPassword):
$dBPassword = "db passwordd";
// ...
$conn = new mysqli($servername, $dBUsername, $dPassword, $dBName, $dBPort);
In PHP the undefined variable "$dPassword" will be treated as a NULL which is likely why you're getting that "using password: NO" in the returned error.
I've been trying to upload my PHP MySQL(in Dreamweaver) project to a free web-hosting site.
When I logged in, there is an error that appear in dbconn.php file.
The error is shown below:
and here's the code in my dbconn.php file:
<?php
/* php& mysqldb connection file */
$user = 1350048; //mysqlusername to db
$pass = "password"; //mysqlpassword to db
$host = "eskl.freeoda.com"; //server name or ipaddress
$dbname= 1350048; // db name in server freeoda
$dbconn= mysql_connect($host, $user, $pass);
if(isset($dbconn)){
mysql_select_db($dbname, $dbconn) or die("<center>Error: " . mysql_error() . "</center>");
}
else{
echo "<center>Error: Could not connect to the database.</center>";
}
?>
I would really appreciate if anyone can teach me how to solve this.. thanks in advance!
As Kerbholz already stated, don't use mysql_* functions, they are really outdated.
Instead use mysqli:
$servername = "eskl.freeoda.com";
$username = "1350048";
$password = "password";
$database = "1350048";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
For your error it got mostly something to do your host doesn't allow remote connections. Try to change the serverhost to localhost or 127.0.0.1
I have a back end cron script that checks MySQl regurlarly for an if-then condition and then runs a Curl action. Anyway, the script has been running fine for over a year now when suddenly a problem occured and it does not make sense to me
here is the truncated code for the script
<?php
//assign values to variables
$servername = "localhost:3306";
$username = "username";
$password = "password";
$database = "database";
$con = mysqli_connect($servername, $username, $password, $database);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = "SELECT NewUserID FROM queue ORDER BY ID";
$queryresults = $con->query($results);
if($con->error) {
printf("Errormessage: %s\n", $con->error);
die();
}
I would see this error:
Warning: mysqli_connect(): (HY000/2002): Failed to parse address "localhost:3306:3306" in /home/foo/bar/BackEnd.php on line 11
Failed to connect to MySQL: Failed to parse address "localhost:3306:3306"
Fatal error: Uncaught Error: Call to a member function query() on boolean in /home/foo/bar/BackEnd.php:19
Stack trace:
#0 {main}
thrown in /home/foo/bar/BackEnd.php on line 19
The solution was simple, I change "localhost:3306" to "localhost" and everything worked again, since it seemed that ":3306" was being added to what was already there. This solution seems like a problem waiting to happen but im not sure what went wrong in the first place. Any ideas?
You can't specify the port in the host parameter.
Here is the full function definition:
mysqli_connect($host, $username, $password, $dbname, $port, $socket);
You should just add the port at the end of your call. Like this:
$con = mysqli_connect($servername, $username, $password, $database, 3306);
I am trying to create a simple mailing list webpage. Using LAMP. Here is the code I have for connecting to my database:
<?php
function insert()
{
$servername = "127.0.0.1";
$username = "root";
$password = "(my password here)";
$dbname = "(my db name here)";
try
{
// preparing database handle $dbh
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo ("Could not connect to server.\n");
echo ("getMessage(): " . $e->getMessage () . "\n");
}
}
?>
The function continues after that but this connection attempt gets cung up on the catch, and throws the "SQLSTATE[HY000] [2002] Connection refused" error. I have tried:
-Changing the $servername to localhost, but this gives me a different error: "SQLSTATE[HY000] [2002] No such file or directory"
-trying to specify all different ports
-checked all my info, database name and password.
-I can log into phpmyadmin and see that my database is fine
-looked at all other questions on this topic, with no help found.
I have a small page that I'm trying to building a journal with using PHP and My_SQL for both practice and recreation. I have a solid understanding of PHP but less so on My_SQL. Based on what I viewed it seemed that I can implement it almost like looking up a function and applying data to the corresponding areas. I tried doing the most basic thing and connect to my localhost server, but I cannot figure out why I am getting a 500 error. What is wrong with my server???
$servername = "http://127.0.0.1/";
$username = "root";
$password = "***";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else echo "Connected successfully";
`