As additional information, you should probably use PDO. It has more features and helps in training the preparation of SQL statements. It will also serve you much better if you ever write more complex code.
http://www.php.net/manual/en/intro.pdo.php
This example uses objects, not arrays. It doesnβt necessarily matter, but it uses less characters, so I like it. The difference does occur when you delve into objects, but not in this example.
//connection information $user = "your_mysql_user"; $pass = "your_mysql_user_pass"; $dbh = new PDO('mysql:host=your_hostname;dbname=your_db;charset=UTF-8', $user, $pass); //prepare statement to query table $sth = $dbh->prepare("SELECT name, colour FROM fruit"); $sth->execute(); //loop over all table rows and fetch them as an object while($result = $sth->fetch(PDO::FETCH_OBJ)) { //print out the fruits name in this case. print $result->name; print("\n"); print $result->colour; print("\n"); }
You will probably also want to study prepared statements. It helps against injections. Injection is poor for safety reasons. Here is the page for this.
http://www.php.net/manual/en/pdostatement.bindparam.php
You should probably also consider disinfecting your user. Just a head and not related to your current situation.
Also to get all field names using PDO try this
$q = $dbh->prepare("DESCRIBE tablename"); $q->execute(); $table_fields = $q->fetchAll(PDO::FETCH_COLUMN);
Once you have all the fields in the table, it would be fairly easy to use <div> or even <table> to arrange them as you like using <th>
Happy learning PHP. It's fun.
source share