I've been working on some beginner php exercise recently and have encountered an error to which I have no idea what is causing it.
It says
"Error: SQLSTATE[HY000] [1049] Unknown database 'otkrica'"
The sql file is correctly named "otkrica" and my db.php looks like this:
<?php
$dsn = "mysql:host=127.0.0.1;charset=utf8;dbname=otkrica";
try{
$pdo = new PDO($dsn,"root","");
}catch(PDOException $e){
die("Error: " . $e->getMessage());
}
?>
I would really appreciate input on this one. What am I missing?
The correct answer is found in the comments (credit to cmnardi). Posted as a clear answer for anyone looking:
Set the port you are using like: mysql:host=localhost;port=3307;dbname=testdb
I fix this problem wait this way
$connect = new PDO($dsn, $user, $pass);
$connect->exec("SET character_set_connection = 'utf8'");
$connect->exec("SET NAMES 'UTF8'");
<?php
$server="localhost";
$user="root";
$pass="";
$dbname="clicksite";
$dsn="mysql:host=$server;dbname=$dbname";
try{
$connect=new PDO($dsn,$user,$pass);
$connect->exec("SET character_set_connection = 'utf8'");
$connect->exec("SET NAMES 'UTF8'");
}catch(PDOException $error) {
echo "unable to connect".$error->getMessage();
}
?>
attention: use as wampserver ver2.5 for build database in the server
after run wampserver, click left on the icon in notify bar and then click on phpmyadmin and create the database.
Related
I'm trying to make a connection to the database while having the database config information saperated from the connection.
This is the code I wrote
db config information:
$config = array(
"database" => array(
"host" => "host",
"username" => "username",
"password" => "password",
"name" => "name",
)
);
define("databse_config", $config["database"]);
$config normaly also contains other information not relevant to the question ,that is why I have defined "database_config".
database connection:
require_once "config.php";
define("DB_HOST", databse_config['host']);
define("DB_USERNAME", databse_config['username']);
define("DB_PASSWORD", databse_config['password']);
define("DB_NAME", databse_config['name']);
function connect()
{
$dbhost = DB_HOST;
$dbuser = DB_USERNAME;
$dbpass = DB_PASSWORD;
$dbname = DB_NAME;
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$conn->exec("set names utf8");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
connect();
The problem I keep running in to is that wile trying to execute the function the page keeps loading and eventualy outputs the following error:
Connection failed: SQLSTATE[HY000] [2002] Connection timed out
I'm fairly new to PDO so I also tried the same while using mysqli, which gave the same result.
Any help would be appreciated.
Connection failed: SQLSTATE[HY000] [2002] Connection timed out
This means php tried to establish a TCP/IP connection to your database server AND FAILED. You provided a DB_HOST hostname that's unreachable by network from the location of your php program.
The hostname value could be missing, or it could be wrong. If you provided it by name (mysql-server.example.com), this error message tells you the DNS name lookup succeeded.
So your php sends out a request to the server for a TCP/IP connection. That request never gets a response. That means there's a firewall somewhere blocking the connection, or that there's no network route from your php program to the network.
This all means the $dbhost value is wrong in your new PDO() operation.
Your code could be simplified a lot like this with no loss of modularity. sprintf() helps. Simpler is better for troubleshooting.
require_once "config.php";
function connect()
{
try {
$connectionString = sprintf ("mysql:host=%s;dbname=%s",
database_config['host'],
database_config['name']);
$conn = new PDO($connectionString,
database_config['username'],
database_config['password']);
$conn->exec("set names utf8");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
connect();
And you can echo $connectionString; to see what's in it, and make sure it isn't something bizarre.
It's been many years since I did any dev in PHP.
The concept of connecting to MySQL using PDO is completely new to me, and I don't seem to be able to get it to work.
Using MySQLi, and the following code, which I know is deprecated, works nicely:
<?php
include 'models/db_details.php';
$db_connection = mysql_pconnect("$dbhost", "$dbusername", "$dbpasswd") or die("Couldn't connect to server: " . mysql_error());
$db = mysql_select_db("$dbname", $db_connection) or die("Couldn't select database.");
?>
<h1>After connecting to the DB</h1>
As expected, this displays:
After connecting to the DB
From all of the pages I have read, using PDO, this should look something like:
<?php
include 'models/db_details.php';
try
{
$conn = new PD0("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbusername, $dbpasswd);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
<h1>After connecting to the DB</h1>
The result of this is a completely blank page.
All of this is on a FreeBSD box, running:
Apache/2.4.25
PHP/5.6.30
MySQL 5.6.35
I have uncommented extension=php_pdo_mysql.dll in php.ini.
I can also confirm that the MySQL driver for PDO is installed:
What am I missing?
Check the font. Make sure it is pdo and not pd0 (zero).
Magic happens after that.
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.
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.
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", "");