peppericious Posted November 8, 2011 Share Posted November 8, 2011 I want to add a query to a page to retrieve blog posts from a table I've added to the db . At the top of the page in an include, the following db connector (not made by me) is already in use: <?php class Database { private $conn; function __construct() { $this->conn = mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("thedatabase", $this->conn); } function query($sql) { $result = mysql_query($sql, $this->conn); if(!$result) { die(mysql_error()); } else { return $result; } } } ?> I've not used classes before, nor have I used functions of the type in the code snippet above so I'll admit I'm fumbling around. If I were using a more basic connector, the query I would run would be something like this: <?php $q = "SELECT post_id, post_title, post_subtitle, post_body, DATE_FORMAT(post_created, '%M %D %Y') AS created_on, post_ext_link FROM blog ORDER BY post_id DESC LIMIT 3"; $r = @mysqli_query($conn, $q); if (mysqli_num_rows($r) > 0) { while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo "<h2>" . $row['post_title'] . "</h2>" . "<h3" . $row['post_subtitle'] . "</h3>" . "<p" . $row['post_body'] . "</p>" . "<p" . $row['created_on'] . "</p>" ; } } else { echo '<p>No posts to display at present.</p>'; } ?> Could someone kindly oblige me by telling me what changes I must make to the query so that it can make use of the connector already in use on the page? Hope my question makes sense... All/any help appreciated. Quote Link to comment Share on other sites More sharing options...
Spring Posted November 29, 2011 Share Posted November 29, 2011 <?php $db = new Database(); $q = "SELECT post_id, post_title, post_subtitle, post_body, DATE_FORMAT(post_created, '%M %D %Y') AS created_on, post_ext_link FROM blog ORDER BY post_id DESC LIMIT 3"; $r = $db->query($q); if (mysqli_num_rows($r) > 0) { while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo "<h2>" . $row['post_title'] . "</h2>" . "<h3" . $row['post_subtitle'] . "</h3>" . "<p" . $row['post_body'] . "</p>" . "<p" . $row['created_on'] . "</p>" ; } } else { echo '<p>No posts to display at present.</p>'; } ?> Quote Link to comment Share on other sites More sharing options...
jcbones Posted December 16, 2011 Share Posted December 16, 2011 You will have to use the mysql functions and not mysqli, as you cannot mix the two. <?php $db = new Database(); $q = "SELECT post_id, post_title, post_subtitle, post_body, DATE_FORMAT(post_created, '%M %D %Y') AS created_on, post_ext_link FROM blog ORDER BY post_id DESC LIMIT 3"; $r = $db->query($q); if (mysql_num_rows($r) > 0) { while ($row = mysql_fetch_assoc($r)) { echo "<h2>" . $row['post_title'] . "</h2>" . "<h3" . $row['post_subtitle'] . "</h3>" . "<p" . $row['post_body'] . "</p>" . "<p" . $row['created_on'] . "</p>" ; } } else { echo '<p>No posts to display at present.</p>'; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.