mysql row into variable using while loop - php

I'm trying to output data into an xml file, everything is working fine except it's only saving the last record fetched.
The following query is used:
$query = mysql_query("SELECT mileage FROM cars WHERE make ='bmw' ORDER BY date DESC, time DESC LIMIT 3");
while ($row = mysql_fetch_array($query)){
$result = $row['mileage'];
}
My question is how do I make it so each record is saved in 3 seperate variables so I can output those 3 variables into an xml file. I am also trying to fetch only the last 3 rows sorted by the last date and last time so not sure if the query is correct for doing that.
Thank you.

Firstly: Don't use the mysql_* functions! They are obsolete and you should use PDO instead.
Secondly: The easiest thing to do is to construct an array containing the results.
$result = array();
$query = mysql_query("SELECT mileage FROM cars WHERE make ='bmw' ORDER BY date DESC, time DESC LIMIT 3");
while ($row = mysql_fetch_array($query)){
$result[] = $row['mileage'];
}
The results of your query are now contained in $result[0], $result[1] and $result[2]. (If you use PDO the code does not look very different.)

$result=array();
$query = mysql_query("SELECT mileage FROM cars WHERE make ='bmw' ORDER BY date DESC, time DESC LIMIT 3");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$result[] = $row['mileage'];
}

You can use folowing;
$query = mysql_query("SELECT mileage FROM cars WHERE make ='bmw' ORDER BY date DESC, time DESC LIMIT 3");
while ($row = mysql_fetch_array($query)){
$result[] = $row['mileage'];
}
You can foreach $result and construct your xml.

try saving the output as an array instead.
$array[]=$value;
will push the $value inside the $array array.

get yourself a function
function dbGetCol($sql){
$ret = array();
$res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
if ($res) {
while($row = mysql_fetch_row($res)){
$ret[] = $row[0];
}
}
return $ret;
}
and then just call it
$data = dbGetCol("SELECT mileage FROM cars WHERE make ='bmw' ORDER BY date DESC, time DESC LIMIT 3");

Related

Is there a way to sort PHP array_merge based on raw[2]?

