In: Computer Science
How would I structure the following PHP (PDO) code into a table format using only PHP?
//Our SQL statement, which will select a list of tables from the
current MySQL database.
$sql = "SELECT * FROM jobs";
//Prepare our SQL statement,
$statement = $pdo->prepare($sql);
//Execute the statement.
$statement->execute();
//Fetch the rows from our statement.
$tables = $statement->fetchAll(PDO::FETCH_NUM);
//Loop through our table names.
foreach($tables as $table){
//Print the table name out onto the page.
echo $table[0], '
';
echo $table[1], '
';
echo $table[2], '
';
echo $table[3], '
';
echo $table[4], '
';
}
If you have any doubts, please give me comment...
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
try {
$pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM jobs";
//Prepare our SQL statement,
$statement = $pdo->prepare($sql);
//Execute the statement.
$statement->execute();
//Fetch the rows from our statement.
$tables = $statement->fetchAll(PDO::FETCH_NUM);
//Loop through our table names.
echo "<table>";
foreach($tables as $table){
//Print the table name out onto the page.
echo "<tr><td>".$table[0]. "</td><td>".$table[1]."</td><td>".$table[2]."</td><td>".$table[3]."</td><td>".$table[4]."</td></tr>";
}
echo "</table>";
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>