$query1 = "select *
from linkat_link
where emailuser = '$email2'
or linkname ='$domain_name2'
ORDER BY date desc
LIMIT $From,$PageNO";
id catid discription price
------------------------------------
1 1 domain name 100
2 1 book 50
3 2 hosting 20
4 2 myservice 20
in this script i have one problem , if i have an ID for Each Cantegory , i have some duplicated CATID which has different content but shares the same CATID, i need to make any duplicated CATID to show in one , and all the discription will be in the same line (Cell) on the same row .
So Each CatID will have all the details in one Row without any redundancy in the CATID
Using:
SELECT t.catid,
GROUP_CONCAT(t.description),
GROUP_CONCAT(t.price)
FROM LINKAT_LINK t
WHERE t.emailuser = mysql_real_escape_string($email2)
OR t.linkname = mysql_real_escape_string($domain_name2)
GROUP BY t.catid
ORDER BY t.date DESC
LIMIT mysql_real_escape_string($From), mysql_real_escape_string($PageNO)
...will return:
catid description price
--------------------------------
1 domain name,book 100,50
2 hosting,myservice 20,20
See the documentation for what the GROUP_CONCAT function does.
How to print it to screen using PHP?
Use:
<?php
$query1 = "SELECT t.catid,
GROUP_CONCAT(t.description) AS grp_desc,
GROUP_CONCAT(t.price) AS grp_price
FROM LINKAT_LINK t
WHERE t.emailuser = mysql_real_escape_string($email2)
OR t.linkname = mysql_real_escape_string($domain_name2)
GROUP BY t.catid
ORDER BY t.date DESC
LIMIT mysql_real_escape_string($From), mysql_real_escape_string($PageNO)";
$result = mysql_query($query1);
while($row = mysql_fetch_array($result)) {
echo "CatID:{$row['catid']} <br>" .
"Description : {$row['grp_desc']} <br>" .
"Price : {$row['grp_price']} <br><br>";
} ?>
Related
I have problem with selecting selecting SQL Max and group syntax then set it to variable. I have no problem from just selecting the MAX score. But Id like to echo the user that own that score and id to redirect to his profile page
This is my Database
-----------------------------
id | user | score | justplayed
1 | player1 | 1000 | 1
2 | player2 | 1000 | 0
PHP code
$sql = "SELECT MAX(score) AS max_score FROM score GROUP BY score WHERE justplayed > 0 ";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$highscore = $row["max_score"];
$highscoreowner = $row["user"];
$highscoreownerid = $row["id"];
}
}
Html Code
Player of the day
<? echo $highscoreowner; ?>
Score
<? echo $highscore; ?><br>
<a href="profile.php?id=<? echo $highscoreownerid; ?>
You're only selecting the max score in the SQL query, so you can't extract the other values in PHP. If you're looking for the entire row where the score is highest, then use this query instead:
SELECT score, user, id
FROM score
WHERE justplayed > 0
ORDER BY score DESC
LIMIT 1
This will take all the rows, order them from highest to lowest score, and then return only the first row.
I'm having this PDO query to call data from a MySQL.
$sql = "SELECT itemName FROM furniture WHERE itemID = :item";
While calling for this particular itemName, is it possible to get the next and previous itemNames by using its itemID within this same query itself without having to write a new query to get the next and previous itemNames?
e.g.
if
itemID | itemName
___________________________
553 | Mahogani Black Chair
554 | Teak Round Table
555 | Thulang Relaxing Chair
556 | Teak Relaxing Chair
$sql = "SELECT itemName FROM furniture WHERE itemID = :item";
$stmt = $connect->prepare($sql);
$stmt->execute(array(':item'=>"554"));
$rslt = $stmt->fetchAll(PDO::FETCH_ASSOC);
I'm looking away of getting Teak Round Table and Mahogani Black Chair and Thulang Relaxing Chair
Please use this code:
(SELECT itemName FROM furniture WHERE itemID < 554 order by itemID desc limit 1)
UNION
(SELECT itemName FROM furniture WHERE itemID >= 554 order by itemID asc limit 2)
For Example code :
MyTable:
================
id Store_name
================
1 English
2 French
3 Tamil
4 Uk
5 US
<?php
$con = mysqli_connect("localhost","root","ramki","ramki");
$sql = "(SELECT store_name FROM store WHERE id < 2 order by id desc limit 1)
UNION
(SELECT store_name FROM store WHERE id >= 2 order by id asc limit 2)";
$query = mysqli_query($con,$sql);
while ($row = mysqli_fetch_assoc($query)) {
echo $row['store_name'];
echo "<br>";
}
?>
$sql = "SELECT itemName FROM furniture WHERE itemID IN (:item-1, :item, :item+1) ORDER BY itemID";
For iterating the results, you can also use PDO fetch() function to get each row.
I am trying to show statistics for goals scored by players, however sometimes the same player is added to the database with the same playerID, when a player is added twice how can I add the values together to show it as a total, rather than echo the player twice.
db structure example:
playerID | Goals | Season | leagueID
1 5 1 1
2 1 1 1
1 2 1 2
5 3 1 1
1 3 2 2
2 2 2 1
php:
$query = $db->query('SELECT * FROM playerstats ORDER BY goals DESC LIMIT 30');
$num = $query->num_rows;
if($num > 0) {
foreach($query as $row) {
$playerID = $row['playerID'];
$goals = $row['goals'];
echo '
<tr>
<td>'.$playerID.'</td>
<td>'.$goals.'</td>
</tr>
';
}
}
This would show playerID 1, 3 seperate times.
How can I make it show playerID 1 just once with all the goals added together (10)
I have tried changing the query to: SELECT DISTINCT * FROM playerstats ORDER BY goals DESC LIMIT 30 but this made no difference.
Group BY will help you:
<?php
$query = $db->query('SELECT SUM(p.goals) as total_goals,
p.playerID, p.leagueID, p.Season
FROM playerstats as p
GROUP BY p.playerID, p.leagueID, p.Season
ORDER BY total_goals DESC LIMIT 30');
$num = $query->num_rows;
if($num > 0) {
foreach($query as $row) {
echo '
<tr>
<td>'.$row['playerID'].'</td>
<td>'.$row['total_goals'].'</td>
</tr>
';
}
}
Please note, that my query will group also by season and leagues, if you want total goals throughs seasons and leagues, your group by will be:
GROUP BY p.playerID
If you want only the playerID and the sum of the goals not mattering the Season or the leagueID, change your query to:
SELECT DISTINCT playerID AS player,
(SELECT SUM(goals) FROM playerstats WHERE playerID = player) AS totalGoals
FROM playerstats
Well you would group results in array then loop over it. Or sort at SQL level.
Something like this might work:
$playerStats = array();
foreach($query as $row)
{
if(array_key_exists($row['playerID'], $playerStats))
$playerStats[$row['playerID']] += $row['goals'];
else
$playerStats[$row['playerID']] = $row['goals'];
}
// Then loop over playerStats
foreach($playerStats as $playerID => $playerGoals)
}
echo $playerID;
echo $playerGoals;
{
Here the SQL query and I need some adjustment to it
SELECT DISTINCT ic.img_path, ic.id
FROM images_community ic
WHERE ic.delete_flag = 0 AND ic.status = 1
ORDER BY ( SELECT (count(id = ic.id ) + ic.views)
FROM image_comments WHERE img_id = ic.id) DESC
I need to show record after specific ID
not limit and offset for sure I need display rows after this id =5
this images retrieve
> images id | img_path
> 1 | dafad.sjdbh
> 2 | dafad.sjdbh
> 5 | dafad.sjdbh
> 3 | dafad.sjdbh
IS there a way to display records after id = 5 in the same query retrieved
You can use limit and offset to achieve it, please see Limit, Offset
If you want all rows after offset just do
SELECT DISTINCT ic.img_path, ic.id
FROM images_community ic
WHERE ic.delete_flag = 0 AND ic.status = 1
ORDER BY ( SELECT (count(id = ic.id ) + ic.views)
FROM image_comments WHERE img_id = ic.id) DESC LIMIT (SELECT cout(*) FROM table_name) OFFSET my_row_offset
Found the row position for this ID and start the Limit from it.
My database has 2 tables "category" and "article" ,
Category
id | Category
---------------------
1 Mobiles
2 Computers
--------------------
Article
id | Category | title |article
-------------------------------------
1 2 title article desc
2 1 title article desc
3 1 title article desc
4 1 title article desc
5 2 title article desc
--------------------------------------
My generated url for articles pages is in this format.
URL/ID/TITLE
I would like to show the next and previous articles titles in the current articles page .
so i used the below code to show the next and previous title with link.
Previous article
$getid = $_GET['id'];
$previous = $getid - 1;
$previousdata = $db->getRow("select * from article WHERE news_id='$previous'");
Next article
$getid = $_GET['id'];
$next = $getid + 1;
$nextdata = $db->getRow("select * from article WHERE news_id='$next'");
And php code like
<?php echo friendlyURL($previousdata['title']); ?>
This works fine,It shows the mobiles and computers posts in next and previous links using the id's,
if i post 2 articles in mobiles category the generated id was 12 and 13 for example, and next if i post an article in computer category the generated id was 14. when the user reading mobile article of id 13 ,at bottom it shows previous article title with id 12 and next article title with id of 14 . Obviously the previous article is mobile and next is computers categories.
But i would like to show only the next and previous pagination with "mobiles" category only,
So i try to assign the category
$getid = $_GET['id'];
$next = $getid + 1;
$nextdata = $db->getRow("select * from article WHERE Category=1 AND id='$next'");
This doesn't filter only mobile ..
You can directly get it from query as below
$getid = $_GET['id'];
$nextdata = $db->getRow("select * from article WHERE Category=1 AND news_id > ".$getid." order by news_id ASC LIMIT 0,1");
$previousdata = $db->getRow("select * from article WHERE Category=1 AND news_id < ".$getid." order by news_id DESC LIMIT 0,1");
If I understand you want to get then next/previous article id in the same category:
The next article:
$nextdata = $db->getRow("
select *
from article
where Category = 1 and id > $current_article_id
order by id
limit 1
");
The previous article:
$previousdata = $db->getRow("
select *
from article
where Category = 1 and id < $current_article_id
order by id desc
limit 1
");