I was using the PHP's define() to define constants for my PDO connection string, ie. in mysqli. However, it just didn't seem to work. I kept getting the error: Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO) when I used the code below:
The PDO connection string would work when I didn't use PHP's define() function to pass in my connection variables. I'm using PHP 7, MySQL 8, and Apache 2.4.
Problem code below:
error_reporting(E_ALL); //check all type of errors
ini_set('display_errors', 1); // display those if any happen
//Database Connection Constant
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'root');
define('DB_NAME', 'gallery_db');
echo DB_PASS;
//phpinfo();
//$conn = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
try {
$dbc = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . "," . DB_USER, DB_PASS);
// set the PDO error mode to exception
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($dbc) {
echo "connected";
}
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Working Connection Code:
$servername = 'localhost';
$username = 'root';
$password = 'root';
$dbn = 'gallery_db';
//phpinfo();
try {
$dbc = new PDO("mysql:host=$servername;dbname=$dbn", $username, $password,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => false
));
// set the PDO error mode to exception
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($dbc) {
echo "connected";
}
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Why does PHP define() not work in PDO's connection string?
In the first instance you try this:
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.",". DB_USER, DB_PASS);
This only has two arguments, but PDO needs 3:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>
https://php.net/manual/en/pdo.connections.php
What happens is that your password constant works but went into the username part of the connection.
Fix:
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
Change
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.",". DB_USER, DB_PASS);
to
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
Related
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!
I have newly purchased server, on that server database connections are not working.
<?php
error_reporting(E_ALL);
$server = "server name";
$user = "username";
$password = "password";
$db = "test";
echo "Before";
$con = mysql_connect($server, $user, $password);
echo "After";
if (!$con){
die('Could not connect:' . mysql_error());
}
mysql_select_db($db, $con);
?>
When run this file its print Before text but not print After text.
Currently you can use the following code:
ini_set("error_reporting", E_ALL & ~E_DEPRECATED);
Using this you can get either deprecated or not.
FYI: The mysql_* functions have been removed in PHP7.x. There are two modules you can use.
The first is MySQLi, just use the code as follows:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
You can also use PDO using code :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $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();
}
?>
This is my situation:
I'm trying to connect to my MySQL database with a PHP file on my Apache server, now: my PHP can connect to the MySQL database when I run it from the terminal (using "php -f file.php") but when I execute it from a web page it just doesn't connect.
This is my php file:
echo "TRY CONNECTION";
$conn = mysql_connect($servername, $username, $password);
echo "EXECUTED CONNECTION";
The Linux shell prints this:
TRY CONNECTIONEXECUTED CONNECTION
But the web page prints this:
:TRY CONNECTION
I tried using mysqli but the result is the same
try this
echo "TRY CONNECTION";
$conn =new mysqli($servername, $username, $password,$database);
echo "EXECUTED CONNECTION";
or may be this
$con = mysqli_connect("localhost","my_user","my_password","my_db");
Which MySQL version you use?
define('HOST_NAME', 'localhost');
define('DB_NAME', 'test');
define('DB_USER', 'root');
define('DB_PASS', '');
// DB Connections
$db = mysql_connect(HOST_NAME, DB_USER, DB_PASS);
if($db){
if(mysql_selectdb(DB_NAME, $db)){
//echo 'DB Connected.';
}else{
echo mysql_errno();
}
}else{
echo mysql_error();
}
or used below one
// Create connection
$conn = new mysqli(HOST_NAME, DB_USER, DB_PASS);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
I am having an issue not sure how to get mssql connect to my slim framework with angular js. I have it working with mysql.
part of my helper code
<?php
require_once 'config.php'; // Database setting constants [DB_HOST, DB_NAME, DB_USERNAME, DB_PASSWORD]
class dbHelper {
private $db;
private $err;
function __construct() {
$dsn = 'mssql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8';
try {
$this->db = new PDO($dsn, DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch (PDOException $e) {
$response["status"] = "error";
$response["message"] = 'Connection failed: ' . $e->getMessage();
$response["data"] = null;
//echoResponse(200, $response);
exit;
}
}
here is my config code
<?php
/**
* Database configuration
*/
define('DB_USERNAME', 'puma');
define('DB_PASSWORD', 'puma');
define('DB_HOST', 'WIN-F96PF4GQMCS\SQLEXPRESS');
define('DB_NAME', 'angularcode_products');
?>
I have the following lines Of code for a database connection, however I would just want to use my include("config.php") Instead of this long line of code, how would i go about doing that
try {
$dbh = new PDO('mysql:host=localhost;dbname=lms;charset=UTF-8', 'root', 'password');
}
catch (PDOException $e) {
die('unable to connect to database ' . $e->getMessage());
}
// create LM object, pass in PDO connection
$lm = new lazy_mofo($dbh);
Just make a config.php file, for example consider this file is stored in web root (/var/www/), then the contents of the config.php are like this:
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=lms;charset=UTF-8', 'root', 'password');
}
catch (PDOException $e) {
die('unable to connect to database ' . $e->getMessage());
}
// create LM object, pass in PDO connection
$lm = new lazy_mofo($dbh);
?>
then in your other PHP scripts use the include function:
<?php
include '/var/www/config.php';
...
Now in your PHP scripts you can access the $lm object and $dbh object.
for example include("config.inc.php") and for the config.inc.php:
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=lms;charset=UTF-8', 'root', 'password');
}
catch (PDOException $e) {
die('unable to connect to database ' . $e->getMessage());
}
$lm = new lazy_mofo($dbh);
?>
You can create a db wrapper class in your include file and get really lazy about it =)
define('DB_TYPE', 'mysql');
define('DB_HOST', 'localhost');
define('DB_NAME', 'lms');
define('DB_USER', 'root');
define('DB_PASS', 'password');
class Database{
public function __construct() {
$db = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
$o = new Database();