I am creating a time attendance PHP script. I started from storing data in the database MySQL and use different tables for the Punch-in/Punch-out, Breaks, Lunch/Dinner breaks.
Each one of the casualties mentioned before have a table which is structured with an:
ID, start, end, flag
I have already tried to do this with the following code of foreach but unsuccessfully!
foreach ($total as $key => $row) {
$start[$key] = $row['start'];
}
$final = array_multisort($start, SORT_DESC, $total);
echo $final;
Here is the code that I have made with 2 SELECT MySQL queries
$result = mysqli_query($db, "SELECT * FROM time WHERE username =
'$user_check' ORDER BY start DESC");
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
} $db->next_result();
$result = mysqli_query($db, "SELECT * FROM break WHERE username =
'$user_check' ORDER BY start DESC");
$data2 = array();
while ($row = mysqli_fetch_assoc($result)) {
$data2[] = $row;
}
$total = array_merge ($data, $data2);
I would like to have 1 array of this 2 queries sorted DESC by start as I will use this array to populate a table in DESC order.
Use UNION ALL in your query, instead of merge arrays. Let the database do the work for you:
SELECT * FROM
(SELECT * FROM time WHERE username = '$user_check'
UNION ALL
SELECT * FROM break WHERE username = '$user_check'
) ORDER BY start DESC
PS: Your code is vulnerable to SQL Injection. You should use prepared statement instead.

Two MySQL output in single JSON encode in PHP

I have two MySQL output which I need to encode in a single JSON output.
Output 1:
$sql = "select * from t1 ORDER BY id DESC LIMIT 25";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$output[] = array_map("nl2br", $row);
}
Output 2:
$sql2 = "select * from t2 ORDER BY id DESC LIMIT 25";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc()) {
$output2[] = array_map("nl2br", $row2);
}
This is what I am doing to get them in single JSON_encode:
echo json_encode($output.$output2);
still not getting both the outputs. I came to know of another solutions i.e. to merge both the queries but I am not able to do that as well. I referred this question also but no luck :(
How about using UNION in your query? Please check it out here: https://dev.mysql.com/doc/refman/5.0/en/union.html
What about
$fullOutput = array_merge($output1, $output2);
echo json_encode($fullOutput);

Echo the 2 last columns of tabel

I have a table and i want to echo the 2 last rows of my tabel, I used the below code but just the last one showed, what is the problem.
$result1 =(mysql_fetch_array(mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")));
Print $result1['time'];
mysql_fetch_array = 1 fetch.
do it again for fetching 2nd result.
Also, use mysqli eh.
You're doing mysql_fetch_array only one time, so it gets the first element. If you want to get all the elements, then do it again, or use a loop.
Something like:
$query = "SELECT * FROM $table ORDER BY id DESC LIMIT 2";
while($row = mysql_fetch_array(mysql_query($query) )
{
echo $row['time'].'<br>';
}
For 2 Or more rows you need to loop it
$sql = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")
while($row=mysql_fetch_array($sql))
{
echo $row['time']."<br>";
}
$query = mysqli_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2");
while ($result = mysqli_fetch_array($query)) {
echo $result['time'];
}
Gives out every return of your database (2 in this case). You should use mysqli_-functions.
Maybe you should try like this, since mysql_fetch_array returns only one row
while ($row = mysql_fetch_array($yourQuery)) {
echo $row["yourAlias"];
}
Further details here : http://fr2.php.net/manual/en/function.mysql-fetch-array.php
My solution:
$limit = 2;
$sql = "SELECT * FROM $table ORDER BY id DESC LIMIT $limit";
$result = mysql_query($sql);
$array = array(); $i = 0;
while($row = mysqli_fetch_array($result))
{
$array[$i] = $row;
$i++;
}
var_dump($array[0]);
var_dump($array[1]);

mysql_fetch_array pulling fewer results than it should

I have the following code:
$mostRecent = mysql_query("SELECT couponID FROM users_coupons WHERE userID = '$userID' ORDER BY id LIMIT 3");
while($row = mysql_fetch_array($mostRecent))
{
$mostRecentArr = $row;
}
var_dump($mostRecentArr);
The same SQL query in the command line returns 3 results, but this code only returns one result, even though I put LIMIT 3. Any help?
while($row = mysql_fetch_array($mostRecent))
{
$mostRecentArr[] = $row['couponID'];
}
You reassign the value in the loop each time, then dump the last value. Put var_dump in the loop after the assignment. Like so:
$mostRecent = mysql_query("SELECT couponID FROM users_coupons WHERE userID = '$userID' ORDER BY id LIMIT 3");
while($row = mysql_fetch_array($mostRecent))
{
$mostRecentArr = $row;
var_dump($mostRecentArr);
}
I guess what are you trying to do is:
$mostRecent = mysql_query("SELECT couponID FROM users_coupons WHERE userID = '$userID' ORDER BY id LIMIT 3");
$mostRecentArr = array();
while($row = mysql_fetch_array($mostRecent))
{
$mostRecentArr[] = $row;
}
var_dump($mostRecentArr);

How to display other values, when a query is limited by 3?

Can anyone tell me how to display the other values, when a query is limited my 3. In this question I asked how to order and limit values, but now I want to show the others in another query. How would I go about doing this?
Here's the code I used before:
$query = "SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
}
If display all rows use like this :
$query = "SELECT gmd FROM account ORDER BY gmd DESC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
}
If display all rows without that 3 rows use like this :
$query = "SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3,1000000";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
}
if you LIMIT the resultset in the query you only get 3 rows returned.
if you want to show the rest, don't limit inside the query, but check the rowcount in your php loop
SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3,9999999999
or may be you need pagination
SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3,10
Will skip first 3 values and display next 10 ones satisfying the condition. This is MySQL only solution though.

Categories