User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 21 Oct 2002, 18:37   #1
laney
Queen of England
 
Join Date: Apr 2000
Location: not where you think
Posts: 40
laney has a spectacular aura aboutlaney has a spectacular aura aboutlaney has a spectacular aura about
php weirdo problem help me

right, i wrote this function:
Code:
//exQuery - performs mysql queries and sanitises data
function exQuery($query) {
	$arguments = func_get_args();
	$query = str_replace('%', chr(1), $query);

	for ($i = 1, $argnum = sizeof($arguments); $i < $argnum; $i++) {
		$query = sprintf(substr_replace($query, '%', strpos($query, chr(1)), 1), mysql_escape_string($arguments[$i]));
	}

	return mysql_query($query, $this->db)
		or die('error ' . mysql_errno() . ' @ mysql_query: ' . mysql_error() . ' (' . $query . ')');
}
basically it takes a variable number of arguments and does a mysql_query with their escaped forms.. the problem is that it always returns a bool value and not a resource..

it would be called as such
$query = exQuery('SELECT username FROM users WHERE username = \'%s\', $user);

anyone know why it's doing this?!
__________________
hello
laney is offline   Reply With Quote
Unread 21 Oct 2002, 18:52   #2
BesigedB
Darling
 
BesigedB's Avatar
 
Join Date: Dec 2000
Location: Edinburgh
Posts: 890
BesigedB is a glorious beacon of lightBesigedB is a glorious beacon of lightBesigedB is a glorious beacon of lightBesigedB is a glorious beacon of lightBesigedB is a glorious beacon of light
laney feels wanted so i thought i would post here to falsely get his hopes up.


(i dont know)
__________________
..
BesigedB is offline   Reply With Quote
Unread 22 Oct 2002, 07:29   #3
mogrika
Guest
 
Posts: n/a
i'll just titter at laneys phpness.

hehehehe.
  Reply With Quote
Unread 22 Oct 2002, 09:55   #4
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
Not 100% sure (and my lovely university decide that amongst the things student dont need, a webserver with PHP and a mysql db server are top of the list) and I can't check, but perhaps some debugging (ignore me if youve already tried this)

Code:
$res = mysql_query($query, $this->db)
		or die('error ' . mysql_errno() . ' @ mysql_query: ' . mysql_error() . ' (' . $query . ')');
var_dump($res);
return $res;
I cant see (off the top of my head) why it is doing it.
....
Got it (I think). Normally variables are returned by value, since this isnt a value, but a reference to a resource, perhaps returnign by reference may solve your problems.

Code:
//exQuery - performs mysql queries and sanitises data
function &exQuery($query) {
	$arguments = func_get_args();
	$query = str_replace('%', chr(1), $query);

	for ($i = 1, $argnum = sizeof($arguments); $i < $argnum; $i++) {
		$query = sprintf(substr_replace($query, '%', strpos($query, chr(1)), 1), mysql_escape_string($arguments[$i]));
	}

	$res = mysql_query($query, $this->db)
		or die('error ' . mysql_errno() . ' @ mysql_query: ' . mysql_error() . ' (' . $query . ')');
        return $res;
}

$result =& exQuery( .. );
I may be COMPLETELY wrong, but I don't care.
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 24 Oct 2002, 04:11   #5
Breed
Albatross!
 
Join Date: Mar 2000
Location: Oslo
Posts: 14
Breed is an unknown quantity at this point
Re: php weirdo problem help me

Quote:
Originally posted by iainlane
right, i wrote this function:
Code:
//exQuery - performs mysql queries and sanitises data
function exQuery($query) {
	$arguments = func_get_args();
	$query = str_replace('%', chr(1), $query);

	for ($i = 1, $argnum = sizeof($arguments); $i < $argnum; $i++) {
		$query = sprintf(substr_replace($query, '%', strpos($query, chr(1)), 1), mysql_escape_string($arguments[$i]));
	}

	return mysql_query($query, $this->db)
		or die('error ' . mysql_errno() . ' @ mysql_query: ' . mysql_error() . ' (' . $query . ')');
}
basically it takes a variable number of arguments and does a mysql_query with their escaped forms.. the problem is that it always returns a bool value and not a resource..

it would be called as such
$query = exQuery('SELECT username FROM users WHERE username = \'%s\', $user);

anyone know why it's doing this?!

Amateurs...

Quote:
return mysql_query($query, $this->db)
or die('error ' . mysql_errno() . ' @ mysql_query: ' . mysql_error() . ' (' . $query . ')');
That will never work.
Code:
$returnquery=mysql_query($query, $this->db)
		or die('error ' . mysql_errno() . ' @ mysql_query: ' . mysql_error() . ' (' . $query . ')');
return $returnquery;
This will..
Simply because otherwise you only return the status of the query. (bool value).
If you want to know why go read a bit more about the mysql_query() function and how you should init functions properly if you want all the instances you use inside functions to be accessible outside. (Not a good idea).

Oh.. and thats not a very efficient function anyway.
But best of luck

Chriso aka Breed
__________________
.........................
Any kiddie in school can love like a fool,
But Hating, my boy, is an Art.
-- Ogden Nash
Breed is offline   Reply With Quote
Unread 26 Oct 2002, 20:09   #6
Dudels
Guest
 
Posts: n/a
The problem lies in your main code, if it is how I suspect.


Breed speaks the truth, pay heed to his words.


Now then, I presume exQuery will be a sort of drop-in replacement for a normal query call.

In which case, use it as such.

ie.
Code:
$q = "SELECT `YouKnowWhat` FROM `YouKnowWhere` WHERE ID>6";

$array = mysql_fetch_array(exQuery($q));
I use a very similar method for MrDal; it should work.


Also, would it not be better to use func_num_args() instead of using sizeOf() on the arguments variable?
  Reply With Quote
Unread 30 Oct 2002, 21:23   #7
Breed
Albatross!
 
Join Date: Mar 2000
Location: Oslo
Posts: 14
Breed is an unknown quantity at this point
Quote:
Originally posted by Dudels

Breed speaks the truth, pay heed to his words.


That should be a general rule

Quote:
Also, would it not be better to use func_num_args() instead of using sizeOf() on the arguments variable?
Yes it would. But he uses func_get_args() allready and sizeOf() is faster.
(alltough there are reasons for using the slower method func_num_args() when accessing the args sent to the functions)

Chriso aka Breed
__________________
.........................
Any kiddie in school can love like a fool,
But Hating, my boy, is an Art.
-- Ogden Nash
Breed is offline   Reply With Quote
Unread 31 Oct 2002, 13:13   #8
Dudels
Guest
 
Posts: n/a
Quote:
Originally posted by Breed

allready and sizeOf() is faster.
Fair enough then.
  Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 22:45.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018