我已经开始使用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:
答案 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>";
}
?>