Jump to content


SQL query function not working


10 replies to this topic

#1 Hit3k

    Young Padawan

  • Members
  • Pip
  • 120 posts
  • Gender:Male
  • Location:Australia

Posted 29 June 2006 - 01:00 AM

Ok so I've been starting to experiment with OOP and I created a function that performs a query
class sql {
	function conn($host, $user, $pass, $db) {
		$this->host = $host;
		$this->username = $user;
		$this->password = $pass;
		$this->database = $db;
		$this->dbh=mysql_connect ("{$this->host}", "{$this->username}", "{$this->password}") or die ('I cannot connect to the database because: ' . mysql_error());
		mysql_select_db ("{$this->database}");
	}
//This one
	function query($query) {
		$this->query = mysql_query($query);
	}
}
and this is the code on the code that performs it
$q = sql::query("SELECT * FROM bofh ORDER BY rand() LIMIT 1");
if ($e = $q) {
	while($r = mysql_fetch_array($e)) {
		echo $r['id'] . "<br />";
		echo $r['excuse'] . "<br />";
	}
}
and it doesnt return anything It is connecting and selecting the DB but it wont run the query any help would be greatly appreciated :)

#2 DoctorPhotoshop

    Young Padawan

  • Members
  • Pip
  • 67 posts

Posted 29 June 2006 - 01:12 AM

I don't mean to get in the way of your problem, but could you explain $this->username and what it means.
I've also seen $variable->something done with mySQL. Could you explain it or point me to an explaination.

#3 Hit3k

    Young Padawan

  • Members
  • Pip
  • 120 posts
  • Gender:Male
  • Location:Australia

Posted 29 June 2006 - 01:25 AM

Its just how I defined the variables in the class thats all you can do $variable->whatever as well in an outside function

#4 dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 29 June 2006 - 09:12 AM

Okay in your query function it deosn't do anything because thing is returned. So try something like this:

//This one
	function query($query) {
		$this->query = mysql_query($query);
		return $this->query; //NOTE: returnes your sql query
	}
}

Also I don't see why you have :: after instead of doing it like this:

$q = sql->query("SELECT * FROM bofh ORDER BY rand() LIMIT 1");

Hope this helps.

dEcade

#5 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 29 June 2006 - 10:33 AM

View PostDoctorPhotoshop, on Jun 29 2006, 07:12 AM, said:

I've also seen $variable->something done with mySQL

Just a bit of backup info for that question, you use that when you have used mysql_fetch_object.

#6 rc69

    PHP Master PD

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

Posted 29 June 2006 - 12:25 PM

the "->" is simply php's way of seperating objects and methods. Similarly, javascript uses a period to seperate their objects.

Decade, you might have noticed he never actually defined an object. So i'm assuming that in php "sql::query()" would work like:
$foo = new sql;
$foo->query();
I remember reading something about it in the manual, and i don't remember it saying anything like that... but oh well.

Other then the return thing, i think you should shorten your connect function, as storing all of your secure info in the object is not a good idea (plus, you're using to many quotes :P)
class sql {
	function conn($host, $user, $pass, $db) {
		$this->dbh=mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
		mysql_select_db ($db);
	}
}
I guess you could keep the quotes, i just find them to be a waste of space.

Anywho, if you want a reference for mysql and OOP, there is a tutorial on my site about it (not saying it's a great tutorial, but it's there) :)

#7 DoctorPhotoshop

    Young Padawan

  • Members
  • Pip
  • 67 posts

Posted 29 June 2006 - 01:44 PM

View PostHit3k, on Jun 29 2006, 06:25 AM, said:

Its just how I defined the variables in the class thats all you can do $variable->whatever as well in an outside function


is there any real differenct from


$variable->whatever

and

$somevariable

?

#8 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 29 June 2006 - 02:26 PM

You don't use $x -> y; just anywhere, its class based its defining a variable within that class.

when using it with mysql_fetch_object it would be like....

while( $row = mysql_fetch_object($query) )
{
	 $row -> field;
	 $row -> field2;

	 /* Just the same as $row['field'] when using mysql_fetch_array  */
}


#9 DoctorPhotoshop

    Young Padawan

  • Members
  • Pip
  • 67 posts

Posted 29 June 2006 - 03:23 PM

would this be the way to use it?

class Person
{


		 var $name;
		 var $age;

		 function setage($giveage) {
						  $age = $giveage;		  }

}

$bob = new Person;

$bob->setage(10);
echo $bob->age;
// 10

could you also do
$bob->age = 10;

Edited by DoctorPhotoshop, 29 June 2006 - 03:28 PM.


#10 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 29 June 2006 - 03:27 PM

Nearly right

class Person
{

	var $name;
	var $age;

	function setage($giveage)
	{

		$this->age = $giveage;


	}
}

	$bob = new Person;

	$bob->setage(10);
	
	echo $bob->age;
	// 10

And as for the last question, yes you can do that :P

Edited by .Matt, 29 June 2006 - 03:28 PM.


#11 DoctorPhotoshop

    Young Padawan

  • Members
  • Pip
  • 67 posts

Posted 29 June 2006 - 03:35 PM

thanks

:P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users