Jump to content

Retrieve MYSQL DB information from a class


waverider303

Recommended Posts

I have this site and the information on the page is coming from a database. I want to create a class so on each page I can just call the class to put the correct information on that page.

 

IE: Instead of writing this on all the pages

 

<?php
      include('connect.php');
      $sql = "SELECT * FROM db_name WHERE db_row = '$variable'";
      $res = mysql_query($sql);
      while($row = mysql_fetch_assoc($res)) {
            $page_content = $row['page_content'];
      }
?>

 

I could intune write just a require_once(''); statement and it will do it all.

couldn't you just put this:

 

<?php
$sql = "SELECT * FROM db_name WHERE db_row = '$variable'";
      $res = mysql_query($sql);
      while($row = mysql_fetch_assoc($res)) {
            $page_content = $row['page_content'];
      }
?>

 

into connect.php and include that on every page?  instead of having to rewrite/convert, etc. anything/everything to accommodate a class when one is not really necessary at this time.  and if you don't know how to write a class to begin with, you're not ready to take advantage of what OOP has to offer anyways, so it's best to just stick with regular includes, etc., and read up on classes and OOP, then implement it when you have a working knowledge of it.

 

or am i missing the point entirely?

Actually one question I have is if i place this:

<?php
      $sql = "SELECT * FROM db_name WHERE db_row = '$variable'";
      $res = mysql_query($sql);
      while($row = mysql_fetch_assoc($res)) {
            $page_content = $row['page_content'];
      }
?>

 

inside the connect.php files and include it on my pages. How would I change $variable depending on the page?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.