I use Xampp as server. I have mysql table that i visualize with bootstrap grid. As the table have more than 1000 rows, the visualization with bootstrap is slow. I'm trying to work server side but have a problem connecting to the data base with this code
$dbDetails = array(
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'db' => 'company');
It can't contact the server or connect to the database, while with this is possible. I receive the following errors: DataTables warning: table id=memListTable - Invalid JSON response AND {"error":"An error occurred while connecting to the database. The error reported by the server was: SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)"}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "company";
Related
This question already has answers here:
How to pass database connection instance between PHP files without redeclaring it
(3 answers)
How to use mysqli query using a separate connection.php file? [closed]
(2 answers)
Closed 3 years ago.
I am working on a project in php. where I have multiple files having data connection.And on the start of every file I write this code.
$servername = "localhost";
$abcusername = "root";
$abcpassword = "password";
$dbName = "db";
When I have to change password , I edit every file and change password that is very hassle process for me.
Can someone help me that how to to store server information in one file and give the link in all other files.
Create db.php file and place
$servername = "localhost";
$abcusername = "root";
$abcpassword = "password";
$dbName = "db";
$con = mysqli_connect($servername, $abcusername, $abcpassword, $dbName) or trigger_error("Unable to connect to the database");
and just use
include 'db.php';
in all of your files and use $con for posting your data to server eg:
mysqli_query($con,"your sql command");
You can simulate a rudimentary environment file by simply having an array with various information.
Lets assume we have a file called env.php:
$connections = [
'connection_1' => [
'driver' => 'mysql',
'username' => 'some username',
'password' => 'some password',
'servername' => 'some servername',
'db_name' => 'some db_name',
'port' => 123456,
],
'connection_2' => [
// ....
],
'connection_2' => [
// ....
]
];
After which you can require_once the file in various places.
With that said I recommend you take a look on how different frameworks handle environmental variables(symfony example here).
I don't expect you to implement the symfony example but it would give you a good idea on how to implement your own.
I've installed Moodle with Postgres. The db configuration looks like (yeah, I did it in a bit hackish way):
$CFG->dbtype = 'pgsql'; // 'pgsql', 'mariadb', 'mysqli', 'mssql', 'sqlsrv' or 'oci'
$CFG->dblibrary = 'native'; // 'native' only at the moment
$CFG->dbhost = "my-moodle-db.net' connect_timeout=10 sslmode=verify-full sslrootcert='/path/to/cas/allCAs.pem"; // eg 'localhost' or 'db.isp.com' or IP
$CFG->dbname = $pwd_name; // database name, eg moodle
$CFG->dbuser = 'moodle'; // your database username
$CFG->dbpass = $pwd_pwd; // your database password
$CFG->prefix = 'mdl_'; // prefix to use for all table names
$CFG->dboptions = array(
'dbpersist' => true, // should persistent database connections be
...
My installation works almost fine but once in roughly 10 requests I'm getting the following error:
Error: Database connection failed
And the page is rendered immediately though I set connection timeout to 10 above.
Is there any other place to fix connection timeouts in PHP?
Can I make moodle somehow retry the connection before returning the error to me?
I have a WAMP installation with Windows Server 2012 R2 Datacenter Edition, Apache 2.4.23, PHP 7.0.10 (mysqlnd 5.0.12-dev - 20150407), MySQL 5.7.14.
I configured a connection with:
$host = 'wrong-server';
$dbase = 'db_name';
$user = 'my_user';
$pwd = 'my_pwd';
$connection='mysql: host='.$host.'; dbname='.$dbase;
$link = new PDO($connection , $user, $pwd);
where wrong-server doesn't exsist (it is for a test for catching errors).
When I execute the code the connection works using localhost as host (I know it because a query to db_name retrive datas).
I searched for some default behaviour or configuration without succes.
Some ideas?
Code update:
<?php
$host = '192.168.123.123'; //'localhost';
$dbase = 'db_name';
$user = 'my_user';
$pwd = 'my_pwd';
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
$connection='mysql: host='.$host.'; dbname='.$dbase;
try {
$link = new PDO($connection, $user, $pwd, $pdo_options);
$_SESSION['DB_INFO']=array( 'db'=>$link->getAttribute(PDO::ATTR_DRIVER_NAME),
'ver'=>$link->getAttribute(PDO::ATTR_SERVER_VERSION),
'cli'=>$link->getAttribute(PDO::ATTR_CLIENT_VERSION ),
'srv'=>$link->getAttribute(PDO::ATTR_SERVER_INFO ),
);
print_r($_SESSION['DB_INFO']);
}
catch(\PDOException $err) {
echo 'Error: '.$err->getMessage();
};
Server 192.168.123.123 doesn't exsist but I have these response:
Array ( [db] => mysql [ver] => 5.7.14 [cli] => mysqlnd 5.0.12-dev - 20150407 - $Id: 241ae00989d1995ffcbbf63d579943635faf9972 $ [srv] => Uptime: 20111 Threads: 1 Questions: 354 Slow queries: 0 Opens: 125 Flush tables: 1 Open tables: 118 Queries per second avg: 0.017 )
But if I set $pwd='wrong_pwd' the answer is:
Error: SQLSTATE[HY000] [1045] Access denied for user 'my_user'#'localhost' (using password: YES)
SOLUTION
As #MichaelBerkowski states, eliminating spaces in the DSN
mysql:host='.$host.';dbname='.$dbase.'
instead of
mysql: host='.$host.'; dbname='.$dbase.'
solves the problem, so an error came out.
Config file:
$db=array(
'host' => 'localhost',
'user' => 'root',
'pass' => 'secret',
'name' => 'jack',
);
Index file
$mysql = new mysqli($db['host'],$db['user'],$db['pass'],$db['name']);
$mysql->set_charset('utf8mb4');
if($mysql->connect_errno)
{
die('Database connection could not be established. Error number: '.$mysql->connect_errno);
}
I get:
Database connection could not be established. Error number: 1045
Please guys, tell me what to do and I will respond you with the answer in 30 seconds!
UPDATE:
Okay so if i make change line to
$mysql = new mysqli(localhost,root,secret,jack);
it works, but how do I get it work with the tags?
Error No: 1045 refer to as Access denied for user 'root'#'localhost' (Using password: NO). so check you password and username for the database. Try passing the password as empty.
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 4 years ago.
Attempting to connect to localhost sql db using the following code (Not doing anything with query at this point just want the query to execute):
<?php
$host = "localhost";
$port = 3306;
$socket = "";
$user = "root";
$password = "Password1";
$dbname = "CIIP_WIKI";
$con = new mysqli($host, $user, $password, $dbname, $port);
if(!$con)
{
echo ("db connection failed!");
die ('Could not connect to the database server' . mysqli_connect_error());
}
else {
echo ("db connection established.");
echo ("<br/>");
}
$query = sprintf("SELECT first_name FROM actor WHERE actor_id='1'");
$result = mysql_query($query);
$con->close();
?>
I keep getting the following...
Welcome
db connection established.
Warning: mysql_query(): Access denied for user ''#'localhost' (using password: NO) in C:\Program Files (x86)\EasyPHP-12.1\www\Cisco Wiki\index.php on line 31
Warning: mysql_query(): A link to the server could not be established in C:\Program Files (x86)\EasyPHP-12.1\www\Cisco Wiki\index.php on line 31
Why does this not work?
This is because you create a connection using mysqli_ and then use mysql_ to try to fetch your result. They are different API's.
<?php
/* You should enable error reporting for mysqli before attempting to make a connection */
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
/* Set the desired charset after establishing a connection */
mysqli_set_charset($mysqli, 'utf8mb4');
printf("Success... %s\n", mysqli_get_host_info($mysqli));
Example taken from the PHP manual
This warning will be logged anytime you try to execute a query without a valid connection object being available.
In your case you thought you had a valid connection object available, but you created it using mysqli while your query is being executed using mysql. Those are two different APIs and so the mysql_query function was looking for a mysql connection object and didn't find it.