Get results and use them to another query mysql php - php

How can I use the fetched result to use the values to make another query? I tried to find info on php.net, but can`t figure out.
$sql = "SELECT id FROM orders WHERE customer_id=$customer_id";
$query = mysqli_query($conn, $sql);
while($row= mysqli_fetch_array($query)) {
$ordersid=$row['id'];
}
$ordersid returns: 13 - order number 1 and 3.
Here is my difficulty. How can I make $orderid(1,3)?
After that I want to use 1 and 3 like that in another query:
SELECT * FROM orderdetails WHERE order_id IN ($orderid)
In that way without direct relation will have all answers from the first query to second.
Where is my mistake?

You could put the results into an array and use like follows:
$a = array();
$sql = "SELECT id FROM orders WHERE customer_id=$customer_id";
$query = mysqli_query($conn, $sql);
while($row= mysqli_fetch_array($query)) {
array_push($a, $row['id']);
}
$data = implode("', '", $a);
And in your next query like so:
SELECT * FROM orderdetails WHERE order_id IN ('$data')

Related

Output is array, how to fix this?

Right now I am selecting some data from Mysql which I want to echo out, but I don't know how..
Code
<?php
// Database connection
require_once($_SERVER["DOCUMENT_ROOT"] . "/includes/config.php");
require_once($_SERVER["DOCUMENT_ROOT"] . "/includes/opendb.php");
// News 1
$searchroutenews1 = "SELECT newsid FROM NewsHomepage WHERE id = '1'";
$handlenews1 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenews1);
$news1 = mysqli_fetch_row($handlenews1);
$searchroutenewsimg1 = "SELECT newsimg FROM NewsHomepage WHERE id = '1'";
$handlenewsimg1 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenewsimg1);
$NewsImg1 = mysqli_fetch_row($handlenewsimg1);
// News 2
$searchroutenews2 = "SELECT newsid FROM NewsHomepage WHERE id = '2'";
$handlenews2 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenews2);
$news2 = mysqli_fetch_row($handlenews2);
$searchroutenewsimg2 = "SELECT newsimg FROM NewsHomepage WHERE id = '2'";
$handlenewsimg2 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenewsimg2);
$NewsImg2 = mysqli_fetch_row($handlenewsimg2);
// News 3
$searchroutenews3 = "SELECT newsid FROM NewsHomepage WHERE id = '3'";
$handlenews3 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenews3);
$news3 = mysqli_fetch_row($handlenews3);
$searchroutenewsimg3 = "SELECT newsimg FROM NewsHomepage WHERE id = '3'";
$handlenewsimg3 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenewsimg3);
$NewsImg3 = mysqli_fetch_row($handlenewsimg3);
?>
After this I require_once this in an other file, and then I echo the variables $news1, $news2, $news3, $NewsImg1, $NewsImg2 and $NewsImg3. But if I echo this variables out now it says: array.
You can fetch all this information via a single query, instead of the 6 you currently are running. Then it's a matter of putting mysqli_fetch_*() as the argument of a while, as you'll then fetch all the rows, until that function returns null - at which point you've fetched all the rows returned by the query.
$result = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT newsid, newsimg FROM NewsHomepage WHERE id IN (1, 2, 3)");
while ($row = mysqli_fetch_assoc($result)) {
echo $row['newsid']." ".$row['newsimg'];
}
Change however you need it to be displayed inside the while loop, and use the two variables as they are inside.
Alternatively, you can use WHERE id BETWEEN 1 AND 3 instead, but using IN (1, 2, 3) can more easily be changed to the exact ids you need.
http://php.net/mysqli-result.fetch-assoc
First you should read http://php.net/manual/en/mysqli-result.fetch-row.php
you can find there mysqli_result::fetch_row -- mysqli_fetch_row — Get a result row as an enumerated array mysqli_fetch_row always return array so now you can't echo array thats why it gives you array.
You can try foreach loop or for loop or while loop to display your data. there are also various methods to get array value.
Below is an example you can use.
while ($news1 = mysqli_fetch_row($handlenews1)) {
echo $news1[0];
}

PHP & MYSQLI selecting MySQL Table for Shop Items

For my school I need to Select items from the MySQL Server and it only should show the name and the price.
$sql = "SELECT `item`,`price` FROM `items` ";
$query = mysqli_query(con(), $sql);
$row = mysqli_fetch_array($query);
foreach($row as $values)
{
echo "<p>".$values["item"]."</p>";
echo "<p>".$values["price"]."</p>";
}
I only got something like:
L
L
L
L
4
4
4
4
Its the only first item in the table but there are many rows in DB.
you're doing fine, but what you're doing wrong is, you're picking just one item and iterating over that single value.
Here you're extracting only LLLL4444 and looping on this only, so, in order to get all.
You need to do this.
$sql = "SELECT `item`,`price` FROM `items` ";
$query = mysqli_query(con(), $sql);
while($row = mysqli_fetch_array($query))
{
echo "<p>".$row["item"]."</p>";
echo "<p>".$row["price"]."</p>";
}
Try this:
$sql = "SELECT `item`,`price` FROM `items` ";
$query = mysqli_query(con(), $sql);
while ($row = mysqli_fetch_assoc($query)) {
echo "<p>".$row["item"]."</p>";
echo "<p>".$row["price"]."</p>";
}
mysql_fetch_array() essentially returns two arrays one with numeric index, one with associative string index.
So using mysql_fetch_array() without specifying MYSQL_ASSOC or MYSQL_NUM, or by specifying MYSQL_BOTH will return two arrays

Accessing two different tables for one loop

I have a small issue that I can't figure out.
I have to pull data from two different tables, in one loop. I've never done that before, so I have no idea how. I tried two different queries. That looked like this:
$query = "SELECT * FROM colors ";
$color_select = mysqli_query($connection, $query);
$second_query = "SELECT * FROM votes";
$vote_select = mysqli_query($connection, $second_query);
And then put them into a loop:
while($row = mysqli_fetch_assoc($color_select) && $second_row = mysqli_fetch_assoc($vote_select))
{
$color = $row['Colors'];
$votes = $second_row['Votes'];
echo "<tr><td>$color</td><td>$votes</td></tr>";
}
But that didn't work. I didn't expect it to, just wanted to try. :) Maybe someone experienced can help me out. Thanks.
At the end of the day I need a table displayed, that has two columns, one of them contains the color name from one DB table and the other one contains a number of votes.
As requested: table structures.
Table: colors has only one field Colors.
Table: votes has four fields city_id, City, Colors and Votes
*************************EDIT**************************************
So fixed up the query as suggested, but is still shows nothing.
Here is the edited code:
$query = "SELECT * FROM colors,votes WHERE colors.Colors=votes.Colors";
$color_votes_select = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($color_votes_select))
{ $color = $row['Colors'];
$votes = $row['Votes']; }
if table having relation.
try this in single query .
SELECT
`colors`.*,votes.*
FROM
`colors`
INNER JOIN
`votes` ON
`votes`.colorId = `colors`.Id
Most imp *****You should have some relationship between tables
Otherwise workaround
Run query on color, Save it in ArrayA
Run query on vote, Save it in ArrayB
Create New Array ArrayC
$arrayC = array();
Loop array A or C if they both contact same row count
array_push($ArrayC, key and value of color, key and value of votes);
Final loop ArrayC to print tr and td
First Relate These two tables, write color_id in votes table.
$query = "SELECT * FROM colors,votes where colors.id=votes.color_id";
$color_select = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($color_select))
{
$color = $row['Colors'];
$votes = $row['Votes'];
}
Try this:
$query = "SELECT colors FROM colors";
$color_select = mysqli_query($connection, $query) or die (mysqli_error());
$second_query = "SELECT votes FROM votes"; //you only need column votes right?
$vote_select = mysqli_query($connection, $second_query) or die (mysqli_error());;
while( $row = mysqli_fetch_assoc($color_select) && $second_row = mysqli_fetch_assoc($vote_select)){
$color[] = $row['colors'];
$votes[] = $second_row['votes'];
echo "<tr><td>$color</td><td>$votes</td></tr>";
}
Short explanation:
It will fetch the select and store into an array (because what you were doing is selecting multiple rows into one single variable) and then just display with the echo.

Using a php function in a while loop to get the values and the counts of the values in same column

I am trying to perform a query inside a while loop that is getting values from a column. I am trying to query first to get all my values from a column in my DB and then get the count of how many times that value is in that column.
Examples of output trying to get
myValue is in the column 3 times
myOtherValue is in the column 10 times
myOtherOtherValue is in the column 22 times
Example of code
$sql = "SELECT DISTINCT id, columnName FROM tableName";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
function myCount($id)
{
$query = "SELECT COUNT(*) FROM tableName WHERE name = '$id'";
$result = mysql_query($query);
$count = mysql_fetch_array($result);
}
echo "$id is in the column $count[0] times";
}
You can't define a function inside the while loop.
Using COUNT(*) with a GROUP BY name clause, then you could solve the problem with only one query.

Mysqli fetch array nth row

I have the following code that fetches a single row:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$row = mysqli_fetch_array($result);
I know this is useful in a while loop, where you can just loop through the results.
But I need to be able to grab specific rows that meet this condition. Something following this pseudo code:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_arrayofrows($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
and so on...
How can I do this?
You can try something like this
$arrayofrows = array();
while($row = mysqli_fetch_array($result))
{
$arrayofrows = $row;
}
You can now have
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
I think the function you are looking for is mysqli_fetch_all as shown below. This avoids the need to create an extra looping structure or a function to do something already provided.
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_all($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
It depends on whether you require the entire result set back or not but I think the LIMIT could be used like:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1 LIMIT 200,200;";
Otherwise as others say you will need to convert to an array (which is what fetch_all does) and then get the element from that array.

Categories