|
21 Oct 2002, 18:37
|
#1
|
Queen of England
Join Date: Apr 2000
Location: not where you think
Posts: 40
|
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
|
|
|
21 Oct 2002, 18:52
|
#2
|
Darling
Join Date: Dec 2000
Location: Edinburgh
Posts: 890
|
laney feels wanted so i thought i would post here to falsely get his hopes up.
(i dont know)
|
|
|
22 Oct 2002, 07:29
|
#3
|
Guest
|
i'll just titter at laneys phpness.
hehehehe.
|
|
|
22 Oct 2002, 09:55
|
#4
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
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
|
|
|
24 Oct 2002, 04:11
|
#5
|
Albatross!
Join Date: Mar 2000
Location: Oslo
Posts: 14
|
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
|
|
|
26 Oct 2002, 20:09
|
#6
|
Guest
|
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?
|
|
|
30 Oct 2002, 21:23
|
#7
|
Albatross!
Join Date: Mar 2000
Location: Oslo
Posts: 14
|
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
|
|
|
31 Oct 2002, 13:13
|
#8
|
Guest
|
Quote:
Originally posted by Breed
allready and sizeOf() is faster.
|
Fair enough then.
|
|
|
|
All times are GMT +1. The time now is 02:46.
| |