Cannot connect to MYSQL using PHP - php

I could not connect to the local host MYSQL database using my simple php code and DOES NOT display anyything. THis is applicable to all users and root.Eventhough the problem is reported many times, i could not fix it. Also it is written in one site that it may have to make changes in php file.Please give me solution for this connection error.
I use php 5.5.9-1 in Ubuntu. MYSQL version is 5.5.38.
My code is
<?php
$servername = "localhost";
$username = "root";
$password = "*****";
$conn = mysql_connect($servername, $username, $password);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Call to undefined function mysql_connect()
This error message from your comments tells me that you do not have the MySQL extension installed.
Execute this in your terminal to install: sudo apt-get install php5-mysql
Then restart Apache: sudo /etc/init.d/apache2 restart
Please be aware that the "mysql_" functions are marked as deprecated and your script might fail with a future PHP update. Please use "mysqli_" or PDO.

Mysql is deprecated. Use Mysqli.
Create a file and name it as db_connect.php and copy the below code.
<?php
$connection = mysqli_connect("localhost","username","password","database_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Open browser and type http://localhost/db_connect.php. If you see a blank page, your connection is fine. If there's a problem, it will show you the error.

Related

Error when connecting from a web server to a remote MySQL DB Server

I have been struggling to connect from a Ubuntu version 16.04 web server
(running on 1 AWS instance, with PHP 7.0.33 and Apache 2.7.18)
communicating with a data base server using MySQL 8.0.19 running on another AWS instance (running Ubuntu version 16.04).
I set up the data base server and configured it as follows:
I edit the file /etc/mysql/mysql.conf.d/mysqld.cnf
I add the following in the [mysqld] section
[mysqld]
require_secure_transport = on
I save the file and from the console issue:
sudo mysql_ssl_rsa_setup --uid=mysql
I then restart mysql with:
sudo systemctl restart mysql
I have created a data base and tables on this data base server.
I also created a remote user on this data base server as follows (IP address and user are fictitious):
CREATE USER 'Master'#'1.26.4.44' IDENTIFIED BY 'admin';
GRANT ALL PRIVILEGES ON sample_data_base.* TO ' Master'#'1.26.4.44';
FLUSH PRIVILEGES;
Exit
I successfully test the connectivity to this db server from the web server client instance:
mysql -u Master -p -h '1.26.4.44'
However when I use the following PHP code snippet from the web server client instance
to open the connection to the data base server, I get an error (see below):
// set up for remote DB access
$dbhost = '1.26.4.44'; // Unlikely to require changing
$dbname = 'sample_data_base'; // Modify these...
$dbuser = 'Master'; // ...variables according
$dbpass = "admin"; // ...to your installation
$appname = "test_app"; // ...and preference
$dbconnection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if ( !$dbconnection)
{
echo "connection failed ";
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
The error is:
Connect Error (3159) Connections using insecure transport are prohibited while --require_secure_transport=ON
Just as an FYI, when I comment out the line on the data base server in the
file /etc/mysql/mysql.conf.d/mysqld.cnf
# require_secure_transport = on
There are no issues with the php code connection to the remote data base server.
What am I missing on the web server client side to let me connect to the remote db server from the php code?
You need extra configurations in your db setup:
$db = mysqli_init();
mysqli_options ($db, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$db->ssl_set(
'/etc/mysql/ssl/client-key.pem',
'/etc/mysql/ssl/client-cert.pem',
'/etc/mysql/ssl/ca-cert.pem',
NULL,
NULL);
$link = mysqli_real_connect(
$db, 'ip', 'user', 'password', 'db', 3306, NULL, MYSQLI_CLIENT_SSL);
if (!$link)
{
die ('Connect error (' . mysqli_connect_errno() . '): ' . mysqli_connect_error() . "\n");
} else {
// your queries should be here
$db->close();
}
Hope this can help.

PHP not able to connect to MySQL in Ubuntu based custom S2I image

Playing with Openshift origin 3.9 on my custom servers. So far it has been a pleasant experience. I've been building a custom s2i image based on Ubuntu for my LEMP stack.
I'm not able to connect to the MySQL database. I always get an error saying:
Failed to connect to MySQL: (2002) No such file or directory
Here's my PHP code:
$mysql_database = getenv("MYSQL_DATABASE");
$mysql_server_name =getenv("MYSQL_HOST");
$mysql_username = getenv("MYSQL_USER");
$mysql_password = getenv("MYSQL_PASSWORD");
$mysql_port = getenv("MYSQL_PORT");
$mysqli = new mysqli($mysql_server_name, $mysql_username, $mysql_password, $mysql_database, $mysql_port);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
die();
}
Some observations:
I'm able to connect from MySQL CLI client from inside the pod.
The same app/code works fine with the official openshift PHP s2i image.
Am I missing anything in my s2i?
Check your phpinfo() and search for MySQLi extension. If you can't find, that means you do not have the extension installed yet and it needs to install.
try installing from terminal
(Ubuntu/Mint) $ sudo apt-get install php(version)-mysql
replace (version) with any of your php version number. For example, php7.0 / php7.1, etc.
To check phpinfo(), create a phpinfo.php file which you can access from url with the content
<?php
phpinfo()
If you have everything and still unable to connect? Try checking your mysql.sock file path.
Open the php.ini file and find this line:
mysql.default_socket
And make it (know where your mysql.sock located)
mysql.default_socket = /path/to/mysql.sock

Could not connect to the database mytable: could not find driver

I had the php version 5 but now i upgraded to php 7 and i am getting problems that a didn't had with the older version.
One that i'm still trying to solve is this:
Could not connect to the database mydatabase :could not find driver (this appear when i run my script).
I tryed to open phpmyadmin and also appeared an error:
The mysqli extension is missing. Please check your PHP configuration.
After a lot of search i still can find a solution, is this related to the version of my mysql on xampp? It need to be upgraded too?
Apache version: 2.4
mysqlnd 5.0.12
********************** EDIT*****************
Here is the code that makes the connection to database:
function connection()
{
$host = 'localhost';
$dbname = 'mydatabase';
$username = 'root';
$password = '';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname;", $username, $password);
$conn->exec("SET CHARACTER SET utf8");
// echo "Connected to $dbname at $host successfully.";
}
catch(PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
echo "Line: " . __LINE__;
}
return $conn;
} //connection
Either install the mysqli driver on your server or use PDO, if that's available. Use this code to find out more information about the installed drivers:
<?php phpinfo() ?>
Go to your php.ini file and uncomment this line
extension=mysqli
Then restart your local server
This was how i solved the problem:
1º rename php.ini-developer to php.ini
2º add extensions:
extension_dir = "D:\Programs\xampp\php\ext"
extension=php_mysqli.dll
extension=pdo_mysql
extension=mbstring
extension=php_mbstring.dll
Working like a charm now!

Connection failed: could not find driver, PHP7.1 SqlServer, Windows Server 2012R2, IIS

I'm attempting to get PHP 7.1 to connect to an instance of SQL Server on the same network, I get the following error:
Connection failed: could not find driver
I've downloaded and added both php_pdo_sqlsrv.dll and php_sqlsrv.dll (both nts versions). I have tried both the most recent release of, AND, the pre-release versions that supposedly fix an error with the .dll files not working with PHP 7.1.
Both files have been added in our php.ini file as well.
PHP file that should connect to the DB..
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$serverName = "######";
// $myUser = "#####";
// $myPass = "######^";
// $dbName = "######";
//connection to the database
try {
$conn = new PDO("sqlsrv:Server=($serverName,53000);port=1433;Database=$dbName", NULL, NULL);
// 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();
}
$conn = null;
?>
The PHP.ini section currently looks like this:
extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
;extension=php_pdo_sqlite.dll
;extension=php_pgsql.dll
;extension=php_shmop.dll
;extension=php_sqlsrv_71_nts.dll
;extension=php_pdo_sqlsrv_71_nts.dll
extension=php_pdo_sqlsrv.dll
;extension=php_pdo_sqlsrv_54_nts.dll
;extension=php_sqlsrv_53_nts.dll
extension=php_sqlsrv.dll
php -info only shows odbc under "PDO drivers" - which I believe indicates that it isn't activating.
Have restarted the server multiple times.
Any help is much appreciated, thanks!

PHP script can't run once i add `mysql_connect()`

i have installed Linux Mint 18.1, then LAMP server using the command
sudo apt-get install lamp-server^ , for testing i tried to create a script connecting to MYSQL database, just like the following.
<?php
$h = "localhost";
$u = "root";
$p = "password";
$conn = mysql_connect($h,$u,$p);
echo "test";
?>
when i remove the line $conn = mysql_connect($h,$u,$p);
the script works fine, otherwise it isn't running,
php5.6-mysql is already installed , and i also tried to connect using
mysqli_connect() instead of mysql_connect()
i don't know what's wrong ?!
Paste this at the top of your file:
error_reporting(E_ALL);
ini_set('display_errors', 1);
It will help you to see the exact error.
Alternatively, you can try:
$conn = mysql_connect($h,$u,$p) or die('Connection Error');
If connection is not getting established then it will show Connection Error

Categories