My first real attempt with OOP.
Morning peeps,
I was wondering if people could advise of better ways to do what i've attempted to.
Its just a small class to carry out a select query, as i'm learning i guess i need critism for me to learn from my mistakes so fire away. Ignore how i have displayed the data as thats not what im intrested in, that was just to show it worked.
Beleive me, its nothing fancy!
Class:
Code PHP:
<?php
class select {
var $query; // will store the query string only.
var $result; // will store the results after the execute function is called.
function __construct($columns, $table, $clause){
// Create the query and store in var "query"
$query = "SELECT ";
$query .= implode(", ", $columns); // separate the colums.
$query .= " FROM ".$table;
$query .= $clause;
$this->query = $query;
}
function execute (){
//execute the query and store the results into the var "results";
$this->result = mysql_query($this->query);
}
}
?>
view page:
Code PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>OOP in PHP</title>
<?php include('class_lib.php'); ?>
<?php include('conn.php'); ?>
</head>
<body>
<?php
//setup query parameters
$columns = array("username","email"); // select these columns.
$table = "users";// tables to select from
$clause = " WHERE user_id !='1'"; //class query, options.
$q_get_usrname_email = new select($columns, $table, $clause); // create a new object for select.
$q_get_usrname_email->execute(); // execute the query.
//output the query.
while ($row = mysql_fetch_assoc($q_get_usrname_email->result)){
echo $row['username']." ".$row['email']."<br>";
}
?>
</body>
</html>
Thanks for any pointers in advance.
Steve