Jump to content


MySql class


7 replies to this topic

#1 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 11 December 2007 - 11:53 PM

I have this class... the problem is when i go to the webpage all it does is copy the code that's in the class. Please take a look at it and tell me whats wrong.

class MySql{

	function Connect($host, $name, $pass, $db){
		$connection = mysql_connect("$host", "$name", "$pass");
		mysql_select_db("$db", $connection) or die(mysql_error());
	}

	function Close(){
		mysql_close($this->connection);
	}

	function FetchRow($query){
		$rows = mysql_fetch_row($query);
		return $rows;
	}

	function FetchArray($query){
		$array = mysql_fetch_array($query);
		return $array;
	}

	function FetchNum($query){
		$num = mysql_num_rows($query);
		return $num;
	}

	function Query($sql){
		$query = mysql_query($sql) or die(mysql_error());
		return $query;
	}

}

Edited by Korndawg, 13 December 2007 - 03:59 PM.


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 12 December 2007 - 12:43 AM

What do you mean by "copy the code that's in the class." Last i checked, simply visiting a page couldn't "copy" code... Now, it could display it if you didn't add the opening/closing php tags, but that's not copying.

p.s. Don't want to sound rude or anything, but that class would be a little more useful if you used it in a more meaningful way, such as passing $this->connection to every mysql_* function in their repsected areas. Also, if you can avoid it, don't put variables in quotes for no reason. It is a performance drag (obviously it won't be noticable in something like this, but i'm a nut when it comes to that stuff).

#3 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 12 December 2007 - 04:06 AM

This is what I mean by "copy the code" it seems to have a hard time w/ the $this->connection snippet.

Quote

connection); } function FetchRow($query){ $rows = mysql_fetch_row($query); return $rows; } function FetchArray($query){ $array = mysql_fetch_array($query); return $array; } function FetchNum($query){ $num = mysql_num_rows($query); return $num; } function Query($sql){ $query = mysql_query($sql) or die(mysql_error()); return $query; } } ?>
Fatal error: Class 'MySql' not found in C:\wamp\www\test\panelinfo.php on line 21

LINE 21 just says: $sql = new mysql();

rc69, please be as blunt as you wish. I need all the advice I can take. Right now I just want to get a class working. I've used classes in C++ and I thought I would try it out in PHP. This script is actually one I got off of a tutorial site here at P2L. I figure since classes are OOP, the site would load faster. Please correct me if I am wrong. I will make the changes once I can get this up and running.

Thanks for the response!

EDIT: I just removed $this and it got rid of the displaying code part. But it still has a problem with building the class object ($sql). But to call functions inside the class wouldnt I need to use $this?

Edited by Korndawg, 12 December 2007 - 04:20 AM.


#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 December 2007 - 03:30 PM

From the looks of it, you're not including the file properly. Could you show me the panelinfo.php file from line 21 up?

The problem with assuming OOP is faster in any case is that it is more complex. Programs are inherently procedural (strictly speaking, php is procedural, it's OOP extension is considered far from true OOP). Writing anything in OOP is just an easier way to manage the code, and in some languages, it maybe true that it runs faster that way. In PHP, i doubt it though.

One benifit of mysql classes is error handling. The rest of it is just fluff and only serves to create extra overhead, thus slowing things down the way you are doing it. As-is, connect() and query() are the only usefull functions there. The rest are pointless as they simply return the result of a function. If you're aiming at speeding things up, then you should know that the more calls to user-defined functions you have, the slower things will be. If you called the rest of the mysql_* functions directly, or changed the class to alter the functionality of those functions, then you would be in a decent place to use a class like that for anything but the query() error handling.

Lastly, yes, you need $this to reference class variables/methods in php.

#5 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 13 December 2007 - 03:54 PM

panelinfo.php
include 'class.php';
	
//Assigns navigation information to an array
	$adminnav = array('<a href="index.php">Home</a>', 
														  '<a href="index.php?page=news">News</a>', 
					  '<a href="index.php?page=products">Products</a>', 
					  '<a href="index.php?page=pictures">Pictures</a>', 
					  '<a href="index.php?page=stats">Statistics</a>');
	$adminnav = implode(" | ", $adminnav);


	$host = 'localhost';
				$name = '*****';
				$pass = '*****';
				$db = '*****';
	
		//$sql = new MySql;
		  //$connection = $sql->Connect($host, $name, $pass, $db);

lol I understand this class sucks. I just want to get it running, so I can build my own classes later.

Edited by Korndawg, 13 December 2007 - 03:59 PM.


#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 December 2007 - 07:03 PM

All right, i want to start from the beginning.

View Postrc69, on Dec 11 2007, 10:43 PM, said:

Now, it could display it if you didn't add the opening/closing php tags, but that's not copying.

So, this is what you see in the browser:

Quote

connection); } function FetchRow($query){ $rows = mysql_fetch_row($query); return $rows; } function FetchArray($query){ $array = mysql_fetch_array($query); return $array; } function FetchNum($query){ $num = mysql_num_rows($query); return $num; } function Query($sql){ $query = mysql_query($sql) or die(mysql_error()); return $query; } } ?>
Fatal error: Class 'MySql' not found in C:\wamp\www\test\panelinfo.php on line 21

LINE 21 just says: $sql = new mysql();

Which coincidentally seems to cut off the following:
class MySql{

	function Connect($host, $name, $pass, $db){
		$connection = mysql_connect("$host", "$name", "$pass");
		mysql_select_db("$db", $connection) or die(mysql_error());
	}

	function Close(){
		mysql_close($this->
Notice, the greater than sign on the end (a.k.a. the same simple that closes html tags).

I'm willing to put money on the fact that you start that class file with just a "<" and either forgot the question mark, or don't have short-tags enabled and forgot the "php" part as well.

#7 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 13 December 2007 - 07:54 PM

Like always your a genious rc69... WAMP wont allow regular <? tags, which I don't understand, but I fixed it. Problem solved thank you very much.

#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 December 2007 - 12:32 AM

View Postrc69, on Dec 13 2007, 05:03 PM, said:

...or don't have short-tags enabled and forgot the "php" part as well.
http://php.net/language.basic-syntax





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users