#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    757
    Rep Power
    0

    Question To Display Mysql Rows Or Results Through Html Table, Should We Use echo or printf ?


    Php Buds,

    When you want to display results from your mysql tbl onto the page then you use echo, right ?
    Q1. But, when you want to display the results through html table then do you use echo or print or printf ?
    I guess any would work but I see printf used the most. Why is that ?

    Anyway, do have a look at my code ....It's a code that pulls only the final row from the mysql tbl. The query part was aided by Sepodati.
    PHP Code:
    <?php 

    //Required PHP Files. 
    include 'config.php'
    include 
    'header.php'
    include 
    'account_header.php'

    if (!
    $conn

        
    $error mysqli_connect_error(); 
        
    $errno mysqli_connect_errno(); 
        print 
    "$errno$error\n"
        exit(); 

    else 
    {     
        
    //Get the Page Number. Default is 1 (First Page). 
        
    $page_number $_GET["page_number"]; 
        if (
    $page_number == ""
        { 
            
    $page_number 1
        } 
        
        
    /*
        <?php     
        
        $sender_username = "admin123";
        $recipient_username = "user";
        $result_limit = 1; 
        
        //FORMULA $query = SELECT fields FROM table ORDER BY id DESC LIMIT 1;
        $query = "SELECT id,date_and_time,recipient_username,sender_username,notice FROM notices WHERE recipient_username = ? AND sender_username = ? ORDER BY id DESC LIMIT ?"; 
        $stmt = mysqli_prepare($conn, $query);    
        mysqli_stmt_bind_param($stmt,'ssi',$recipient_username,$sender_username,$result_limit);      
        mysqli_stmt_execute($stmt);  
        $result = mysqli_stmt_bind_result($stmt,$id,$date_and_time,$recipient_username,$sender_username,$notice);
        mysqli_stmt_fetch($stmt);    
        
        echo "id: $id";?><br><?php 
        echo "date & time: $date_and_time";?><br><?php 
        echo "recipient username: $recipient_username";?><br><?php 
        echo "sender_username: $sender_username";?><br><?php   
         
        ?> 
        */

        
    $sender_username "admin123"
        
    $recipient_username "$user"
        
    $result_limit 1
        
        
    //FORMULA SELECT fields FROM table ORDER BY id DESC LIMIT 1; 
        //$query = "SELECT id,date_and_time,recipient_username,sender_username,notice FROM notices WHERE recipient_username = ? AND sender_username = ? ORDER BY id DESC LIMIT ?"; 
        
    $query "SELECT * FROM notices WHERE recipient_username = ? AND sender_username = ? ORDER BY id DESC LIMIT ?"
        
    $stmt mysqli_prepare($conn,$query);
        
    mysqli_stmt_bind_param($stmt,'ssi',$recipient_username,$sender_username,$result_limit); 
        
    mysqli_stmt_execute($stmt); 
        
    $result mysqli_stmt_bind_result($stmt,$id,$date_and_time,$recipient_username,$sender_username,$notice); 
        
    mysqli_stmt_fetch($stmt); 
        
    //mysqli_stmt_free_result($stmt); 
        
        
    ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN"> 
        <html> 
        <head> 
        <meta content="text/html; charset=ISO-8859-1" http-equiv=" content-type"> 
        <title><?php echo "$site_name User $user Notices in $server_time time."?></title> 
        </head> 
        <body> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User $user Final Notice in $server_time time."?></span></align> 
        <br> 
        <br> 
        <table width="1500" border="0" cellpadding="5" cellspacing="2" bgcolor="#666666"> 
        <?php if(!$stmt
        { 
            
    ?> 
            <tr> 
            <td bgcolor="#FFFFFF">No record found! Try another time.</td> 
            </tr> 
            <?php 
        

        else 
        { 
            echo 
    "Result Found!"?><br> 
            <br> 
            <tr name="headings"> 
            <td bgcolor="#FFFFFF" name="column-heading_submission-number">Submission Number</td> 
            <td bgcolor="#FFFFFF" name="column-heading_logging-server-date-and-time">Date & Time in <?php echo "$server_time?></td> 
            <td bgcolor="#FFFFFF" name="column-heading_recipient-username">To</td> 
            <td bgcolor="#FFFFFF" name="column-heading_sender-username">From</td> 
            <td bgcolor="#FFFFFF" name="column-heading_notice">Notice</td> 
            </tr> 
            <tr name="user-details">             
            <td bgcolor="#FFFFFF" name="submission-number"><?php printf("%s",$id); ?></td> 
            <td bgcolor="#FFFFFF" name="logging-server-date-and-time"><?php printf("%s",$date_and_time); ?></td> 
            <td bgcolor="#FFFFFF" name="recipient-username"><?php printf("%s",$recipient_username); ?></td> 
            <td bgcolor="#FFFFFF" name="sender-username"><?php printf("%s",$sender_username); ?></td> 
            <td bgcolor="#FFFFFF" name="notice"><?php printf("%s",$notice); ?></td> 
            </tr> 
            <?php 
            
    //Use this technique: http://php.net/manual/en/mysqli-stmt.fetch.php 
            
    while(mysqli_stmt_fetch($stmt)) 
            { 
                
    ?> 
                <tr name="user-details"> 
                <td bgcolor="#FFFFFF" name="submission-number"><?php printf("%s",$id); ?></td> 
                <td bgcolor="#FFFFFF" name="logging-server-date-and-time"><?php printf("%s",$date_and_time); ?></td> 
                <td bgcolor="#FFFFFF" name="recipient-username"><?php printf("%s",$recipient_username); ?></td> 
                <td bgcolor="#FFFFFF" name="sender-username"><?php printf("%s",$sender_username); ?></td> 
                <td bgcolor="#FFFFFF" name="notice"><?php printf("%s",$notice); ?></td> 
                </tr> 
                <?php 
                ?>
     
                <tr name="pagination"> 
                    <td colspan="10" bgcolor="#FFFFFF"> Result Pages:             
                    <?php              
                    
    if($page_number $total_pages
                    { 
                        for(
    $i=1;$i<=$total_pages;$i++) //Show Page Numbers in Serial Order. Eg. 1,2,3.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?user=$user&page_number={$i}\">{$i}</a> "
                    }             
                    else 
                    { 
                        for(
    $i=$total_pages;$i>=1;$i--) //Show Page Numbers in Reverse Order. Eg. 3,2,1.
                        
    echo "<a href=\"{$_SERVER['PHP_SELF']}?user=$user&page_number={$i}\">{$i}</a> "
                    } 
                    
    ?> 
                    </td> 
                    </tr> 
                    <?php 
            

        } 
        
    ?>  
        </table>     
        <br> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User $user Final Notice in $server_time time."?></span></align> 
        <br> 
        </div> 
        <br> 
        </body> 
        </html> 
        <?php 
        
    //Free Result Set. 
        
    mysqli_stmt_free_result($stmt); 
        
    //Close Statement Connection. 
        
    mysqli_stmt_close($stmt); 
        
    //Close Database Connection. 
        
    mysqli_close($conn); 

    ?>
    Note the big commented-out section. Originally, I used echo to display result with no html table. Then, I switched to displaying the result inside the html table and switched from echo to printf. Should I keep printf as it is or switch to echo ?
    I did google and had a read at this thread which is not mine:
    https://stackoverflow.com/questions/...o-print-printf

    Any tips and advice ?
    Once upon a time few wks back, I did learn the pros of the printf but now I have forgotten it why you use it over the echo. I'd appreciate it if anyone can jog my memory to what I forgot about the benefits of the printf over the echo.

    Btw, I did my best to keep the code neat and short using mysqli and procedural style. You're welcome to smoothen it up as much as you can and show your edit (using mysqli and procedural style, ofcourse.

    Thanks!
    Last edited by UniqueIdeaMan; May 1st, 2018 at 05:42 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    757
    Rep Power
    0

    Question


    Folks,

    If you really must know then my final edit looks like this following now.
    But, I do need answers to my questions mentioned in my previous post so do remember to address them!
    Thanks!

    PHP Code:
    <?php 

    //Required PHP Files. 
    include 'config.php'
    include 
    'header.php'
    include 
    'account_header.php'

    if (!
    $conn

        
    $error mysqli_connect_error(); 
        
    $errno mysqli_connect_errno(); 
        print 
    "$errno$error\n"
        exit(); 

    else 
    {     
        
    //Get the Page Number. Default is 1 (First Page). 
        
    $page_number $_GET["page_number"]; 
        if (
    $page_number == ""
        { 
            
    $page_number 1
        } 
        
        
    $sender_username "$social_network_admin_username"
        
    $recipient_username "$user"
        
    $result_limit 1
        
        
    //FORMULA SELECT fields FROM table ORDER BY id DESC LIMIT 1; 
        //$query = "SELECT id,date_and_time,recipient_username,sender_username,notice FROM notices WHERE recipient_username = ? AND sender_username = ? ORDER BY id DESC LIMIT ?"; 
        //$query = "SELECT * FROM notices WHERE recipient_username = ? AND sender_username = ? ORDER BY id DESC LIMIT ?"; 
        
    $query "SELECT * FROM notices ORDER BY id DESC LIMIT ?"
        
    $stmt mysqli_prepare($conn,$query);
        
    //mysqli_stmt_bind_param($stmt,'ssi',$recipient_username,$sender_username,$result_limit); 
        
    mysqli_stmt_bind_param($stmt,'i',$result_limit); 
        
    mysqli_stmt_execute($stmt); 
        
    $result mysqli_stmt_bind_result($stmt,$id,$date_and_time,$recipient_username,$sender_username,$notice); 
        
    mysqli_stmt_fetch($stmt); 
        
    //mysqli_stmt_free_result($stmt); 
        
        
    ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN"> 
        <html> 
        <head> 
        <meta content="text/html; charset=ISO-8859-1" http-equiv=" content-type"> 
        <title><?php echo "$site_name User $user Notices in $server_time time."?></title> 
        </head> 
        <body> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User $user Final Notice in $server_time time."?></span></align> 
        <br> 
        <br> 
        <table width="1500" border="0" cellpadding="5" cellspacing="2" bgcolor="#666666"> 
        <?php if(!$stmt
        { 
            
    ?> 
            <tr> 
            <td bgcolor="#FFFFFF">No record found! Try another time.</td> 
            </tr> 
            <?php 
        

        else 
        { 
            echo 
    "Result Found!"?><br> 
            <tr name="headings"> 
            <td bgcolor="#FFFFFF" name="column-heading_submission-number">Submission Number</td> 
            <td bgcolor="#FFFFFF" name="column-heading_logging-server-date-and-time">Date & Time in <?php echo "$server_time?></td> 
            <td bgcolor="#FFFFFF" name="column-heading_recipient-username">To</td> 
            <td bgcolor="#FFFFFF" name="column-heading_sender-username">From</td> 
            <td bgcolor="#FFFFFF" name="column-heading_notice">Notice</td> 
            </tr> 
            <tr name="user-details">             
            <td bgcolor="#FFFFFF" name="submission-number"><?php printf("%s",$id); ?></td> 
            <td bgcolor="#FFFFFF" name="logging-server-date-and-time"><?php printf("%s",$date_and_time); ?></td> 
            <td bgcolor="#FFFFFF" name="recipient-username"><?php printf("%s",$recipient_username); ?></td> 
            <td bgcolor="#FFFFFF" name="sender-username"><?php printf("%s",$sender_username); ?></td> 
            <td bgcolor="#FFFFFF" name="notice"><?php printf("%s",$notice); ?></td> 
            </tr> 
            <?php 
            
    /*
            <tr name="user-details">            
            <td bgcolor="#FFFFFF" name="submission-number">echo "id: $id";?></td>
            <td bgcolor="#FFFFFF" name="logging-server-date-and-time">echo "date & time: $date_and_time";?></td>
            <td bgcolor="#FFFFFF" name="recipient-username">echo "recipient username: $recipient_username";?></td>
            <td bgcolor="#FFFFFF" name="sender-username">echo "sender_username: $sender_username";?></td>
            <td bgcolor="#FFFFFF" name="notice">echo "Notice: $notice";?></td>
            </tr> 
            */ 
            
    ?> 
        <br> 
        <br> 
        <?php 
        

        
    ?> 
        </table>     
        <br> 
        <br> 
        <p align="center"><span style="font-weight:bold;"><?php echo "$site_name User $user Final Notice in $server_time time."?></span></align> 
        <br> 
        </div> 
        <br> 
        </body> 
        </html> 
        <?php 
        
    //Free Result Set. 
        
    mysqli_stmt_free_result($stmt); 
        
    //Close Statement Connection. 
        
    mysqli_stmt_close($stmt); 
        
    //Close Database Connection. 
        
    mysqli_close($conn); 

    ?>
    Note that, I did my query in 3 different ways. I commented-out the earlier 2 versions for you to check from what I changed to my latest. What do you think ? Do you think I got it right at the 3rd round to shorten the code as much as possible ? Remember, the code is supposed to pull the final row ONLY from the mysql tbl.

    I'm still at procedural style using mysqli. Let me know how I can improve using the mysqli in the procedural style.
    I will flock to pdo later. Not now. Getting there soon. First, need to learn how to walk first and finish these projects with mysqli using procedural style. Then, jump to pdo and oop style where (chuckle: Benanamen will help me throughout my pdo learning sessions).
    Last edited by UniqueIdeaMan; May 1st, 2018 at 05:47 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Location
    Cheshire, UK
    Posts
    85
    Rep Power
    72
    If you are worried about the length of your code, start with the HTML.

    <td> elements do not have a name attribute so all those name="blah" 's are just a waste of space.
    <bgcolor> tags are deprecated. Use a CSS style td {background-color:#FFFFFF;} and your <td>'s all get a white background automatically.

    So
    Code:
    <td bgcolor="#FFFFFF" name="column-heading_recipient-username">To</td>
    becomes
    Code:
    <td>To</td>

    Comments on this post

    • UniqueIdeaMan agrees : Thanks for the html shortening tips!
  6. #4
  7. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,750
    Rep Power
    9646
    Use ONE THREAD for this code. I don't care what the question is, if it involves that code then ask it in the other thread.

IMN logo majestic logo threadwatch logo seochat tools logo