Hi Folks!
I have been designing basic sites / web applications for the last couple of years now but have recently decided that I really want to be able to code amazing sites, so have started a journey to teach myself to become a better programmer / coder. (so any pointers on books to read would be good) my skills so far... (Jquery, PHP (procedural), HTML / XHTML) so want to develop these further and also learn AJAX, PHP OOP.
On a daily basis i get on just fine with PHP writing almost all sites / apps in PHP ( Procedural ) but have decided that i should stop being lazy and actually learn to code OOP PHP, the only problem is on my first attemp i am completly stumped, i have wrote what i believe to be an extramly simple piece of code but for some reason it just does not work .
Can one of you nice people please point out where the hell i am going wrong, i have written (see code below) a "class_lib.php" for my db connection etc and a simple html ("index.php") to display the results.
"CLASS_LIB.PHP"
<?php
class dbcon {
var $dbhost = NULL;
var $dbuser = NULL;
var $dbpass = NULL;
var $db = NULL;
var $con = NULL;
function setdb($host, $user, $password, $database) {
$this->dbhost = $host;
$this->dbuser = $user;
$this->dbpass = $password;
$this->db = $database;
$this->con = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die (mysql_error());
mysql_select_db($this->db) or die (mysql_error());
}
function querydb($query) {
return mysql_query($query, $this->con) or die (mysql_error());
}
}
?>
"INDEX.PHP"
<?php include("class_lib.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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p><?php
$mysql = new dbcon();
$mysql->setdb('localhost','user','password','database');
$result = $mysql->querydb("SELECT * FROM datatable");
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$id = mysql_result($result,$i,"id");
$fname = mysql_result($result,$i,"fname");
$sname = mysql_result($result,$i,"sname");
echo nl2br("<div><b>$fname $sname</b><br /><br /><hr></div>");
$i++;
}
?></p>
</body>
</html>