Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i am trying to display my table from my MySQL database. it doesn't seem to work. i am a newbie so i get its most likely something simple. if possible i would like to have the table look like just a normal table with no frils just a boarder and to be able to fit in a normal sized page. this is what i have so far.

<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?
$connection = "connect.php";
require $connection;

mysql_select_db("bank");

$sql = "SELECT * FROM profiles";

$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>

<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>

 <?php

 while( $row = mysql_fetch_assoc( $result ))
 {
 echo <tr>
 <td>{$row\['id'\]}</td>
 <td>{$row\['fname'\]}</td>
 <td>{$row\['lname'\]}</td>
 <td>{$row\['email'\]}</td>
 <td>{$row\['dob'\]}</td>
 <td>{$row\['age'\]}</td>
 <td>{$row\['city'\]}</td> 
 <td>{$row\['state'\]}</td>
 <td>{$row\['zip'\]}</td>
 <td>{$row\['offence'\]}</td>
 <td>{$row\['notes'\]}</td>
 </tr>\n;
 }
 ?>
 </tbody>
 </table>

 <?php 
 mysql_close($connection); 
 ?>

 </body>
 </html>
share|improve this question
    
You are not opening/closing your table rows - <tr>...</tr> –  Sean Sep 13 '13 at 5:01
    
are you getting any errors? it doesn't seem to work,means you are getting errors or o/p is not what you want.. –  plain jane Sep 13 '13 at 5:02
    
you're missing the <tr> tags, also mysql_ functions are deprecated, you should not use them anymore, learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. –  koala_dev Sep 13 '13 at 5:02
    
thank you very much, i did not know about that. lol ive been reading about it for the past half hour now and it does seem like something i need to learn more about. –  user2775066 Sep 13 '13 at 6:49

5 Answers 5

Give this a go:

<table>
    <tr>
        <th>ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>E-Mail</th>
        <th>DOB</th>
        <th>Age</th>
        <th>City</th>
        <th>State</th>
        <th>Zip Code</th>
        <th>Offence</th>
        <th>Notes</th>
    </tr>
    <?php

        require "connect.php";

        mysql_select_db("bank");
        $result = mysql_query("SELECT * FROM profiles");

        while($row = mysql_fetch_array($result, MYSQL_NUM)) {
            echo '<tr>';
            foreach($row as $column) {
                echo '<td>', $column, '</td>';
            }
            echo '</tr>';
        }
    ?>
</table>
share|improve this answer
    
i tried that, it does show the table names, but nothing populates –  user2775066 Sep 13 '13 at 6:23
WHILE($row = mysql_fetch_array($result))

should be either

WHILE($row = mysql_fetch_array($result, MYSQL_ASSOC))

OR

WHILE($row = mysql_fetch_assoc($result))

Also the mysql_* functions have been deprecated and relying on them is HIGHLY discouraged.

Kindly check the below discussions :-

Deprecated MySql Functions
How to successfully rewrite old mysql-php code with deprecated mysql_* functions?
http://php.net/manual/en/migration55.deprecated.php

Use either MySQLi or PDO.

share|improve this answer
    
i managed to get the table to show the header, but the information from the table is still not populating. we are connecting and it does recognize the db and table, –  user2775066 Sep 13 '13 at 6:12
    
whats the diffrence between mysql and mysqli? –  user2775066 Sep 13 '13 at 6:25
    
oh wow. thank you –  user2775066 Sep 13 '13 at 6:50
<center><table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
<?
$connection = "connect.php";
require $connection;

mysql_select_db("bank");

$sql = "SELECT * FROM profiles";

$result = mysql_query($sql);

WHILE($row = mysql_fetch_array($result))

{
$id = $row ['id'];
$firstname = $row ['fname'];
$lastname = $row ['lname'];
$email = $row ['email'];
$dob = $row ['dob'];
$age = $row ['age'];
$city = $row ['city'];
$state = $row ['state'];
$zip = $row ['zip'];
$offence = $row ['offence'];
$nots = $row ['notes'];

echo '<tr>';   // change here
echo '<td>'.$id.'</td>';
echo '<td>'.$firstname.'</td>';
echo '<td>'.$lastname.'</td>'; 
echo '<td>'.$email.'</td>';
echo '<td>'.$dob.'</td>'; 
echo '<td>'.$age.'</td>';
echo '<td>'.$city.'</td>'; 
echo '<td>'.$state.'</td>';
echo '<td>'.$zip.'</td>'; 
echo '<td>'.$offence.'</td>';
echo '<td>'.$notes.'</td>'; 
echo '</tr>';  // change here
}

mysql_close();
?>


</table></center>
share|improve this answer
    
thank you very much. this has helped me. this shows the table head and a boarder but it doesnt seem to populate the data. i am connecting and it does see the table. –  user2775066 Sep 13 '13 at 6:30
    
welcome..please accept as answer click on the tick mark given to the left side of my answer. –  chirag ode Sep 13 '13 at 6:53

Try this code:

<?php

$username = "your_name";
$password = "your_password";
$hostname = "localhost";

$dbconnect = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$select=mysql_select_db("bank",$dbconnect) or die("Could not select bank");

$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<center><table border="1">
<tr>
<th>ID</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>E-Mail</th>
    <th>DOB</th>
    <th>Age</th>
    <th>City</th>
    <th>State</th>
    <th>Zip Code</th>
    <th>Offence</th>
    <th>Notes</th></tr>
<?php
while($row = mysql_fetch_array($result))

{
    $id = $row ['id'];
    $firstname = $row ['fname'];
    $lastname = $row ['lname'];
    $email = $row ['email'];
    $dob = $row ['dob'];
    $age = $row ['age'];
    $city = $row ['city'];
    $state = $row ['state'];
    $zip = $row ['zip'];
    $offence = $row ['offence'];
    $nots = $row ['notes'];

    echo '<tr>';
    echo '<td>'.$id.'</td>';
    echo '<td>'.$firstname.'</td>';
    echo '<td>'.$lastname.'</td>';
    echo '<td>'.$email.'</td>';
    echo '<td>'.$dob.'</td>';
    echo '<td>'.$age.'</td>';
    echo '<td>'.$city.'</td>';
    echo '<td>'.$state.'</td>';
    echo '<td>'.$zip.'</td>';
    echo '<td>'.$offence.'</td>';
    echo '<td>'.$notes.'</td>';
    echo '</tr>';
}
?>
</table></center>

<?php
mysql_close();
?>
share|improve this answer

Corrected Code:

<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?php
$connection = "connect.php";
require $connection;

mysql_select_db("bank");

$sql = "SELECT * FROM profiles";

$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>

<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>

 <?php

 while( $row = mysql_fetch_assoc( $result ))
 {
 ?>
 <tr>
 <td><?php echo $row['id'];?></td>
 <td><?php echo $row['fname'];?></td>
 <td><?php echo $row['lname'];?></td>
 <td><?php echo $row['email'];?></td>
 <td><?php echo $row['dob'];?></td>
 <td><?php echo $row['age'];?></td>
  <td><?php echo $row['city'];?></td>
 <td><?php echo $row['state'];?></td>
 <td><?php echo $row['zip'];?></td>
 <td><?php echo $row['offence'];?></td>
 <td><?php echo $row['notes'];?></td>
 </tr>
 <?php
 }
 ?>
 </tbody>
 </table>

 <?php 
 mysql_close($connection); 
 ?>

 </body>
 </html>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.