MySQL Multi Query store result second query - php

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.

Related

Show different data in webpage when clicked depending on the ID

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 .

How to store UUID from one MySQL query and use it in another one by using PDO

I'm using this query in my php file:
<?php
require_once("database/config.php");
$customerNr = $_POST['customer_nr'];
$customerUuid = '';
$name = '';
$query = "SELECT HEX(uuid) AS customer_uuid, name FROM customers
WHERE customer_number = :customerNr";
$sql = $db->prepare($query);
$sql->bindValue(":customerNr", $customerNr);
$sql->execute();
if ($row = $sql->fetch()) {
$name .= $row["name"];
$customerUuid .= $row["customer_uuid"];
}
When I use echo "$customerNr<br>$name<br>$customerUuid"; I can see all these data.Now I want to use $customerUuid in another query in the same php file but it's not working.
$addressUuid = '';
$street = '';
$zipCode = '';
$city = '';
$query = "SELECT HEX(uuid) AS address_uuid, street, zip_code, city
FROM addresses WHERE customer_uuid = UNHEX(:customerUuid)";
$sql = $db->prepare($query);
$sql->bindValue(":customerUuid", $customerUuid);
$sql->execute();
if ($row = $sql->fetch()) {
$street .= $row["street"];
$zipCode .= $row["zip_code"];
$city .= $row["city"];
$addressUuid .= $row["address_uuid"];
}
Could anybody help me how to prepare this query?

How to show multiple items from mySQL database

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/>';

php while loop only displays one result for a mysql query across multiple tables

I am having an issue with a while statement only returning one result. this is my first time trying to display product information from several tables and I don't know what I'm doing wrong. here is the code:
<?php
require('./includes/config.inc.php');
require(MYSQL);
$sql = sprintf("SELECT * FROM tea");
$res = mysqli_query($dbc, $sql);
if(!$res){
die('Could not complete query: '.mysqli_error($dbc));
} else {
echo 'Success!<br />';
while($row = mysqli_fetch_array($res)){
$table = $row['category'];
$inum = $row['item_number'];
echo $table.' '.$inum.'<br />';
$fetch = sprintf("SELECT sub_category, item_name, description FROM $table WHERE item_number ='$inum'");
$fetchRes = mysqli_query($dbc, $fetch);
if(!$fetchRes){
die('Could not fetch: '.mysqli_error($dbc));
} else {
while($fetchRow = mysqli_fetch_array($fetchRes)){
$subCat = $fetchRes['sub_category'];
$iNumber = $inum;
$iname = $fetchRes['item_name'];
$desc = $fetchRes['description'];
echo $id.' '.$subCat.' '.$iNumber.' '.$iname.' '.$desc.'<br />';
}
}
}
}
Inside your while loop, you should be using the $fetchRow variable as the array from which to grab columns, such as 'sub_category'.
IS
$subCat = $fetchRes['sub_category'];
$iNumber = $inum;
$iname = $fetchRes['item_name'];
$desc = $fetchRes['description'];
Needs to be: Res to Row
$subCat = $fetchRow['sub_category'];
$iNumber = $inum;
$iname = $fetchRow['item_name'];
$desc = $fetchRow['description'];

Grab min value in PHP from MySQL Database?

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.

Categories