Looking to display a value from the 1st record's 'post' field which is stored in a MySQL database using PHP. The following div is where the value is to be inserted:
<div id="insert1"><?php echo($r); ?></div>
The PHP (Note using MeekroDB):
<?php
require_once 'meekrodb.2.2.class.php';
DB::$user = 'username';
DB::$password = 'password';
DB::$dbName = 'database';
DB::$host = 'hostname';
// get all entries from database table Microblog;
$results = DB::query("SELECT post FROM MicroBlog");
foreach ($results as $row) {
$id = $row['id'];
// loop through each entry - there's 3
for($i = 0; $i < 3; $i++) {
// if it's the 1st entry (id = 1) then get field 'post'
if ($id == 1) {
$r = $row['post'];
}
}
}
?>
Currently #insert1 is blank. What do I need to change to get it into the div? Please note, I want to keep the for loop, as I'll be adding other if's once I get it running. Thanks.
Probably this part is wrong:
// get all entries from database table Microblog;
$results = DB::query("SELECT post FROM MicroBlog");
should be:
// get all entries from database table Microblog;
$results = DB::query("SELECT * FROM MicroBlog");
You're getting id which i think you're not getting from your query.
$id = $row['id'];
Other option:
Comment out $id.
//$id = $row['id'];
and in the if block below, change $id to $i:
if ($i == 1) {
$r = $row['post'];
}
To add number of records:
$x = 0;
foreach ($results as $row) {
$x++;
$id = $row['id'];
// loop through each entry - there's 3
for($i = 0; $i < 3; $i++) {
// if it's the 1st entry (id = 1) then get field 'post'
if ($id == 1) {
$r = $row['post'];
}
}
}
echo $x;
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.