Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Development
[DEV] simpleCMS [PHP / MySQL]
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="marles" data-source="post: 311940" data-attributes="member: 2333"><p>Here's a simple class to manage your database.</p><p></p><p>[PHP]<?php</p><p></p><p> class Database {</p><p></p><p> protected $_con;</p><p> protected $_query = '';</p><p> protected $_where = array();</p><p> protected $_whereTypeList = '';</p><p> protected $_paramTypeList = '';</p><p></p><p> protected $_bindParams = array('');</p><p> public $queries_count;</p><p></p><p> public function __construct($username = '',$password = '',$host = '',$database = '') {</p><p> $this->con = new mysqli($host,$username,$password,$database);</p><p> $this->queries_count = 0;</p><p> }</p><p></p><p> /**</p><p> * Reset states after an execution</p><p> * @return object Returns the current instance.</p><p> */</p><p> protected function reset()</p><p> {</p><p> $this->_where = array();</p><p> $this->_bindParams = array(''); // Create the empty 0 index</p><p> $this->_query = '';</p><p> $this->_whereTypeList = '';</p><p> $this->_paramTypeList = '';</p><p> }</p><p></p><p> /**</p><p> *</p><p> * @param string $query Contains a user-provided select query.</p><p> * @param int $numRows The number of rows total to return.</p><p> * @return array Contains the returned rows from the query.</p><p> */</p><p> public function query($query, $numRows = NULL)</p><p> {</p><p> $this->_query = filter_var($query, FILTER_SANITIZE_STRING);</p><p> $stmt = mysqli_stmt_init($this->con);</p><p> $stmt->prepare($query);</p><p> $stmt->execute();</p><p> $this->reset();</p><p></p><p> $results = $stmt->get_result();</p><p> $this->queries_count = $this->queries_count + 1;</p><p> return $results;</p><p> }</p><p></p><p> }</p><p>?>[/PHP]</p><p></p><p>Call this</p><p>[CODE]<?php</p><p>require 'connect.php';</p><p></p><p>// Create's Connection</p><p>$conn = new Database($db_host, $db_user, $db_pass, $db_database);</p><p></p><p>// Gets into database & posts data.</p><p>$sql = "SELECT id, username FROM users";</p><p>$result = $conn->query($sql);</p><p></p><p>if ($result->num_rows > 0) {</p><p> // Data is now posted.</p><p> while($row = $result->fetch_assoc()) {</p><p> echo 'User ID: <b>' . $row["id"] . '</b> - Username: <b><a href="' . $row["username"] . '.php">' . $row["username"] . '</a></b><br />';</p><p> }</p><p>} else {</p><p> echo "There are no registered users!";</p><p> }</p><p>?>[/CODE]</p><p></p><p>Don't know if it's secure or not... Been using it in my project...</p></blockquote><p></p>
[QUOTE="marles, post: 311940, member: 2333"] Here's a simple class to manage your database. [PHP]<?php class Database { protected $_con; protected $_query = ''; protected $_where = array(); protected $_whereTypeList = ''; protected $_paramTypeList = ''; protected $_bindParams = array(''); public $queries_count; public function __construct($username = '',$password = '',$host = '',$database = '') { $this->con = new mysqli($host,$username,$password,$database); $this->queries_count = 0; } /** * Reset states after an execution * @return object Returns the current instance. */ protected function reset() { $this->_where = array(); $this->_bindParams = array(''); // Create the empty 0 index $this->_query = ''; $this->_whereTypeList = ''; $this->_paramTypeList = ''; } /** * * @param string $query Contains a user-provided select query. * @param int $numRows The number of rows total to return. * @return array Contains the returned rows from the query. */ public function query($query, $numRows = NULL) { $this->_query = filter_var($query, FILTER_SANITIZE_STRING); $stmt = mysqli_stmt_init($this->con); $stmt->prepare($query); $stmt->execute(); $this->reset(); $results = $stmt->get_result(); $this->queries_count = $this->queries_count + 1; return $results; } } ?>[/PHP] Call this [CODE]<?php require 'connect.php'; // Create's Connection $conn = new Database($db_host, $db_user, $db_pass, $db_database); // Gets into database & posts data. $sql = "SELECT id, username FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { // Data is now posted. while($row = $result->fetch_assoc()) { echo 'User ID: <b>' . $row["id"] . '</b> - Username: <b><a href="' . $row["username"] . '.php">' . $row["username"] . '</a></b><br />'; } } else { echo "There are no registered users!"; } ?>[/CODE] Don't know if it's secure or not... Been using it in my project... [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Development
[DEV] simpleCMS [PHP / MySQL]
Top