Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/250734-how-to-use-this-db-connector/
Share on other sites

  • 3 weeks later...
<?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>';
	  }
?>

  • 3 weeks later...

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>';
	  }
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.