Issue with PHP (Warning: PDO::__construct() ) - php

Hello guys,
I know this might seem to be a duplicate question but i am having an issue connecting to my database with PDO. I tried interchanging the server name from localhost to '127.0.0.1' but to no avail i have gained success. I am here testing and trying to create my own database for my own personal use and to get an understanding of how querying a database works, but my lack of knowledge brought me here. Can someone just point me in the right direction.
a snippet of my code is below
<?php
require_once('CONFIG.php');
session_start();
try {
$database_handler = new PDO('mysql:host=' . $databasehost . 'houses', 'root', '');
}
catch (PDOException $e) {
print "Error: " . $e->getMessage();
}
?>
So i have my Config.php file which has
<?php
$databasehost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'house';
$conn = mysql_connect($databasehost , $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'house';
mysql_select_db($dbname);
?>
Any help would be greatly appreciated

The format of your DSN is wrong
<?php
require_once 'CONFIG.php';
session_start();
try {
$dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8', $databasehost, $dbname);
$database_handler = new PDO($dsn, $dbuser, $dbpass, array(
PDO::ATTR_EMULATE_PREPARES=>false,
PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
}
catch (PDOException $e) {
print "Error: " . $e->getMessage();
}
and drop the lines
$conn = mysql_connect($databasehost , $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'house';
mysql_select_db($dbname);
from your Config.php

Related

Mysql multiple database connection doesn't work

I just tried to connect a secondary database like this example bellow but i don't know why refuse to work. Any idea?
I mention that each database connection works properly individualy.
$db_HOST = "localhost";
$db_USER = "db_user";
$db_PASS = "db_pass";
$db_NAME1 = "db_test1";
$db_NAME2= "db_test2";
$db_LINK1 = mysql_connect($db_HOST, $db_USER, $db_PASS) or die("Technical revision. Please try again later!");
mysql_select_db($db_NAME1, $db_LINK1) or die("Couldn't select database");
$db_LINK2 = mysql_connect($db_HOST, $db_USER, $db_PASS, true) or die("Technical revision. Please try again later!");
mysql_select_db($db_NAME2, $db_LINK2) or die("Couldn't select database");
Errors i get in the log file:
PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /config/global/variables.php on line 27
PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in config/global/hello.php on line 3
Thank you!
Do the following (with PDO instead of mysql_connect as the latter is deprecated):
$db_HOST = "localhost";
$db_USER = "db_user";
$db_PASS = "db_pass";
$db_NAME1 = "db_test1";
$db_NAME2= "db_test2";
try {
$db_LINK1 = new PDO('mysql:host='.$db_HOST.';dbname='.$db_NAME1, $db_USER, $db_PASS);
foreach($db_LINK1->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
try {
$db_LINK2 = new PDO('mysql:host='.$db_HOST.';dbname='.$db_NAME2, $db_USER, $db_PASS);
foreach($db_LINK2->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
More info here: php.net/manual/en/pdo.connections.php
If the second connection fails check the exact error message.
You should really use PDO but your code works if you use your link in your db select. For example:
$db1 = mysql_connect($hostname, $username, $password);
$db2 = mysql_connect($hostname, $username, $password, true);
mysql_select_db('database1', $db1);
mysql_select_db('database2', $db2);
that should work. You forgot to select which database should be used on every link.
I thnk I found the problem.
I forgot to change all queries accordind to the new multiple connection.
LE: Solve confirmed! Thank you all!

PHP MySQL connecting error

I have a question that is really bothering my understanding of working with php and MySQL. The problem is when i try to connect to my online DB.
If I Write my code like this:
define('DB_NAME', 'name');
define('DB_USER', 'user');
define('DB_PASS', 'pass!');
define('DB_HOST', 'localhost');
I connect with no problems. Great, that works!
But, when i want to connect like this:
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "name";
$conn = new mysqli($servername, $username, $password, $dbname);
I get this error: "Access denied for user..."
Can somebody please help me with this silly problem?
This code works for both options
<?php
//Creates static credentials
define('DB_NAME', 'data');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
//Creates connection to the database
$con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
//Checks for connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
//If there are no connection, error
if (!$con) {
die('Could not connect' . mysqli_error());
}
//Select the 'data' database
$con->select_db(DB_NAME);
//Checks if database 'data' has been selected
if (mysqli_select_db($con, DB_NAME)) {
echo "Database exists <br>";
} else {
echo "Database does not exist";
}
if($con){
echo "connection succss";
}
?>
option 2
<?php
$servername="localhost";
$username ="root";
$password="";
$dbname="test";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo "connected";
}
?>
Those are my codes, and also yours is working fine, might be a problem, with your username/password
Try this:
$mysql_host = 'localhost';
$mysql_user = 'user';
$mysql_password = 'pass';
$mysql_database = 'localhost';
$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password);
#mysql_select_db($mysql_database);
Also check the privelges for that user.
This happens some time because you have changed user name and password of data base or you can try make your user as root and password empty.

MySQL and PHP connection failing

I have an issue with PHP script not connecting to MySQL database. Below code always generates error:
$dbconn = #mysql_connect('SFDC1', '<username>', '<password>');
if (!$dbconn)
{
die('Error connecting to DB!');
}
There is no issue if I connect to the database using MySQL workbench with same credentials. Issue only occurs during the communication between PHP and MySQL.
Any help on debug of this issue?
Try this one;
$dbhost = "localhost";
$dbuser = "username";
$dbpass = "pass";
$dbname = "DBname";
if(!#mysql_connect($dbhost,$dbuser,$dbpass)) {
echo "Cannot connect";
die();
} else
mysql_select_db($dbname);// or report();
Use this code to connect mysql.
<?php
$dbconn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$dbconn ) {
die('Could not connect: ' . mysql_error());
}
?>

php syntax database connect

I'm trying to learn this stuff. Please be gentle.
Something is wrong here:
$dbhost = "localhost";
$dbname = "something_dbname";
$dbuser = "something_user";
$dbpass = "pwpwpwpw";
$dberror1 = "Could not connect to the database!";
$dbconnected = "you are connected!";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1);
$select_db = mysql_select_db($dbname . $dbconnected) or die ($dberror1);
where is my mistake? I want $dbconnected to show...
I can just as easily use
echo "hello";
it shows that I connect but I'm trying to get familiar with using multiple variables.
would this be better?
$dbhost = "localhost";
$dbname = "something_dbname";
$dbuser = "something_user";
$dbpass = "pwpwpwpw";
$dberror1 = "Could not connect to the database!";
$dbconnected = "you are connected!";
if ($mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname))
echo $dbconnected;
else die($dberror1);
Right now you are trying to connect to a database called something_dbnameyou are connected. The . concatenates variables into one string.
To fix your immediate problem, try this:
First, define $dbhost - I don't see it in your code.
Then change the last line to this:
$select_db = mysql_select_db($dbname) or die ($dberror1);
Then, just echo $dbconnected;
If you are not connected, the page will have called die, and will never reach the line that echos $dbconnected. If you are connected, the program will proceed to this next line and echo your success message.
Or you can do it more explicitly like this:
if ($select_db = mysql_select_db($dbname))
echo $dbconnected;
else die($dberror1);
To fix the bigger problem, DON'T use mysql_*. Read this for more information.
mysqli or pdo are far better options, and you can accomplish the same task easier, for instance, connecting to a db with mysqli is just:
$mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname);
Or you can do it procedural style, which is closer to your current code. The following snippet is from the php manual, on the page I linked in the comment below.
$link = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
I'd strongly recommend using PDO. The connection string is similar and can be done using:
// I do not see $dbhost defined in your code. Make sure you have it defined first
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo $dbconnected; // will print out the connection success message
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
To answer your question about not using mysql_* functions, you can check out this

Can't make mysql connection

If I use the following code, it works:
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
But when I do this, it doesn't:
$db_host='localhost';
$db_id='root';
$db_pass='';
$con = mysql_connect($db_host, $db_id, $db_pass);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
Trying to swap (") and (').
mysql_ functions are discouraged for new applicationa you are advised to use mysqli or PDO. The following code uses PDO to connect to database.
//dependant on your setup
$host= "localhost";
$username="XXX";
$password="YYY";
$database="ZZZ";
// connect to the database
try {
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
//Remainder of code
}
catch(PDOException $e) {
echo "I'm sorry I'm afraid you can't do that.". $e->getMessage() ;// Remove or modify after testing
file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]'). $e->getMessage()."\r\n", FILE_APPEND);//Change file name to suit
}
// close the connection
$dbh = null;
try using a script like this
$db_host = 'localhost';
$db_id = 'root';
$db_pass ='';
$con = mysql_connect ($ db_host, $ db_id, $db_pass) or die ('Could not connect:'. mysql_error ());
That code is fine. Review the error log- the problem has to be external to that code.

Categories