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: "%s" by %s<br />', $row['id'],
htmlspecialchars($row['title']),
htmlspecialchars($row['Username']) );
}
}
?>
</body>
</html>
Where is the error? The MySQL credentials are provide correctly…
Form HTML with whatever you want to display, so your
whileloop would look like, i.e.:which would list all items, like
PS: you should always use the same naming convention to avoid problems. in your DB all columns are lowercased, except
Username).