-1

I am trying to display my table from my MySQL database. It doesn't seem to work, I guess it's most likely something simple. If possible I would like to have the table look like just a normal table with no frills; just a border 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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2775066
  • 1
  • 1
  • 2
  • You are not opening/closing your table rows - `...` – Sean Sep 13 '13 at 05: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 05:02
  • you're missing the `` tags, also mysql_ functions are deprecated, you should not use them anymore, learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – omma2289 Sep 13 '13 at 05: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 06:49

5 Answers5

0

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>
George Brighton
  • 5,131
  • 9
  • 27
  • 36
0
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.

Community
  • 1
  • 1
verisimilitude
  • 5,077
  • 3
  • 30
  • 35
0
<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>
chirag ode
  • 950
  • 7
  • 15
  • 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 06: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 06:53
0

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();
?>
Saravanan M P
  • 561
  • 1
  • 7
  • 12
0

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>
Pupil
  • 23,834
  • 6
  • 44
  • 66