我创建了一个表格(笔记本),用于保存用户的文本。表是这样的:
+------+------------+-------+------+--------+------------------------+
| id | Username | title | html | public | create_date |
|--------------------------------------------------------------------|
| 1 | sorge13248 | Test | ... | n |2012-11-24 9:00:00 |
+------+------------+-------+------+--------+------------------------+
id = INT(25) A_I NOT NULL
Username = VARCHAR(65) NOT NULL
title = VARCHAR(26) NOT NULL
html = LONGTEXT NOT NULL
public = VARCHAR(11) NOT NULL
create_date = TIMESTAMP CURRENT_TIMESTAMP NOT NULL
所以,我试过了使用此代码为用户列出所有笔记本的记录:
$link = mysql_connect("localhost", "mysql_user", "password");
mysql_select_db("notebook", $link);
$result = mysql_query("SELECT * FROM notebook WHERE Username = '"$username."'", $link);
if(0 == mysql_num_rows($result)) {
echo 'No notebook was created for this user';
}
else {
while(row = mysql_fetch_array($result)) {
// do whatever you want with the data here
}
}
代码取自:How to list rows for a query or display 'no records' using a single query,我已经对其进行了编辑,以便与我的表一起使用。
现在,我很困惑,我必须在“//对这里的数据做任何你想做的事情”字符串中写什么来列出记录?
编辑:现在,我有这段代码,但 Chrome 显示“HTTP 500 错误,内部服务器错误”:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Notebook</title>
</head>
<body>
<h1>Notebook</h1><br/>
<?=
$link = mysql_connect("localhost", "database_username", "password");
mysql_select_db("database_name", $link);
$result = mysql_query("SELECT * FROM notebook", $link);
// If if result set contains rows
if(0 == mysql_num_rows($result)) {
echo 'No notebook was created for this user';
}
else {
while(row = mysql_fetch_array($result)) {
printf( '%d: "%s" by %s<br />', $row['id'],
htmlspecialchars($row['title']),
htmlspecialchars($row['Username']) );
}
}
?>
</body>
</html>
错误在哪里?正确提供了 MySQL 凭据...