Why php does not connect to mysql, here’s the code:

$db_hostname = 'localhost'; $db_database = 'publications'; $db_username = 'root'; $db_password = 'pass'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Невозможно подключиться к MySQL: " . mysql_error()); mysql_select_db($db_database) or die("Невозможно выбрать базу данных: " . mysql_error()); 

It does not give an error, but it is impossible to choose anything. For example, if you change the value of $db_username to root2 , then nothing will change

  • what error gives? - XenK
  • The point is that there is no error, but there is no connection either. I figured that if you connect with incorrect data, the error will be displayed on the browser page - Misha Spring
  • @MishaSpring, mysql_ functions mysql_ deprecated since PHP 5.5.0. - Visman
  • Yes, use better mysqli_ - Maxim Drobyshev
  • It can not be that you have disabled error messages in the browser? - cheops

1 answer 1

Try the following.

 $mysqli = new mysqli("localhost", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Не удалось подключиться к MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } echo $mysqli->host_info . "\n"; $mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306); if ($mysqli->connect_errno) { echo "Не удалось подключиться к MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } echo $mysqli->host_info . "\n";