<?php
require_once('db.php');
function getLanguage() {
global $db;
global $conn;
$sql = "SELECT * FROM books.languages ORDER BY name ASC";
$db = mysql_connect($hostname, $username, $password);
$rs = mysql_query($sql, $db) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$tot_rows = mysql_num_rows($rs);
if($tot_rows > 0){
?>
答案 0 :(得分:0)
问题是没有选择数据库(我没有在你的问题中看到这个?)
您对mysql_select_db ( string $database_name [, resource $link_identifier = NULL ] )
的电话在哪里?
应该在connect
之后。
$db = mysql_connect($hostname, $username, $password);
mysql_select_db("your_database_name");
$rs = mysql_query($sql, $db) or die(mysql_error());
(显然你也应该检查错误......)
答案 1 :(得分:0)
也许以下内容会有所帮助?我正在使用MySQLi,因为MySQL已被弃用。
db.php中
<?php
// CONNECT TO THE DATABASE
$DB_NAME = 'database';
$DB_HOST = 'host';
$DB_USER = 'username';
$DB_PASS = 'password';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
然后使用以下内容查询您的数据库。
<?php
require_once('db.php');
$sql = "SELECT * FROM tablename";
$rs = $mysqli->query($sql) or die($mysqli->error.__LINE__);
if($rs->num_rows > 0) {
while($row = $rs->fetch_assoc()) {
//Do Something
}
}
else {
echo 'NO RESULTS';
}
mysqli_close($mysqli);
?>