没有数据库选择mySQL

时间:2014-11-01 18:16:22

标签: php mysql

我已经开始使用php和mysql进行编码了,我有点困惑,我有mysql服务器设置,这里是我的代码

<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = '';
$db = 'mydatabase';
$connection = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($db);
?>

<?php
include 'connection.php';
$query = "SELECT * FROM mytable";
$result = mysql_query($query);
if ($result === FALSE){
die(mysql_error());
 }
while($mytable = mysql_fetch_array($result)){
echo "<h3>" . $mytable['Name'] . "</h3>";
}
 ?>

我收到错误“没有选择数据库。”,代码有什么问题? d:

2 个答案:

答案 0 :(得分:0)

尝试:

echo mysql_error($connection);

在mysql_connect()行之后,一个在mysql_db_select行之后()

你可能有某种错误。

答案 1 :(得分:0)

我不会在您的代码中看到任何错误。请尝试以下代码,看看错误是否仍然存在。

connection.php:

 <?php
    $connection = mysqli_connect('localhost', 'user', '', 'mydatabase');
    ?>

 <?php
    include 'connection.php';
    $query = "SELECT * FROM mytable";
    $result = mysqli_query($connection,$query);
    if ($result === FALSE){
    die(mysqli_error());
     }
    while($mytable = mysqli_fetch_array($connection,$result)){
    echo "<h3>" . $mytable['Name'] . "</h3>";
    }
     ?>