我已经开始用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:
我在您的代码中没有看到任何错误。尝试以下代码,看看错误是否仍然存在。
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>";
}
?>
尝试:
echo mysql_error($connection);
就在mysql_connect()行之后,在mysql_db_select行()之后
你可能有某种错误。