Wrote this class it allows me to connect to database and display as many columns as I wish to. I can just give it desired parameters and I'm done. I've commented about the parameters that need it to be passed.
include('connect.php');
//include this file. This is our class
$connection = new connection_to_db;
//instantiate class
$connection->make_connection("host_name", "user", "pass", "database_name");
//1st param: your host name
//2st param: your username
//3rd param: your password
//4th param: your database name
$connection->run_test_query("shouts", "id", 2);
//1st param: your table name.
//2nd param: which column to show.
//3rd param: how many results to display.
class connection_to_db{
function make_connection($host, $user, $pass, $db){
global $conn;
try{
$conn = new pdo("mysql: host=". $host . ";dbname=". $db, $user, $pass);
}catch(PDOException $error){
echo $error->getMessage();
die();
}
}
function run_test_query($shouts, $column_to_show, $count){
global $conn;
$query = $conn->prepare("SELECT * FROM $shouts LIMIT $count");
$query->execute();
while($result = $query->fetch(PDO::FETCH_ASSOC) ){
echo $result["$column_to_show"] . "</br>";
}
}
}