pls i tried to display all content in a database, it keeps dispaying the last inserted, kindy help out.
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts ");
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
?>
<?php
}
?>
<?php
echo $id;
echo $body;
?>
Either print the values in while loop or store them in an array to print them outside the loop.
while($row = mysql_fetch_assoc($query)){
echo $row["id"];
echo $row["body"];
}
condb.php
<?php
$con = mysqli_connect("localhost","username","password","databasename");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
index.php
<?php
include 'database/condb.php';
$query = "SELECT * FROM posts";
$res = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($res)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
echo $id;
echo $body;
}
?>
For storing data, sometimes using array becomes helpful. I usually recommend it:
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts");
$array = [];
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags = $row["hashtags"];
$array[$id] = ['username'=>$username, 'body'=>$body, 'date_added'=>$date_added, 'hasttags'=>$hasttags];
}
?>
<?php
foreach($array as $id => $value){
echo $id; //Prints id
echo "<br>";
echo $value['username'].", ".$value['body'].", ".$value['date_added'].", ".$value['hasttags'];
}
?>
Or you can simply do this:
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts");
$array = [];
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags = $row["hashtags"];
?>
<?php
echo $id.", ".$username.", ".$body.", ".$date_added.", ".$hasttags;
}
?>
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts ");
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
echo $id;
echo "<br>";
echo $body;
?>
<?php
}
?>
As an answer to the question "pls i tried to display all content in a database, it keeps dispaying the last inserted, kindy help out."
Put echo $id, echo $body be inside the loop.
Related
I have tried but i could not just get where i am doing it wrong. The table only echo the first row continuously and did not echo the other rows. Kindly help please.
$run = "
SELECT * FROM staff
";
$runquery = mysqli_query($connection, $run);
$runrow = mysqli_num_rows($runquery);
if($runrow < 1){
echo "<p class='errormsg'>You do not have any Staff</p>";
}
else{
$row = mysqli_fetch_array($runquery);
if($row) {
$surname = $row['surname'];
$lastname = $row['lastname'];
$phone = $row['phone'];
$username = $row['username'];
$role = $row['auth'];
}
foreach ($row as $staff) {
$table .= "
<tr>
<td>$surname</td>
<td>$phone</td>
<td>$username</td>
<td>$role</td>
</tr>
";
}
}
You need to change loop like this. also the tabular view of your record require <table> body.
$run = "SELECT * FROM staff";
$runquery = mysqli_query($connection, $run);
$runrow = mysqli_num_rows($runquery);
if( $runrow < 1 ) {
echo "<p class='errormsg'>You do not have any Staff</p>";
}
else {
$table = ""; // create table here
while($row = mysqli_fetch_array($runquery)) {
$surname = $row['surname'];
$lastname = $row['lastname'];
$phone = $row['phone'];
$username = $row['username'];
$role = $row['auth'];
$table .= "<tr>
<td>$surname</td>
<td>$phone</td>
<td>$username</td>
<td>$role</td>
</tr>";
}
echo $table;
}
did you try to move the following code part into the foreach-loop?
$run = "
SELECT * FROM staff
";
$runquery = mysqli_query($connection, $run);
$runrow = mysqli_num_rows($runquery);
if($runrow < 1){
echo "<p class='errormsg'>You do not have any Staff</p>";
}
else{
$row = mysqli_fetch_array($runquery);
foreach ($row as $staff) {
$surname = $staff['surname'];
$lastname = $staff['lastname'];
$phone = $staff['phone'];
$username = $staff['username'];
$role = $staff['auth'];
$table .= "
<tr>
<td>$surname</td>
<td>$phone</td>
<td>$username</td>
<td>$role</td>
</tr>
";
}
}
I want to achieve this output in php.
[
{
"id": 1388534400000,
"author": "Pete Hunt",
"text": "Hey there!"
},
{
"id": 1420070400000,
"author": "Paul O’Shannessy",
"text": "React is *great*!"
}
]
I have a while loop in my backend below.
$pull = "SELECT * FROM mydb";
$result = $con->query($pull);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json['id'] = $row['id'];
$json['author'] = $row['author'];
$json['text'] = $row['text'];
}
echo json_encode($json);
It only returns the last row in the database, and I want to display them all.
Thank you.
If your $row contains only these three fields from database then use below code otherwise #govindkr13 answer
$pull = "SELECT * FROM mydb";
$result = $con->query($pull);
$json = [];
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[] = $row;
}
echo json_encode($json);
You are simply overwriting your $json array every time. Try this:
$i=0;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$i]['id'] = $row['id'];
$json[$i]['author'] = $row['author'];
$json[$i]['text'] = $row['text'];
$i++;
}
echo json_encode($json);
use this
$pull = "SELECT * FROM mydb";
$result = $con->query($pull);
$final = [];
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json['id'] = $row['id'];
$json['author'] = $row['author'];
$json['text'] = $row['text'];
$final[] = $json;
}
echo json_encode($final);
Maybe this can help.
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$row['id']]['author'] = $row['author'];
$json[$row['id']]['text'] = $row['text'];
}
$i=0;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$i]['id'] = $row['id'];
$json[$i]['author'] = $row['author'];
$json[$i]['text'] = $row['text'];
$i++;
}
echo json_encode($json);
try this
$count=0;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$count]['id'] = $row['id'];
$json[$count]['author'] = $row['author'];
$json[$count]['text'] = $row['text'];
$count++;
}
echo json_encode($json);
I am working on a directory where some of the listings have a images associated with them and others do not. I am wondering how I can write a loop within a loop to get my results.
Example, User selects state they want results from, query goes to DB requesting all listings in that state.
<?php
if (isset($_POST['searchButton'])) {
$state = $_POST['state'];
$query = "SELECT * FROM directory LEFT JOIN directory_images ON directory.id = directory_images.user_id WHERE directory.state = '$state' ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) == 0) {
echo "<p>Sorry, there are no listings in '$state', check back soon!</p>\n";
}
else
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['id'];
$name = $row['name'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$has_support_pics = $row['file_name'];
?>
<h4><?php echo $name ?></h4>
<p><?php echo $address ?><br/>
<?php echo $city . ' ' . $state . ', ' . $zip; ?><br/>
</p>
<?php
// check to see if ID has extra images
if (isset($has_support_pics)) {
$query2 = "SELECT file_name FROM directory_images WHERE user_id = '$id'";
$result2 = mysql_query($query2) or die(mysql_error());
echo $query2.'<br/>';
?>
<ul class="support_images">
<?php
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
$support_image = $row['file_name'];
echo $support_image.'<br/>';
}
?>
</ul>
</div>
<br/>
</div>
<?php
}
echo "<hr/>";
}
}
?>
Do NOT run queries in loops - use a join.
Here is a tutorial: http://thewebmason.com/tutorial-parent-child-lists/
When I run this query, it works fine but the individual fields would pull out. Like ThumbFilePath and Title.
If I run the query with only one field like:
$result = mysql_query("select ThumbFilePath from artwork where SCID = $SCID") or die(mysql_error());
It works fine. Any ideas why I can't pull the other fields?
<?php
$dbname = 'pdartist2';
$table = 'artowrk';
// query
$result = mysql_query("select AID, ThumbFilePath, Title, DisplayOrder from artwork where SCID = $SCID") or die(mysql_error());
while($row = mysql_fetch_row($result))
{
foreach($row as $cell)
{
echo "<div id='thumb_container'>";
echo "
<a href='gallery_detail.php?AID=$AID'>
<img src='http://markdinwiddie.com/PHP2012/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'>
</a>
";
echo "$Title";
echo "</div>";
}
}
mysql_free_result($result);
?>
In general, you'll ALWAYS need to dereference the columns you need. For example:
while($row = mysql_fetch_row($result)) {
$aid = $row['AID'];
$tpath= $row['ThumbFilePath'];
$title = $row['title'];
...
<?php
$dbname = 'pdartist2';
$table = 'artowrk';
// query
$sql = "select AID, ThumbFilePath, Title, DisplayOrder from artwork where SCID = $SCID";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_row($result)) {
$AID = $row['AID'];
$ThumbFilePath= $row['ThumbFilePath'];
$Title = $row['Title'];
$DisplayOrder = $row['DisplayOrder'];
echo "<div id='clear'></div>";
echo "<div id='thumb_container'>";
echo "<a href='gallery_detail.php?AID=$AID'><img src='http://markdinwiddie.com/PHP2012/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'></a>";
echo "<div id='name_spacer'></div>";
echo "<div id='thumbdesc'>";
echo "$Title";
echo "</div>";
echo "</div>";
}
mysql_free_result($result);
?>
So I have some numbers that take the name "id" in my MySQL database. I tried this code:
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$user = $row['usrname'];
$fname = $row['fname'];
$lname = $row['lname'];
echo "<strong>User:</strong> ".$user." ".$fname." ".$lname."<br/>";
echo min($id);
}
On the last line I tried to echo the min value for id, but it didn't work. Any suggestions?
ANSWER: Use "ASC LIMIT 1"
while($row = mysql_fetch_assoc($sql)){
$id=$row['id'];
$user = $row['usrname'];
$fname = $row['fname'];
$lname = $row['lname'];
echo "<strong>User:</strong> ".$user." ".$fname." ".$lname."<br/>";
echo $id;
}
$sql = mysql_query("SELECT * FROM users ORDER BY id ASC LIMIT 1");
while($row = mysql_fetch_assoc($sql))
{
$id=$row['id'];
$user = $row['usrname'];
$fname = $row['fname'];
$lname = $row['lname'];
echo "<strong>User:</strong> ".$user." ".$fname." ".$lname."<br/>";
echo $id;
}
Try this:
$sql = 'SELECT min(id) as min FROM users';
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$min_id = $row['min'];
In case you cannot change the query order (sort by names?), you can determine the lowest $id by using a helper variable:
$min = isset($min) ? min($min, $id) : $id;
And then print $min; after your loop.
If not able to use MIN() in MySQL, try this...
$smallestId = -PHP_INT_MAX;
while($row = mysql_fetch_assoc($sql)) {
...
$smallestId = min($smallestId, $id);
}
If you never have any negative ids (you shouldn't), you could just change -PHP_INT_MAX to 0.