PDO Query Help

Discussion in 'PHP' started by scottlpool2003, Apr 25, 2013.

  1. #1
    Hi

    I'm trying to paginate some results from a PDO query. I can get the count and get the pagination working, but for some reason can't output the data.

    I'm not too good at handling arrays, but I can var_dump the data and get an output, but when I try and output it as a $row, I don't get a result.

    What am I missing?

    Thanks


    
     
    <?php 
    $sth = $dbconn->prepare ("SELECT     publication.id, publication.title, publication.text, publication.country, publication.city, publication.cat, publication.user_id, publication.use_ad, publication.viewcount, 
                          publication.tags, publication.status, publication.mod_status, publication_issue.publication_id, publication_issue.img, publication_issue.id AS Expr1
    FROM         publication INNER JOIN
                          publication_issue ON publication.id = publication_issue.publication_id
    WHERE     (publication.tags LIKE :tags)"); 
     
     
    $results = $sth->execute(array(":tags"=>'%e%'));
     
    while ($row = $sth->fetchAll(PDO::FETCH_ASSOC)) {
     
     
     
        $pages = ceil(count($row)/4); 
        $page  = (isset ($_GET['page']))  ? (int) $_GET['page'] : 1 ;
        $start = ($page - 1) *  $per_page; 
     
        echo '<br>' . $row['id'] . '<br>' . $row['title'] . '<br>' . $row['img'] . '<br>';
        ?>
        <br><br>
        <?php
        if ($pages >=  1 && $page <= $pages){
        //if ($page>1 && $page <= $pages){$previous=($page -1); echo '<a href="?page=' .$previous. '">Previous</a>';}
        for($x=1; $x<=$pages; $x++){ echo ($x == $page) ? ' <strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : ' <a href="?page='.$x.'">'.$x.'</a> ';}
        //if ($page>1  && $page <= $pages){ $next=($page+1) ; echo '<a href="?page=' .$next. '">Next</a>';}
        }
     
     
    }
     
     
     
    ?>
     
    
    PHP:

     
    scottlpool2003, Apr 25, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    You need to be using LIMIT.

    At the end of your query you need to set the start point (0 for page 1) and then the end point (total shown per page), these numbers increase as someone goes through the pages.

    LIMIT $start, $end
    Code (markup):
    I don't have time to write the code but have a look it up online, there's tons of examples.
     
    HuggyStudios, Apr 25, 2013 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    Any idea on how the limit would translate into MSSQL? Seen some examples but all of them have rownum in the WHERE clause rather than my wildcard tag...
     
    scottlpool2003, Apr 25, 2013 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4

    Just looked on Google and found this, I have no experience with MSSQL so can't help directly.

    select * from
    (select Row_Number() over ( ORDER BY FLD_AGE DESC ) as RowIndex, FLD_NAME, FLD_AGE FROM TBL_USERS WHERE FLAG='1') as pager Where pager.RowIndex >= 10 and pager.RowIndex < 40
    Code (markup):
    Also check this out: http://vorg.ca/626-the-MS-SQL-equivalent-to-MySQLs-limit-command
     
    HuggyStudios, Apr 25, 2013 IP
  5. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #5

    That's why I'm having so many issues getting this to work. There doesn't appear to be enough people with the knowledge to help me unfortunately.

    With your query I get:


     
    scottlpool2003, Apr 26, 2013 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    That's why the only people who use mssql tend to be in .NET to begin with, and then don't usually build their queries by hand.

    though are you on MSSQL 2012? If so they dragged it kicking and screaming into the bleeding edge of 1985 dBase by adding these:
    	OFFSET 10 ROWS
    	FETCH NEXT 10 ROWS ONLY
    Code (markup):
    Older MSSQL engines it's a bit trickier... You're in the right ballpark with row_number, but you need to use common table expressions to do it.

    $sth = $dbconn->prepare('
    	WITH publicationCTE AS (
    		SELECT 
    		
    			publication.id, publication.title, publication.text, publication.country, publication.city, publication.cat, publication.user_id, publication.use_ad, publication.viewcount, publication.tags, publication.status, publication.mod_status, 
    			
    			publication_issue.publication_id, publication_issue.img, publication_issue.id,
    			
    			ROW_NUMBER() OVER(ORDER BY publication.id) AS rowNumber
    			
    		FROM publication
    		
    		INNER JOIN publication_issue 
    			ON publication.id = publication_issue.publication_id
    			
    		WHERE publication.tags LIKE :tags
    	)
    	SELECT *
    	FROM publicationCTE
    	WHERE rowNumber BETWEEN :startRow AND :endRow
    ');
    Code (markup):
    I THINK that's right... but I'm not sure.

    There's so many gaps in MSSQL I don't know how anyone does anything useful with it -- it's outshined by dBase, clipper and Paradox from the DOS days in terms of query building!
     
    deathshadow, Apr 27, 2013 IP
  7. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #7
    I only switched to MSSQL last month and to be honest, I've found it alright so far. I'm just struggling to build this one query and I'm determined to get it done. I've also got MySQL on the server so I could build the query using that but I'm not going to waste resources doing it.

    I'll give the above a go and see what I come up with.

    Thanks.
     
    scottlpool2003, Apr 29, 2013 IP