php mysql_select_db没有选择

时间:2017-05-01 09:05:15

标签: php mysql phpmyadmin

我在选择想要使用的数据库时遇到问题。我可以连接到数据库,但当我尝试选择数据库时,我希望它得到一个错误。据我所知,我做得对。

php code

<?php
$connection = mysql_connect('localhost', '**********', '***********');
if (!$connection) {
    die("DB Connection Faild" . mysqli_error($connection));
}
$select_db = mysql_select_db('web_clients', $connection);
if (!$select_db) {
    die("DB select Faild" . mysqli_error($connection));
}
?>

这是我的PHPmyAdmin

enter image description here

如果有人知道我能做什么让我知道我是新手:)

1 个答案:

答案 0 :(得分:1)

在PHP5中不推荐使用Mysql,而在PHP7中完全删除了mysql使用mysqli(改进了mysql)!

    <?php

    $connection = mysqli_connect('localhost', '****', '****');
    if (!$connection) {
        die("DB Connection Faild" . mysqli_error($connection));
    }
    $select_db = mysqli_select_db($connection, 'yourdbname'); // connection must be first
    if (!$select_db) {
        die("DB select Faild" . mysqli_error($connection));
    }

这是一种与mysqli连接的简洁方法:

<?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();
  }

// Perform a query, check for error
if (!mysqli_query($con,"INSERT INTO Persons (FirstName) VALUES ('Glenn')"))
  {
  echo("Error description: " . mysqli_error($con));
  }

mysqli_close($con);
?>