使用PHP和MySQL列出记录

时间:2012-11-24 13:23:39

标签: php mysql record

我创建了一个表(笔记本),用于保存用户的文本。表格就像这样:

+------+------------+-------+------+--------+------------------------+
|  id  |  Username  | title | html | public |  create_date           |
|--------------------------------------------------------------------|
|   1  | sorge13248 | Test  | ...  |  n     |2012-11-24 9:00:00      |
+------+------------+-------+------+--------+------------------------+

id = INT(25)A_I NOT NULL
用户名= 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: &quot;%s&quot; by %s<br />', $row['id'], 
                 htmlspecialchars($row['title']), 
                 htmlspecialchars($row['Username']) );
}
}
?>
</body>

</html>

错误在哪里? MySQL凭据正确提供......

3 个答案:

答案 0 :(得分:0)

示例:您可以使用echo $row['title']打印用户的音符标题。

使用var_dump($row)查看更多信息。

答案 1 :(得分:0)

使用您想要显示的内容表单HTML,因此您的while循环看起来像是:

while(row = mysql_fetch_array($result)) {
   printf( '%d: &quot;%s&quot; by %s<br />', $row['id'], 
                 htmlspecialchars($row['title']), 
                 htmlspecialchars($row['Username']) );
}

将列出所有项目,例如

1: "Test" by sorge13248

PS:您应该始终使用相同的命名约定来避免问题。在您的数据库中,所有列都是小写的,除了Username)。

答案 2 :(得分:0)

首先,不鼓励使用mysql_函数,因为它将来会被弃用。请考虑更改您的代码,以使用MySQLiPDO API。

您可以在php手册文章中详细了解:Choosing an API

如php手册所述,mysql_fetch_array功能:

  

获取结果行作为关联数组,数字数组或两者

然后移动到下一行,直到没有任何其他行显示结果,当函数返回false并且你的循环结束时。

在您的特定情况下,$ row变量将返回以下结果:

$row['id'] = 1;
$row['Username']= "sorge13248"; 
$row['title']= "Test"; 
$row['html']="... &";
$row['public']="n';
$row['create_date']="2012-11-24 9:00:00";   

因此,对于do whatever you want,教程意味着您可以通过使用上述方法访问数据库来返回结果。例如,假设您有兴趣回复某个用户的创建日期,您可以执行以下操作:

echo "<p>User: ".$row['Username']." was created in: ". $row['create_date']</p>";

将向脚本生成的页面输出以下内容:

<p>User: sorge1348 was created in: 2012-11-24 9:00:00</p>

我希望它有所帮助,欢呼。