OOPing with PHP4 simple DB connect class?

Reply

Join Date: Nov 2007
Posts: 86
Reputation: sagedavis is an unknown quantity at this point 
Solved Threads: 6
sagedavis sagedavis is offline Offline
Junior Poster in Training

OOPing with PHP4 simple DB connect class?

 
0
  #1
Mar 27th, 2008
I am banging my head against the wall here.
I've tried everything that I can think of to get this to work.

  1. class myData {
  2. function myStuff($sql){
  3. $result = @mysql_query($sql,$connection) or die(mysql_error());
  4. while ($row = mysql_fetch_array($result)) {
  5. $adId = $row['adId'];
  6. $adName = $row['adName'];
  7. $adData .= "my adId is $adId and guess what? my adName is $adName <br/><br/>";
  8.  
  9. }
  10. }
  11. }

Note, I am also using a "require_once" of a file which holds variables for
  1. $connection = mysql_connect("localhost","maDB","maPassword") or die(mysql_error());
  2. $db_name = "myDB";
  3. $db = @mysql_select_db($db_name,$connection) or die(mysql_error);

in my index page I have the following

  1. sql = "select * from tablename";
  2. $objAdvertisers = new myData;
  3. $objAdvertisers->myStuff($sql);

Using this method keeps giving me an error of
unexpected T_CLASS...

I've tried several other things, but I was almost sure that this was the way that it was sposed to work?

All I am trying to do is create a generic connect to database and pass $sql as an argument, then store some information based on that as an array for each record that matches my request.

I am using a PHP4 server.

Any help?
Thanks
Sage
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 154
Reputation: Suomedia is an unknown quantity at this point 
Solved Threads: 19
Suomedia Suomedia is offline Offline
Junior Poster

Re: OOPing with PHP4 simple DB connect class?

 
0
  #2
Mar 28th, 2008
There is nothing in the code that you posted that is actually causing the error.

Look at my tutorial here which has a nice introduction to using database classes.


Matti Ressler
Suomedia
If you want your dreams to come true, the first thing you must do is to wake up....
Suomedia - Dynamic Content Management
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 86
Reputation: sagedavis is an unknown quantity at this point 
Solved Threads: 6
sagedavis sagedavis is offline Offline
Junior Poster in Training

Re: OOPing with PHP4 simple DB connect class?

 
0
  #3
Mar 28th, 2008
Thanks for this tute, it's pretty good, although, I wouldn't recommend a beginning OOPer to read that right off the bat.

I've been working with PHP OOPing for only a short time, but, as it's a daily requirement in my job, I find your stuff easier to follow than our own docs at work.

The only thing is that I have never seen anything like line 17 before.
  1. function flowSQL($db_server = DATABASE_SERVER, $db_user = DB_USERNAME, $db_pass = DB_PASSWORD, $database = DATABASE)
is this a PHP5 thing?
I don't have access to 5 at the moment, err... scratch that.. I don't have access on the server that I am creating this for, to PHP5.

I am assuming that, this line must stay as is, however, we need to use
  1. define('DATABASE_SERVER', 'localhost');

another item I am assuming is PHP5 which does not work in 4, as I have never seen it in a PHP4 script.

further assumption is that, if I actually did all the defines, that this would globally define these items as long as I use some require page.php.

At which point I would be able to do something like
  1. $query = "SELECT needle FROM haystack WHERE hay = yellow";
  2. $myQuery = new flowSQL;
  3. $myQuery->flowSQL();
  4. $myQuery->query($query);

or is it even needed to use the second to last line?

Thanks again for your help
Sage
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,087
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: OOPing with PHP4 simple DB connect class?

 
0
  #4
Mar 28th, 2008
Originally Posted by sagedavis View Post
I am banging my head against the wall here.
I've tried everything that I can think of to get this to work.
....

in my index page I have the following

  1. sql = "select * from tablename";
  2. $objAdvertisers = new myData;
  3. $objAdvertisers->myStuff($sql);

Using this method keeps giving me an error of
unexpected T_CLASS...
...
Did you forget the $ before sql in the code above?

  1. $sql = "select * from tablename";
  2. $objAdvertisers = new myData;
  3. $objAdvertisers->myStuff($sql);

That would probably generate a T_STRING error...

As for the T_CLASS error, make sure you didn't forget to close open braces () or {} or are missing a ; at the end of your statements just before the class definition.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 154
Reputation: Suomedia is an unknown quantity at this point 
Solved Threads: 19
Suomedia Suomedia is offline Offline
Junior Poster

Re: OOPing with PHP4 simple DB connect class?

 
0
  #5
Mar 28th, 2008
Originally Posted by sagedavis View Post

The only thing is that I have never seen anything like line 17 before.
  1. function flowSQL($db_server = DATABASE_SERVER, $db_user = DB_USERNAME, $db_pass = DB_PASSWORD, $database = DATABASE)
is this a PHP5 thing?
No its not. It uses defines, eg. DATABASE_SERVER that are defined in the configuration file.

Originally Posted by sagedavis View Post
I don't have access to 5 at the moment, err... scratch that.. I don't have access on the server that I am creating this for, to PHP5.

I am assuming that, this line must stay as is, however, we need to use
  1. define('DATABASE_SERVER', 'localhost');
Thats exactly as I have it in the tutorial configuration file. The example given in the tutorial is pretty standard for any version of PHP.

Originally Posted by sagedavis View Post

further assumption is that, if I actually did all the defines, that this would globally define these items as long as I use some require page.php.
Yes, you will see in the tutorial that your main page has:

  1. // include the configuration file and database class
  2. require('include/config.php');
  3. require('include/class/mysql.php');

Originally Posted by sagedavis View Post
At which point I would be able to do something like
  1. $query = "SELECT needle FROM haystack WHERE hay = yellow";
  2. $myQuery = new flowSQL;
  3. $myQuery->flowSQL();
  4. $myQuery->query($query);

or is it even needed to use the second to last line?
Its better structured like this (less confusing):

  1. $dBase = new flowSQL;
  2. $query = $dBase->query("SELECT needle FROM haystack WHERE hay = yellow");

We still need to fetch our result however, eg:

  1. while($result = $dBase->fetch_array($query)) {
  2. // do stuff
  3. }


Matti Ressler
Suomedia
If you want your dreams to come true, the first thing you must do is to wake up....
Suomedia - Dynamic Content Management
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 86
Reputation: sagedavis is an unknown quantity at this point 
Solved Threads: 6
sagedavis sagedavis is offline Offline
Junior Poster in Training

Re: OOPing with PHP4 simple DB connect class?

 
0
  #6
Mar 28th, 2008
Great Matti,
I shall try this over the weekend.

The main reason for wanting to pass the var $sql as an arg, rather than type out the statement is because, there might be some dynamic stuff going on there as well, as in the case of say a search function, with advanced features.

At any rate, I shall dig into your tute more deeply this weekend.

Thanks
Sage
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum


Views: 951 | Replies: 5
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC