简体   繁体   中英

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

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":

<!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...

Example : you can use echo $row['title'] to print user's note title.

Use var_dump($row) to see more information.

Form HTML with whatever you want to display, so your while loop would look like, ie:

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. in your DB all columns are lowercased, except Username ).

First of all, the use of mysql_ functions are discouraged since it's gonna be deprecated in the future. Consider to change your code to use either MySQLi or PDO API.

You can read more about in the php manual article: Choosing an API

As stated by the php manual the mysql_fetch_array function:

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.

In your particular case, the $row variable would return the following results:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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