简体   繁体   English

使用 PHP 和 MySQL 列出记录

[英]List records with PHP and MySQL

I've created a table (notebook) where I keep user's text.我创建了一个表格(笔记本),用于保存用户的文本。 The table is like this one:表是这样的:

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

id = INT(25) A_I NOT NULL id = INT(25) A_I 非空
Username = VARCHAR(65) NOT NULL用户名 = VARCHAR(65) 非空
title = VARCHAR(26) NOT NULL标题 = VARCHAR(26) 非空
html = LONGTEXT NOT NULL html = 长文本非空
public = VARCHAR(11) NOT NULL公共 = VARCHAR(11) 非空
create_date = TIMESTAMP CURRENT_TIMESTAMP NOT NULL create_date = TIMESTAMP CURRENT_TIMESTAMP NOT NULL

So, I've tried to use this code to list all notebook's record for a user:因此,我尝试使用此代码列出用户的所有笔记本记录:

$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
    }
}

The code has been taken from: How to list rows for a query or display 'no records' using a single query and I've edited it for use it with my table.代码取自: 如何使用单个查询列出查询的行或显示“无记录” ,我已经对其进行了编辑,以便与我的表一起使用。

Now, I'm confused, what I must write in "// do whatever you want with the data here" string to list the records?现在,我很困惑,我必须在“// 对这里的数据做任何你想做的事情”字符串中写什么来列出记录?

EDIT: Now, I have this code, but Chrome says "HTTP 500 Error, Internal Server Error":编辑:现在,我有这个代码,但 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>

Where is the error?错误在哪里? The MySQL credentials are provide correctly...正确提供 MySQL 凭据...

Example : you can use echo $row['title'] to print user's note title.示例:您可以使用 echo $row['title']打印用户的笔记标题。

Use var_dump($row) to see more information.使用var_dump($row)查看更多信息。

Form HTML with whatever you want to display, so your while loop would look like, ie:使用您想要显示的任何内容形成 HTML,因此您的while循环将如下所示,即:

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

which would list all items, like它将列出所有项目,例如

1: "Test" by sorge13248

PS: you should always use the same naming convention to avoid problems. PS:您应该始终使用相同的命名约定以避免出现问题。 in your DB all columns are lowercased, except Username ).在您的数据库中,除Username外,所有列都是小写的)。

First of all, the use of mysql_ functions are discouraged since it's gonna be deprecated in the future.首先,不鼓励使用mysql_函数,因为它将来会被弃用。 Consider to change your code to use either MySQLi or PDO API.考虑更改您的代码以使用MySQLiPDO API。

You can read more about in the php manual article: Choosing an API您可以在 php 手册文章中阅读更多信息:选择 API

As stated by the php manual the mysql_fetch_array function:正如php手册中所述mysql_fetch_array函数:

Fetch a result row as an associative array, a numeric array, or both以关联数组、数值数组或两者的形式获取结果行

and then moves to next row until there isn't any other row with results, when the function will return false and your loop is gonna end.然后移动到下一行,直到没有任何其他行有结果,此时函数将返回 false 并且您的循环将结束。

In your particular case, the $row variable would return the following results:在您的特定情况下, $row 变量将返回以下结果:

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

So, for do whatever you want the tutorial meant that you can return your result by accessing your database using the approach above.因此, do whatever you want本教程都意味着您可以通过使用上述方法访问数据库来返回结果。 Let's say for example that you are interested in echoing the create date from a certain user, you could do the following:例如,假设您有兴趣回显某个用户的创建日期,您可以执行以下操作:

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

Wich would output the following to the page the script is generating:将以下内容输出到脚本生成的页面:

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

I hope it helped, cheers.我希望它有帮助,干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM