Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Cant output MySQL Table data

General discussions related to php

Moderators: macek, egami, gesf

Cant output MySQL Table data

Postby alank8887 » Mon Jun 23, 2014 7:10 am

Hello,
I am totally new to php and am trying to write up a simple web form that keeps track of some inventory and who borrowed it. I have LAMP setup on Ubuntu 13.10 w/PHP 5.5. I had no trouble writing the script that takes customer info and writes to MySQL db but am stuck on getting a script that outputs it. Looking at some examples and reading up I have this so far:

Code: Select all
<?php
    ini_set("display_errors","on");
    $dsn='mysql:host=localhost;dbname=inventory_form';
    $username="****";
    $password="*****";
    $database="inventory_form";
   
    try
        {
        $link=new PDO($dsn, $username,$password);
        echo 'Connection is established';
        }
    catch (PDOException $e)
        {
        $error_message=$e->getMessage();
        echo "<h1>An error occurred: $error_message</h1>";
        }
    $query="SELECT * from Inventory";
    $result=$link->query($query);
    /*/
    $num=mysql_num_rows($result);
   
   
    echo "<b><center>Database Output</center></b>";
   
    ?>
    <table border="2" cellspacing="2" cellpadding="2">
    <tr>
    <th><font face="Arial, Helvetica, sans-serif">Name</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Equipment Borrowed</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Service Tag</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Date Borrowed</font></th>
    </tr>
   
    <?php
    $i=0;
    while ($i < $num) {
    $first=mysql_result($result,$i,"FName");
    $last=mysql_result($result,$i,"LName");
    $Eq_Brwd=mysql_result($result,$i,"Eqpmnt_Brwd");
    $Svc_tag=mysql_result($result,$i,"Service_Tag");
    $Date_Bwd=mysql_result($result,$i,"Date_Taken");
    ?>
   
    <tr>
    <td><font face="Arial, Helvetica, sans-serif"><?php echo "$first $last"; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><?php echo "$Eq_Brwd"; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><?php echo "$Svc_tag"; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><?php echo "$Date_Bwd"; ?></font></td>
    </tr>
    <?php
    ++$i;
    }
    echo "</table>";
   
   
    ?>

All I get is the connection established message. One problem I know is the $num variable is not getting a value returned from my_sql_num_rows.I know there must be a simpler way to output the table info. Any suggestions or better way to this most welcome.
alank8887
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Jun 15, 2014 11:11 am

Return to PHP General

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.