Can't connect to database through PHP PDO class - php

I'm trying to connect to my mySQL database using the PDO class in PHP.
Here is my Code :
// Connects to Our Database via PDO.
if($local) {
try {
$db = new PDO("mysql:=localhost;dbname=bbc_archive;port=3306", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch(Exception $e) {
echo "Connection to the DataBase was not possible. ";
die();
}
} else {
try {
$db = new PDO("mysql:=bbcarchive.db.11505263.hostedresource.com;dbname=bbcarchive", "bbcarchive", "myPassword");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch(Exception $e) {
echo "Connection to the live DataBase was not possible. ";
die();
}
}
The $local variable is defined before and determines whether or not the script is running on a the live server or a test server.
When running in my local environment everthing works fine but on my live server it echo's out "Connection to the live DataBase was not possible." from the catch block.
I've contacted my host provider (godaddy) and they think it's a coding error. I've also, obviously, checked the hostname, dbname, username and password a 100 times and it's all correct. I just can't see the problem!
How can i do this ?

Your DSN seems to be incorrect. The documentation on MySQL DSNs indicates that it should look somewhat like this:
$db = new PDO("mysql:host=localhost;dbname=bbc_archive;port=3306", "root", "");

Related

Database error in Ubuntu

im having a problem, my project is working in my windowsOS, and i try it to transfer in Ubuntu16.04 Operating system, with static ip address. now when i trying to access the project it keep saying Database Connection Error!
<?php
$db_password = "Welcome1";
$server = "192.168.2.218";
try{
$pdo_conn = new
PDO("mysql:host=$server;dbname=boldr","phpmyadmin","
{$db_password}");
} catch (PDOException $e){
die("Database Connection Error!");
}
$conn = mysqli_connect("192.168.2.218","phpmyadmin","{
$db_password}") or die(mysqli_error($conn));
mysqli_select_db($conn,'boldr') or die("cannot select DB");
?>
is your database on the same server? If yes try 127.0.0.1 or localhost.

Can't Link To MySQL database

I am learning MySQL/PHP and I cannot figure out how to connect to MySQL on my localhost. I have written a short bit of code and I included root as my user name and root as my password because I have not set these elements yet (as far as I know). I feel that perhaps I am missing something in regards to the username/password combination. However, I feel that this should not be an issue because I have not tampered with the default conditions.
I need some help.
My code is below:
<?php
try {
$db = new PDO("mysql:host=localhost;dbname=shirts4max;port=3306", "root", "root");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf-8'");
} catch (Exception $e) {
echo "Could not connect to the database.";var_dump($e);
exit;
}
I am only seeing the error message on my page:
"Could not connect to the database."
Thank for reading. Please help me Obiwan.
Show the real error. Avoid using root except for maintenance.
From the PDO Manual page here, modified for you
<?php
$dsn = 'mysql:dbname=shirts4max;host=localhost';
$user = 'root';
$password = 'root';
try {
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Have you tried connecting via command line using: mysql -uroot -proot -hlocalhost -p3306? If you can't connect that way, the PDO connection won't go through either. Playing with the connection info via command line gives you a nice easy way to find what works, then include it in your code. Also, if you've not set the user/pw, it's possible there isn't one currently, so you wouldn't need those parameters at all.

Choose which database connection to run

so i'm having my database connection in my project. The file has a local and an online request, one is commented out when i work on localhost.
is there a way in php for it to look up which one it has to use?
like if url = localhost/blabla use local else use online?
Here is the connection.
try{
//local connection
$db = new PDO('mysql:host = 127.0.0.1; dbname=XXXX', 'root','');
//local online
//$db = new PDO('mysql:host = XXXXX; dbname=XXXXX', 'xxxxx','*****');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo $e->getMessage();
die();
}
$db->exec("set names utf8");
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
Hope somone can help if it's possible, thanks
You shouldn't check for the URL but setup a config file. Then you set DEVELOPMENT on 0 on the server, and on 1 if you are local.
config.php
<?php
define('DEVELOPMENT', 1);
?>
database.php
<?php
if(DEVELOPMENT) {
// setup your local DB
} else {
// setup your live db
}
?>
You can know the IP through the $_SERVER variable,so:
define('LOCALHOST', 'XX.XX.XX.XX');
if($_SERVER["REMOTE_ADDR"] == LOCALHOST)
{
//open connection to the localhost users db
$db = new PDO('mysql:host = 127.0.0.1; dbname=XXXX', 'root','');
}else{
//open connection to rest of users db
$db = new PDO('whatever', 'root','');
}
Do this, put your db.php in it's own folder, after uploading it to the server, then change the user rights so you can't overwrite it with your local connection

MAMP localhost not Responding

I am trying to run php file using MAMP on my mac system having maverick operating system.
it was working good before but suddenly it stop responding.
when I run html file it work fine even when I run any .php file with out database PDO it work fine but when I try to run php file with PDO database connection, localhost does work.
any concrete suggestions. welcome !
<?php
try
{
$host ="localhost";
$db ="ijdb";
$user="ijdbuser";
$pwd = "ijdbuser";
$pdo = new PDO('mysql:host=$host; dbname = $db', $user, $pwd);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}
$output = 'Database connection established.';
include 'output.html.php';
There are a few reasons as to why it's failing.
1) Variables do not get parsed in singles quotes
2) You have spaces; there should not be any.
'mysql:host=$host; dbname = $db'
//^ ^ ^
needs to read as:
"mysql:host=$host;dbname=$db"
^ ^
You may also want to use:
$pdo->exec("SET NAMES utf8");
Nota:
You should also modify your catch{...} to read as:
catch(PDOException $e) {
print $e->getMessage();
}
to get the real reason as to why it's failing.

php connect to mysqlworkbench localhost

I have a mac with OSX 10.8.4. I have installed my localhost and it works just fine. I have made a php script, from where I would like to connect MySQL workbench database to. My apache tomcat server runs, and also mysql on the computer, and I use XAMPP. This is my code:
<?php
// Establish connection to DB using PDO
try {
$pdo = new PDO('127.0.0.1:3306', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
echo "Connected!";
} catch (PDOException $e) {
$error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
echo "Connection failed";
exit();
}
I have tried this script to connect to a remote mysql server, where it works fine, but I cannot use it for my localhost. I also tried just to put in localhost in new PDO, but still the same. Does anybody have a clue to what is wrong?
Best Regards
Mads
You'll have an easier time knowing what's not working if you echo the exception being thrown.
Your code
} catch (PDOException $e) {
$error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
echo "Connection failed";
}
doesn't actually print the exception! Try this instead:
} catch (PDOException $e) {
$error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
echo $error;
}
That will at least give you some helpful debugging info.

Categories