OK, this is just one of many PHP ad MySQL implementation, it is my course which I’m taking actually. So in this post is what my lecturer gave to us. Lets dive into our first step. OK, no more talk, lets practice.
Developing a web application based on PHP and MySQL as its database can be done by many tools, you can use EclipsePDT, Adobe® Dreamweaver®, Netbeans, and many more. We ‘ll not cover many PHP IDE feature, so we can focus on simplicity of this tutorial. To proceed to the next step we’ll use these tools:
- Notepad++ (free text editor with php syntax highlighting)
- SRWare (free web browser)
- XAMPP 1.7.3 with PHP 5.3.1 and MySQL 5.1.41
- This sample database
and now, we can try to cook from some PHP and MySQL,
Run your XAMPP, so apache(php web server) and MySQL server started.
- Create a MySQL database, we named it simpleblog, and import the sample database, (If you need detail you can always ask by commenting below).
- Prepare your web folder in xampp htdocs folder. We named it simpleblog.
- Create MySQL connection file to connect PHP to MySQL. Open Notepad++, create new file, save as MysqlConn.php and save it inside simpleblog/lib folder(note: create lib folder inside simpleblog folder). Copy and paste the next code:
- Set user, password, database name, match to your XAMPP setting.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | <?php /* @created nov 10 2007 13:00 * @lastmodified * @package * @file obj/MysqlConn * @desc extends class Connection * support get single / multiple array * - use getArrayAssoc / getArrayNum -> single data * - use getDataAssoc / getDataNum -> mltiple data * Assoc --> association array, * Num --> numeric array * * copyright (c)2001 - 2007 arif laksito */ Class Connection { var $host; var $dbname; var $user; var $pass; var $error; function Connection() { $this->host = 'localhost'; $this->dbname = 'simpleblog'; $this->user = 'root'; $this->pass = 'pass'; } } Class MysqlConn extends Connection { var $db; // var db conn var $str; // var statatement query var $rst; // var result query // constructor class // assign variable connection function MysqlConn() { $this->Connection(); } // open connection MsSQL DB function openConnection() { $this->db = mysql_connect($this->host,$this->user,$this->pass) or die('connection error..'); mysql_select_db($this->dbname, $this->db) or die('conect to db error..'); } // execution query statement function execute($str) { $this->openConnection(); $this->str = $str; if($this->rst = mysql_query($this->str, $this->db)) return true; else{ die( 'sql error : '.mysql_error() ); return false; } $this->closeConnection(); } // get array association from execution query // return false on error function getArrayAssoc() { if($this->rst) { if($this->row = mysql_fetch_array($this->rst, MYSQL_ASSOC)) return $this->row; else return false; } } // get array numeric from execution query // return false on error function getArrayNum() { if($this->rst) { if($this->row = mysql_fetch_array($this->rst, MYSQL_NUM)) return $this->row; else return false; } } // return data execution query on object data // association object function getObjectAssoc() { if($this->rst) { if($this->row = mysql_fetch_object($this->rst, MYSQL_ASSOC)) return $this->row; else return false; } } // return data execution query on object data // numeric object function getObjectNum() { if($this->rst) { if($this->row = mysql_fetch_object($this->rst, MYSQL_NUM)) return $this->row; else return false; } } // get set array from query result // 'select' tipe query // data --> association array function getDataAssoc() { $dataSet = array(); $i = 0; while($data = $this->getArrayAssoc()) { for($j=0; $j < $this->getNumFields(); $j++) { $field = mysql_field_name($this->rst, $j); $dataSet[$i][$field] = $data[$field]; } $i++; } return $dataSet; } // data --> numeric array function getDataNum() { $dataSet = array(); $i = 0; while($data = $this->getArrayNum()) { for($j=0; $j < $this->getNumFields(); $j++) { $dataSet[$i][$j] = $data[$j]; } $i++; } return $dataSet; } function getNumRows() { if($this->numRows = mysql_num_rows($this->rst)) return $this->numRows; else return false; } function getNumFields() { if($this->numFields = mysql_num_fields($this->rst)) return $this->numFields; else return false; } function getAffRows() { if(mysql_affected_rows()>0) return true; else return false; } function closeConnection() { mysql_close($this->db); } } ?> |
Create index.php file inside simpleblog folder. copy and paste this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php include_once 'lib/MysqlConn.php'; $sql = new MysqlConn(); $str = "SELECT beritaId, beritaTitle, beritaDetail, beritaTime, nama from berita left join alumni On berita.beritaSender = alumni.id_alumni;"; $sql->execute($str); $data = $sql->getDataAssoc(); print '<pre>'; print_r($data); print '</pre>'; ?> |
Ok, for now lets try open the url, for me it will be http://localhost/simpleblog. It should be printing some words like this:
1 2 3 4 5 6 7 8 9 10 | <pre>Array ( [0] => Array ( [beritaId] => 3 [beritaTitle] => Internet Semakin diminati Wanita [beritaDetail] => San Fransisco, Kamis Meskipun, pria dan wanita punya minat yang berbeda dalam menggunakan jaringan internet, lambat laun jumlahnya semakin seimbang. Gambaran ... |
For the next step lets make it more web look. Since this post don’t cover xhtml/css theme creating, so we’ll take some free theme from the net, for example freecsstemplates.org. We’ll take sparkling.
Copy unzipped theme to simpleblog folder. So the index.php and index.html which came from theme will be in the same folder.
Rename index.php to something else, index.old.php for example. Rename index.html to index.php. Copy and paste code below into your new index.php, before the html tag, you can paste it on line 13 :
1 2 3 4 5 6 7 8 9 10 11 | <?php include_once 'lib/MysqlConn.php'; $sql = new MysqlConn(); $str = "SELECT beritaId, beritaTitle, beritaDetail, beritaTime, nama from berita left join alumni On berita.beritaSender = alumni.id_alumni;"; $sql->execute($str); $data = $sql->getDataAssoc(); ?> |
on line 58 change the code into this:
1 2 3 4 5 6 7 8 9 10 11 | <div class="post"> <?php foreach($data as $itm) {?> <h2 class="title"><a href="#"><?php echo "$itm[beritaTitle]";?></a></h2> <p class="meta"><span class="date"><?php echo "$itm[beritaTime]";?></span><span class="posted">Posted by <a href="#"><?php echo "$itm[nama]";?></a></span></p> <div style="clear: both;">&nbsp;</div> <div class="entry"> <p><?php echo "$itm[beritaDetail]";?></p> <p class="links"><a href="#">Read More</a></p> </div> <?php } ?> </div> |
As you can see from the code above, the line which write the content of the post (post body) was line 7. The code just barely write all content body. We’ll change the code, so just some of the content will shown on the front page. Replace with this one:
1 |
When you click read more, still nothing happen yet. We’ll make one php file again to handle full post body.