0

I am trying to fetch all data by id , my Id is 149 and 3 post asign with this id . but I am getting only one post here .

SELECT * FROM pages WHERE rel_blog IN (149) and page_category='auspicious' order by add_date desc limit 7

Exact code is

        <?php

        $get_id = "select rel_blog from pages";
        //TRIM(username) AS username
        $get_id;
        $resu = mysql_query($get_id);
        $p_id="";
        $rel_b="";
        $i = 1;
        while ($page_id = mysql_fetch_array($resu)){

           $p_id.= $page_id['page_id']; 
           $rel_b.= $page_id['rel_blog']; 
         //echo $rel_b   
          } 
        $p_id."<br>";
        $k=ltrim($rel_b,',');
       // echo $k;
       // echo $rel_b."<br>";

        echo  $getblog = "SELECT * FROM pages WHERE rel_blog IN ($k) and page_category='auspicious' order by add_date desc limit 7";
        //echo $getblog;

?>

I am trying to fetch data by top written query enter image description here

web duniya
  • 23
  • 3
  • 1
    `LIMIT 7` does not guarantee that you will get 7 records, it only guarantees that you _won't_ get more than 7. – Tim Biegeleisen Feb 21 '18 at 07:25
  • In your query it is you are using `149` and in question you have provided `144` – Mittal Patel Feb 21 '18 at 07:27
  • Tim Biegeleisen@problem is that I am getting only one post , while 3 post assign with this id – web duniya Feb 21 '18 at 07:27
  • Mittal @my mistake , its 149 i will change this – web duniya Feb 21 '18 at 07:28
  • 1
    Check the page_category for all 3 post? Does it 'auspicious' for all? – Mittal Patel Feb 21 '18 at 07:30
  • And what about page category. How you expect a answer in case we can see a data? – Kancho Iliev Feb 21 '18 at 07:30
  • Mittal @yes page category for all , please let me know how can i used group by in this query , IN operator consider only one data, if id is change , like 140 and 150 its return all data , but id is same 140 and 140 then its return only one – web duniya Feb 21 '18 at 07:36
  • Just add some sample and expected data to this question. – cdaiga Feb 21 '18 at 07:37
  • cdaiga@see updated question i attached screenshot, I want fetch data my id is same for both post like category name – web duniya Feb 21 '18 at 07:43
  • Any budy know how can get all data with same id With IN operators or any alternate solution – web duniya Feb 21 '18 at 07:48
  • **The `mysql` PHP extension is dead** -- Stop using the [`mysql_*()` PHP functions](http://php.net/manual/en/function.mysql-connect.php). They are old, deprecated since PHP 5.5 and completely removed in PHP 7. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`PDO_mysql`](http://php.net/manual/en/ref.pdo-mysql.php) instead. Read the answers to [this question](https://stackoverflow.com/q/12859942/4265352) to learn more about why and how. – axiac Feb 21 '18 at 09:32

1 Answers1

0
<?php
while ($page_id = mysql_fetch_array($resu)){    
    $p_id.= $page_id['page_id']; 
    $rel_b.= $page_id['rel_blog']; 
} 
$p_id."<br>";
$k=ltrim($rel_b,',');

You are concatenating the string $rel_b inside while loop. The value of $rel_b will be eg; 145147149

You should also concatenate with "," like this and try again

$rel_b.= $page_id['rel_blog'] . ","; 
Rahul Dev
  • 175
  • 1
  • 15