Jump to content

converting my old script to PDO results do not show?


lovephp

Recommended Posts

can someone tell me why the results are not displaying?

 

 
 <?php        
    
    require_once 'db/_db.php';  
    $stmt = $db->prepare("SELECT ^ FROM article");
    $stmt->execute();
    $result = $stmt->fetchAll();
    $total_rows = $stmt->rowCount();
    
    //$allRecords = mysql_query('select * from article');
    //$total_rows = mysql_num_rows($allRecords);    
            
    //$base_url = 'https://localhost/pagi/';                //
      global $per_page;                           
      $num_links = 4;                           
      $total_rows = $total_rows;
      $cur_page = 1;         
      if(isset($_GET['page']))
        {
          $cur_page = $_GET['page'];
          $cur_page = ($cur_page < 1)? 1 : $cur_page;            //if page no. in url is less then 1 or -ve
        }            
    $offset = ($cur_page-1)*$per_page;       //setting offset   
    $pages = ceil($total_rows/$per_page);
    $start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
    $end   = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;
    
      //$res = mysql_query("SELECT * FROM article ORDER BY id DESC LIMIT ".$per_page." OFFSET ".$offset);
      
      $stm = $db->prepare("SELECT * FROM article ORDER BY id DESC LIMIT ".$per_page." OFFSET ".$offset);
      $stm->execute();
      $res = $stm->fetchAll();
      $rows = $stm->rowCount();
      if($rows < 1){
            header("Location: news-events.php");
            exit;
        }    
            if(is_resource($result))
            {
                
                
                foreach( $res as $row ) {
                
                $desc = $row["ne_article"];
                $img = $row['ne_image'];
                if(!empty($img)){
                $img = '<br/><img src="uploads/images/'.$img.'" alt="" class="responsive-shrink">';
                }else{
                $img = '';
                }
                $youtube = $row["ne_youtube"];    
                if(!empty($youtube)){
                $youtube = '<br/><div class="video-container"><iframe src="http://www.youtube.com/embed/'.$youtube.'"></iframe></div>';
                }else{
                $youtube = '';
                }
                //shortenString($row['ne_article']);
            ?>
                <h4><a href="<?php echo $row['ne_url']; ?>"><?php echo $row['ne_title']; ?></a></h4>
                <h5><b>Views:</b> <?php echo $row['views']; ?>, <b>Posted on:</b> <?php echo format_date($row['created']); ?></h5>                
                <?php echo $img; ?>
                <?php echo $youtube; ?>
                <p>
                <br/><?php echo  bbcode(nl2br(shortenString($desc))); ?>
                </p>
                 <div id="pagelink">
                    <a href="<?php echo $row['ne_url']; ?>" class="myButton"> Read more...</button></a>
                    </div>
            <?php
                }
            }
            ?>

      <div id="pagination">
        <div id="pagiCount">
        <br/><br/>
            <?php
                if(isset($pages))
                {  
                    if($pages > 1)        
                    {    if($cur_page > $num_links)     // for taking to page 1 //
                        {   $dir = "First";
                            echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.(1).'">'.$dir.'</a> </span>';
                        }
                       if($cur_page > 1)
                        {
                            $dir = "Prev";
                            echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page-1).'">'.$dir.'</a> </span>';
                        }                 
                        
                        for($x=$start ; $x<=$end ;$x++)
                        {
                            
                            echo ($x == $cur_page) ? '<strong>'.$x.'</strong> ':'<a href="'.$_SERVER['PHP_SELF'].'?page='.$x.'">'.$x.'</a> ';
                        }
                        if($cur_page < $pages )
                        {   $dir = "Next";
                            echo '<span id="next"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page+1).'">'.$dir.'</a> </span>';
                        }
                        if($cur_page < ($pages-$num_links) )
                        {   $dir = "Last";
                       
                            echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$pages.'">'.$dir.'</a> ';
                        }   
                    }
                }
            ?>

There are dozens of potential and actual problems in your code, so you'll have to be a lot more specific. Check your PHP error log. Use var_dump() to analyze the behavior of your program. Where exactly do things go wrong?

 

Your code contains the check

if(is_resource($result))
{
    ...
}

But $result is an array, and it's completely unrelated to the query at hand. I assume that's a leftover from your old code.

in error logs i find nothing related to this but yes i found this

 

yes that is_resource($result) is previous code how on earth i deal with the pagination without that?

[Tue Mar 15 01:50:40 2016] [error] [client 127.0.0.1] PHP Parse error:  parse error in C:\\wamp\\www\\web\\article.php on line 24, referer: http://localhost/web/news-events.php

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.