I am trying to show user data when clicking link depending on a story ID. However, the page is showing the same data from one row in the database every time (web link is showing a different ID 'php?story_id=10' etc. but it's still showing the same data.
I have this function:
function displayStories(){
include('conn/conn.php');
$queryread = "SELECT Users.ID, Users.FirstName, Stories.Age, Stories.Story,
Stories.Image FROM `Stories` INNER JOIN `Users` ON Users.ID = Stories.User_ID";
$result = mysqli_query($conn, $queryread) or die(mysqli_error($conn));
if(mysqli_num_rows($result) > 0 ){
while($row = mysqli_fetch_assoc($result)){
$name = $row["FirstName"];
$idData = $row["ID"];
$age = $row["Age"];
$story = $row["Story"];
$limit = mb_strimwidth($story, 0, 300, "...");
echo
"<div class='stories'>
<a href='storyDis.php?story_id=$idData'>
<h5><b>$name</b></h5></a>
<p><b>$age years old</b></p>
<p>$limit</p>
</div>
";
}
}
mysqli_close($conn);
}
And this to query to display the data(variables called in HTML):
include('conn/conn.php');
$str = htmlentities($_GET["story_id"]);
$query = "SELECT * "
. "FROM Stories "
. "WHERE ID='$str'";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
$queryread = "SELECT Users.FirstName, Stories.Age, Stories.Story,
Stories.Image FROM `Stories` INNER JOIN `Users` ON Users.ID = Stories.User_ID";
$result1 = mysqli_query($conn, $queryread) or die(mysqli_error($conn));
if(mysqli_num_rows($result1) > 0 ){
while($row = mysqli_fetch_assoc($result1)){
$name = $row["FirstName"];
$age = $row["Age"];
$story = $row["Story"];
$image = $row["Image"];
}
}
mysqli_close($conn);
?>
EDIT
Had two queries when I only needed one.
$str = htmlentities($_GET["story_id"]);
$queryread = "SELECT Stories.ID, Users.FirstName, Stories.Age, Stories.Story, Stories.Image FROM `Stories` INNER JOIN `Users` ON Users.ID = Stories.User_ID
WHERE Stories.ID='$str'";
$result1 = mysqli_query($conn, $queryread) or die(mysqli_error($conn));
if(mysqli_num_rows($result1) > 0 ){
while($row = mysqli_fetch_assoc($result1)){
$name = $row["FirstName"];
$age = $row["Age"];
$story = $row["Story"];
$image = $row["Image"];
}
}
mysqli_close($conn);
?>
you are reading the data from (result1) not from (result).
Change this one while($row = mysqli_fetch_assoc($result1)){
to
while($row = mysqli_fetch_assoc($result)){
and then it will works fine with you .
Related
How to add the result of the while loop which is stored on the variable $profit? results are 5 and 70
the code
<?php
$sql2 = "SELECT * from `products`";
$result2 = $link->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$sql3 = "SELECT * from `orders` where product_id = '".$row2['prod_id']."'";
$result3 = $link->query($sql3);
$row3 = $result3->fetch_assoc();
$sql = "SELECT SUM(product_qty) from orders where product_id = '".$row2['prod_id']."'";
$result = $link->query($sql);
$row = mysqli_fetch_array($result);
$res = bcmul($row2['prod_price'], $row[0]);
$profit = $res - $row2['prod_cost'];
if($row[0] == null){
$row[0] = 0;
}
}?>
Try this code
<?php
$sql2 = "SELECT * from `products`";
$result2 = $link->query($sql2);
$totalProfit = 0;
while($row2 = $result2->fetch_assoc())
{
$sql3 = "SELECT * from `orders` where product_id = '".$row2['prod_id']."'";
$result3 = $link->query($sql3);
$row3 = $result3->fetch_assoc();
$sql = "SELECT SUM(product_qty) from orders where product_id = '".$row2['prod_id']."'";
$result = $link->query($sql);
$row = mysqli_fetch_array($result);
$res = bcmul($row2['prod_price'], $row[0]);
$profit = $res - $row2['prod_cost'];
$totalProfit = $totalProfit + $profit
if($row[0] == null){
$row[0] = 0;
}
}?>
now you can use $totalProfit variable value as total.
I am querying from two different tables in the database here and I can store the values from the first query but how do I store the information from the second query?
$query = "SELECT * ";
$query .= "FROM user_account ";
$query .= "WHERE user_id = $user_id ";
$query .= "SELECT * ";
$query .= "FROM user_profile ";
$query .= "WHERE user_id = $user_id ";
if (mysqli_multi_query($mysqli, $query)) {
do {
if ($result = mysqli_store_result($mysqli)) {
while ($row = mysqli_fetch_row($result)) {
$Firstname = $row['Firstname'];
$Lastname = $row['Lastname'];
$Email = $row['Email'];
$Birthday = $row['Birthday'];
$Address = $row['Address'];
$Zip = $row['Zip'];
$City = $row['City'];
$State = $row['State'];
$Country = $row['Country'];
$Avatar = $row['Avatar']; Will be added later
$Phone = $row['Phone'];
$Website = $row['Website'];
$Member_level = $row['Member_level'];
}
mysqli_free_result($result);
}
if (mysqli_more_results($mysqli)) {
}
while (mysqli_next_result($mysqli)) ;
}
}
Just the usual way
$query = "SELECT * FROM user_account WHERE user_id = $user_id ";
$account = $mysqli->query($query)->fetch_assoc();
$query = "SELECT * FROM user_profile WHERE user_id = $user_id ";
$profile = $mysqli->query($query)->fetch_assoc();
as simple as that.
I am really wondering why PHP users are inclined to writing SO MUCH code and to using so much intricate ways for the most trifle operations
On a side note, in your particular case you can write a single JOIN query.
In your code, why are you using mysqli_free_result function because it will frees the memory associated with a result object so when while loop run it will not show any result.
$query = "SELECT * FROM user_account WHERE user_id = $user_id ";
$query .= "SELECT * FROM user_profile WHERE user_id = $user_id ";
if (mysqli_multi_query($mysqli, $query)) {
do {
if ($result = mysqli_store_result($mysqli)) {
while ($row = mysqli_fetch_assoc($result)) {
var_dump($row); //for checking result
// Now use array to show your result
}
} }while (mysqli_next_result($mysqli)) ;
You should always free your result with mysqli_free_result(), when
your result object is not needed anymore.
Hi I am building a blog using html and php and have run into a problem with my sql. In my blog I would like to show all the comments that have been put in by users in the comments section that have the same article ID. In my database I am saving these parameters via $_POST and a query ID, ArticleID, Comments. However only the last comment that has been inserted in the database with that articleID is showing up.
this is the code that I am using. Can anyone help me please?
if(isset($_POST['submit']))
{
$comment = htmlentities($_POST["comment"]);
$articleID = $_GET['artId'];
$query = "INSERT INTO tbl_comments (comment, ArticleID) VALUES ('$comment', $articleID)";
$result = mysqli_query($connection, $query) or die("Error in query: ". mysqli_error($connection));
}
$query1 = "SELECT * FROM tbl_comments WHERE ArticleID = $artId";
$result1 = mysqli_query($connection, $query1) or die("Error in query: ". mysqli_error($connection));
while($row = mysqli_fetch_assoc($result1))
{
$articleId = $row['ArticleID'];
$comment = $row['comment'];
}
if(isset($comment))
{
echo "<div class='comments'>";
if (isset($comment))
{
echo "<div class='commentName'>";
echo $comment;
echo "</div>";
}
change
while($row = mysqli_fetch_assoc($result1))
{
$articleId = $row['ArticleID'];
$comment = $row['comment'];
}
to:
$comment = '';
while($row = mysqli_fetch_assoc($result1))
$comment .= $row['comment'] . '<br/>';
So I have two tables in my database one is called Users and the other is News
I made it that users can add News Posts to the site, but I couldn't display user's image next to his post
this is my code right now
<?php
$News = "";
$user_id = "";
$sqlCommand = "SELECT * FROM News ORDER BY id DESC LIMIT 10";
$sqlCommand3 = "SELECT * FROM Users";
$query = mysql_query($sqlCommand) or die(mysql_error());
$query3 = mysql_query($sqlCommand3) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$News .= "";
while(($row = mysql_fetch_array($query)) && ($row2 = mysql_fetch_array($query3)) ){
$News .= "<div class=\"news-post\"> <img src=\".$row2['author_avatar']."\"><p>".$row['author']."</p> <h2>".$row['title']."</h2></div> ";
} // close while
} else {
$News = "No News!";
}
?>
I want where is says $row2['author_avatar'] to echo the image from the users table
You missed out a speech mark (") just before $row2['author_avatar'].
<?php
$News = "";
$user_id = "";
$sqlCommand = "SELECT * FROM News ORDER BY id DESC LIMIT 10";
$sqlCommand3 = "SELECT * FROM Users";
$query = mysql_query($sqlCommand) or die(mysql_error());
$query3 = mysql_query($sqlCommand3) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$News .= "";
while(($row = mysql_fetch_array($query)) && ($row2 = mysql_fetch_array($query3)) ){
$News .= "<div class=\"news-post\"> <img src="\".$row2['author_avatar']."\"><p>".$row['author']."</p> <h2>".$row['title']."</h2></div> ";
} // close while
} else {
$News = "No News!";
}
?>
You should look up INNER JOIN rather than having two SQL queries as mentioned by andrewsi. Here's a good tutorial on how to use it https://www.youtube.com/watch?v=6BfofgkrI3Y.
This is the entire code the error must be in query 3 or 4. As you can see query 3 just gets info to build query 4 which should return the results.
I've got query 1 & 2 working ok which have the correct data showing.
<?php
session_start();
//printf('<pre>%s</pre>', print_r($_SESSION, true));
require('includes/config.inc.php');
require('includes/session.php');
//WORKING
DB_Connect();
$query = "SELECT * FROM food_delivery_orders_items WHERE food_delivery_orders_items.type = 'product' AND food_delivery_orders_items.order_id=".$_SESSION['pdf_quote']['id']."";
$result = mysql_query( $query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$hash = $row['hash'];
$foreignid = $row['foreign_id'];
$qty = $row['cnt'];
}
$query2 = "SELECT * FROM food_delivery_products WHERE food_delivery_products.id = ".$foreignid."";
$result2 = mysql_query( $query2) or die(mysql_error());
while($row = mysql_fetch_array($result2))
{
$name = $row['name'];
$description = $row['description'];
}
//---------------------------------------------------------------------------------------------------------------
$query3 = "SELECT * FROM food_delivery_orders_items WHERE food_delivery_orders_items.type = 'extra' AND food_delivery_orders_items.order_id=".$_SESSION['pdf_quote']['id']."";
$result3 = mysql_query( $query3)or die(mysql_error()) ;
while($row = mysql_fetch_array($result3))
{
$foreignidextra = $row['foreign_id'];
$qtyextra = $row['cnt'];
}
$query4 = "SELECT * FROM food_delivery_extras WHERE food_delivery_extras.id = ".$foreignidextra."";
$result4 = mysql_query( $query4) or die(mysql_error());
while($row = mysql_fetch_array($result4))
{
$nameextra = $row['name'];
}
echo $nameextra;
echo $qtyextra;
DB_Disconnect();
//$products = implode(", ",$_SESSION['pdf_quote']['product_arr']);;
//print $products
?>
print $row; is only valid inside the loop if you want to print all of them. Otherwise it will print only the last $row. Make it
while($row = mysql_fetch_array($result3))
{
$foreignidextra = $row['foreign_id'];
$qtyextra = $row['cnt'];
print $row;
}
its because you are printing the $row after the while loop gets executed
So it only prints the final result set
Use something like this instead:
$query3 = "SELECT * FROM food_delivery_orders_items WHERE food_delivery_orders_items.type = 'extra' AND food_delivery_orders_items.order_id=".$_SESSION['pdf_quote']['id']."";
$result3 = mysql_query( $query3) ;
while($row = mysql_fetch_array($result3))
{
$foreignidextra = $row['foreign_id'];
$qtyextra = $row['cnt'];
print_r($row);
}
you can also use the var_dump($row); instead of print_r($row);
this will output all the values stored in the $row array each time the loop iterates