Call multiple queries as one - php

I have two queries to a single table in a database, the only difference being the limit. I like the output but I know there must be more practical ways of doing this. Can anyone suggest something?
$sql1 = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT 20");
$count = mysql_num_rows($sql1);
if ($count > 0) {
while($row = mysql_fetch_array($sql1)){
}
}
$sql2 = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT 1");
$count = mysql_num_rows($sql2);
if ($count > 0) {
while($row = mysql_fetch_array($sql2)){
}
}

Create a function and pass the limit as a parameter perhaps:
function doQuery($limit = 1) {
$sql = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT $limit");
$count = mysql_num_rows($sql);
if ($count > 0) {
while($row = mysql_fetch_array($sql)){
}
}
};
doQuery(1);
doQuery(20);

$sql1 = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT 20");
$count = mysql_num_rows($sql1);
if ($count > 0) {
while($row = mysql_fetch_array($sql1)){
//loop over all records...
}
}
$count = mysql_num_rows($sql1);
if ($count > 0) {
if($row = mysql_fetch_array($sql1)){
//retrieve one record
}
}

Related

select maximum value of id from mysql table and save it as variable

Is it possible to extract highest id from table (in this case 9) and return it as variable $maximum, which I can use later as integer?
You can use MAX() function. Doc can be found here
Try this, It will must work.
$sql = "SELECT id FROM some_table ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id = " . $row["id"];
}
}
You can try with following snippet:
$sql = "SELECT * FROM some_table where id = (select max(id) from some_table)";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id = " . $row["id"];
}
}
Try this, It will must work.
$sql = "SELECT id FROM some_table ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
$id = 0;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = $row["id"];
}
}
echo $id;

Get story and all its comments in 1 query

I am trying to create an PHP api in which I am trying to retrieve news storys and all its comments and send it as json.
Here is what i have so far:
$sql = "SELECT * FROM fb_clubnews WHERE clubid='$groupId' AND ori_newsid = 0 ORDER BY newsid DESC LIMIT 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$storId = $row["newsid"];
$commentArray = array();
$wallAray[] = array("author"=>$row["userid"],
"story"=>$row["news"],
"date"=>$row["date"],
"time"=>$row["time"],
"matchid"=>$row["fk_match_id"],
"comments"=>$commentsArray);
}
}
My issue is, that I would like to avoid creating a sql inside an sql and loop through it?! The second inside sql would be:
$sql = "SELECT * FROM fb_clubnews WHERE ori_newsid = $storId ORDER BY newsid DESC";
How do I get the $commentArray filled up with comments.
My DB Structure for fb_clubnews looks like this:
int newsid (autoincrement),
int userid,
text news,
int data,
int time,
int matchid,
int ori_newsid
Hoping for help on this and thanks in advance :-)
Something like this:
$sql = "SELECT *, A.newsid as main_news_id FROM fb_clubnews A LEFT JOIN fb_clubnews B ON B.ori_newsid = A.newsid WHERE A.clubid='$groupId' AND A.ori_newsid = 0 ORDER BY A.newsid DESC";
$result = $conn->query($sql);
$wallAray = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$storId = $row["main_news_id"];
if(array_key_exists($storId, $wallAray)) {
$commentArray[] = $wallAray;
} else {
$wallAray[$storId] = array();
$commentArray = array();
}
$wallAray[] = array("author"=>$row["userid"],
"story"=>$row["news"],
"date"=>$row["date"],
"time"=>$row["time"],
"matchid"=>$row["fk_match_id"],
"comments"=>$commentsArray);
}
}

mysql random entries from the top200

is there a better way to return random entries from the TOP 200 bestselling tshirts (tshirt_sales from shop_tshirts) that could just query just 6 entries instead of 200 ?
$SQL = "SELECT * FROM shop_tshirts WHERE shop = 'nidieunimaitre' AND online='1' ORDER BY tshirt_sales DESC LIMIT 200";
$Result = mysql_query($SQL)
or die('A error occured: ' . mysql_error());
$Rows = array();
while ($Row = mysql_fetch_assoc($Result))
$Rows[] = $Row;
shuffle($Rows);
$i = 0;
foreach($Rows as $Data){
$i++;
if($i >= 6) { break; }
}
Try this:
SELECT *
FROM
(SELECT *
FROM shop_tshirts
WHERE shop = 'nidieunimaitre' AND online='1'
ORDER BY tshirt_sales DESC
LIMIT 200) as tableAlis
ORDER BY RAND()
LIMIT 6;
Return first 200 results and after order by rand and return only six results.

PHP MySQL How can I paginate within two while loop

I would like to know how can I do a LIMIT 0,10 in this case (in order to do a pagination later) because when I do this I obtain a total of 30 results (10 results for each result of the first loop) which is logical, any idea ?
$select = "SELECT X FROM Y"
$result = mysql_query($select,$link);
$total = mysql_num_rows($result);
while($row = mysql_fetch_array($result)) {
$Name = $row['X'];
$select2 = "SELECT id FROM `".$Name."` LIMIT 0,30 ";
$result2 = mysql_query($select2,$link) or die ('Erreur : '.mysql_error() );
$total2 = mysql_num_rows($result2);
while($row2 = mysql_fetch_array($result2)) {
echo $row2['id']
}
}
Thanks in advance for your help !

storing percentages into an array

I'm trying to store a number of percentages that I've worked out into an array so that I can call it later on to create a pie chart but don't have a clue how to do it. Here is my PHP code so far for working out the percentages: Thanks for any help in advance!
//execute the SQL query and return records
$result = mysql_query("SELECT book_id, COUNT(*) FROM loan GROUP BY book_id ASC ORDER BY count(*) DESC LIMIT 10");
$book_count = mysql_num_rows($result);
$step = 1;
while($row = mysql_fetch_array($result))
{
$total = $total + $row['COUNT(*)'];
$i[$step] = $row['COUNT(*)'];
$step++;
}
for($index = 1; $index <= 10; $index++)
{
$percentage[$index] = (($i[$index]/$total)*100);
#$degrees = (($percentage/100)*360);
}
This should do what you're asking, just substitute the variables for real ones:
array_push($array, $value);
// This will add "$value" on to the end of $array, i.e the last element
It can also be done much simpler:
$array[] = $value;
Updated code:
//execute the SQL query and return records
$result = mysql_query("SELECT book_id, COUNT(*) FROM loan GROUP BY book_id ASC ORDER BY count(*) DESC LIMIT 10");
$book_count = mysql_num_rows($result);
$step = 1;
while($row = mysql_fetch_array($result))
{
$total = $total + $row['COUNT(*)'];
$i[$step] = $row['COUNT(*)'];
$step++;
}
$percentage_array = array();
for($index = 1; $index <= 10; $index++)
{
array_push($percentage_array, (($i[$index]/$total)*100));
#$degrees = (($percentage/100)*360);
}

Categories