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> ';
                        }   
                    }
                }
            ?>

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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
Edited by lovephp
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.