简体   繁体   中英

cannot connect to database using mysql

I have a question that need all of you to help me. I got stuck with it several days. My problem is related to the connection to database using php. Both Mysqli and Mysql are enabled in phpmyadmin. I can connect to database through Mysqli, but i cannot connect through Mysql.

Here is code

Mysqli

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxxx');
define('DB_DATABASE', 'sample_db');
define('PORT', '80');
define('SOCKET', '/var/lib/mysql/mysql.sock');
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE, PORT, SOCKET) or die("Error " . mysqli_error($connection));
mysqli_set_charset($connection, "utf8");
?>

Mysql

<?php
define('DB_SERVER', 'localhost:/var/lib/mysql/mysql.sock');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxxx');
define('DB_DATABASE', 'sample_db');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die("Error " . mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
mysql_set_charset($connection, "utf8");
?>

Can you help me to solve this problem?

Thank in advance!

which type of error you see??? try to use instead of

define('DB_SERVER', 'localhost');

define('DB_SERVER', 'ipOfTheHost');

// data db
$username = "username";
$pass = "pwd";
$database = "nameDB";
$host = "ipServer"; //my is 62.149.150.187

// connection
mysql_connect($host, $username, $pass);
@mysql_select_db($database) or die("Argh... it's impossible to connect to it");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM