Smipple is shutting down Nov 30, 2017. Some features are currently disabled.
Profile image for John Dave Decano johndavedecano
Create database connection and retrieve datas from your mysql database.
Language
PHP
Tags
class database

My first database class

1 <?php 2 3 /** 4 * @author phpdesigner 5 * @copyright 2011 6 */ 7 8 class Database { 9 10 private $host; 11 private $user; 12 private $pass; 13 private $dbna; 14 public $link; 15 16 function __construct($host,$user,$pass){ 17 18 $this->host = $host; 19 $this->user = $user; 20 $this->pass = $pass; 21 $this->link = mysql_connect($this->host,$this->user,$this->pass) or die(mysql_error()); 22 } 23 function select_db($dbna){ 24 25 $this->dbna = $dbna; 26 return mysql_select_db($this->dbna,$this->link); 27 28 } 29 30 function returnArray($query_var){ 31 32 if(mb_substr($query_var,0,6) == 'SELECT'){ 33 34 $query = mysql_query($query_var); 35 while($row = mysql_fetch_array($query)){ 36 $array[] = $row; 37 } 38 39 return $array; 40 41 } else { die('SELECT is not found as part of your argument!');} 42 43 } 44 45 function close(){ 46 47 return mysql_close($this->link); 48 49 } 50 } 51 ?> 52 <?php 53 // USAGE 54 $db = new Database('localhost','root','password'); 55 $db->select_db('my_database'); 56 $pages = $db->returnArray('SELECT * FROM pages ORDER BY position'); 57 ?> 58 <ul id="sortablelists"> 59 <?php foreach($pages as $page):?> 60 <li id="item_<?php echo $page['id'];?>"><?php echo $page['title'];?></li> 61 <?php endforeach;?> 62 </ul>

Comments