$_GET request -> variable -> object? -> function
$_GET request -> variable -> object? -> function
am 13.04.2007 02:51:59 von zac.carey
I'm trying to extend a class (written by others) and was wondering if
there's a standard a way of passing a $_GET request to a class. Here's
what I have (see below). If I hardcode $where, something like
$where = " WHERE `firstname` = 'John' "
and then pass it as an argument into the scope of a 'select' function
then it works fine, but otherwise the query just appears to hang.
I'm deliberately not including the class or class extender at this
stage, but I'll add them to this thread later if required.
foreach ($_GET as $condition_key => $condition_value) {
$condition[] =" $condition_key = '$condition_value' ";
}
if(is_null($condition)){
$where = " WHERE 1 ";
}else{
$conditionString = implode('AND', $condition);
$where = " WHERE $conditionString ";
}
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 09:47:44 von zac.carey
On Apr 13, 1:51 am, "strawberry" wrote:
> I'm trying to extend a class (written by others) and was wondering if
> there's a standard a way of passing a $_GET request to a class. Here's
> what I have (see below). If I hardcode $where, something like
>
> $where = " WHERE `firstname` = 'John' "
>
> and then pass it as an argument into the scope of a 'select' function
> then it works fine, but otherwise the query just appears to hang.
>
> I'm deliberately not including the class or class extender at this
> stage, but I'll add them to this thread later if required.
>
> foreach ($_GET as $condition_key => $condition_value) {
> $condition[] =" $condition_key = '$condition_value' ";
> }
> if(is_null($condition)){
> $where = " WHERE 1 ";
> }else{
> $conditionString = implode('AND', $condition);
> $where = " WHERE $conditionString ";
> }
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 09:47:44 von zac.carey
On Apr 13, 1:51 am, "strawberry" wrote:
> I'm trying to extend a class (written by others) and was wondering if
> there's a standard a way of passing a $_GET request to a class. Here's
> what I have (see below). If I hardcode $where, something like
>
> $where = " WHERE `firstname` = 'John' "
>
> and then pass it as an argument into the scope of a 'select' function
> then it works fine, but otherwise the query just appears to hang.
>
> I'm deliberately not including the class or class extender at this
> stage, but I'll add them to this thread later if required.
>
> foreach ($_GET as $condition_key => $condition_value) {
> $condition[] =" $condition_key = '$condition_value' ";
> }
> if(is_null($condition)){
> $where = " WHERE 1 ";
> }else{
> $conditionString = implode('AND', $condition);
> $where = " WHERE $conditionString ";
> }
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 10:08:17 von Schraalhans Keukenmeester
strawberry wrote:
> On Apr 13, 1:51 am, "strawberry" wrote:
>> I'm trying to extend a class (written by others) and was wondering if
>> there's a standard a way of passing a $_GET request to a class. Here's
>> what I have (see below). If I hardcode $where, something like
>>
>> $where = " WHERE `firstname` = 'John' "
>>
>> and then pass it as an argument into the scope of a 'select' function
>> then it works fine, but otherwise the query just appears to hang.
>>
>> I'm deliberately not including the class or class extender at this
>> stage, but I'll add them to this thread later if required.
>>
>> foreach ($_GET as $condition_key => $condition_value) {
>> $condition[] =" $condition_key = '$condition_value' ";
>> }
>> if(is_null($condition)){
>> $where = " WHERE 1 ";
>> }else{
>> $conditionString = implode('AND', $condition);
>> $where = " WHERE $conditionString ";
>> }
>
>
You're reposing the question? Or ?
I am not sure myself which is preferrable: having the class handle the
$_GET superglobal array itself, which makes the client code programming
even easier, or leaving it to the client code, allowing for more custom
use. I've seen both examples, each has their practical (dis-)advantages.
If someone has a good argument on why to prefer one over the other (or
even an entirely different way) I'd also be helped!
Sh.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 10:08:17 von Schraalhans Keukenmeester
strawberry wrote:
> On Apr 13, 1:51 am, "strawberry" wrote:
>> I'm trying to extend a class (written by others) and was wondering if
>> there's a standard a way of passing a $_GET request to a class. Here's
>> what I have (see below). If I hardcode $where, something like
>>
>> $where = " WHERE `firstname` = 'John' "
>>
>> and then pass it as an argument into the scope of a 'select' function
>> then it works fine, but otherwise the query just appears to hang.
>>
>> I'm deliberately not including the class or class extender at this
>> stage, but I'll add them to this thread later if required.
>>
>> foreach ($_GET as $condition_key => $condition_value) {
>> $condition[] =" $condition_key = '$condition_value' ";
>> }
>> if(is_null($condition)){
>> $where = " WHERE 1 ";
>> }else{
>> $conditionString = implode('AND', $condition);
>> $where = " WHERE $conditionString ";
>> }
>
>
You're reposing the question? Or ?
I am not sure myself which is preferrable: having the class handle the
$_GET superglobal array itself, which makes the client code programming
even easier, or leaving it to the client code, allowing for more custom
use. I've seen both examples, each has their practical (dis-)advantages.
If someone has a good argument on why to prefer one over the other (or
even an entirely different way) I'd also be helped!
Sh.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 11:06:41 von zac.carey
On Apr 13, 9:08 am, Schraalhans Keukenmeester
wrote:
> strawberry wrote:
> > On Apr 13, 1:51 am, "strawberry" wrote:
> >> I'm trying to extend a class (written by others) and was wondering if
> >> there's a standard a way of passing a $_GET request to a class. Here's
> >> what I have (see below). If I hardcode $where, something like
>
> >> $where = " WHERE `firstname` = 'John' "
>
> >> and then pass it as an argument into the scope of a 'select' function
> >> then it works fine, but otherwise the query just appears to hang.
>
> >> I'm deliberately not including the class or class extender at this
> >> stage, but I'll add them to this thread later if required.
>
> >> foreach ($_GET as $condition_key => $condition_value) {
> >> $condition[] =" $condition_key = '$condition_value' ";
> >> }
> >> if(is_null($condition)){
> >> $where = " WHERE 1 ";
> >> }else{
> >> $conditionString = implode('AND', $condition);
> >> $where = " WHERE $conditionString ";
> >> }
>
> You're reposing the question? Or ?
>
No. I just wanted to get the thread into comp.lang.php too
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 11:06:41 von zac.carey
On Apr 13, 9:08 am, Schraalhans Keukenmeester
wrote:
> strawberry wrote:
> > On Apr 13, 1:51 am, "strawberry" wrote:
> >> I'm trying to extend a class (written by others) and was wondering if
> >> there's a standard a way of passing a $_GET request to a class. Here's
> >> what I have (see below). If I hardcode $where, something like
>
> >> $where = " WHERE `firstname` = 'John' "
>
> >> and then pass it as an argument into the scope of a 'select' function
> >> then it works fine, but otherwise the query just appears to hang.
>
> >> I'm deliberately not including the class or class extender at this
> >> stage, but I'll add them to this thread later if required.
>
> >> foreach ($_GET as $condition_key => $condition_value) {
> >> $condition[] =" $condition_key = '$condition_value' ";
> >> }
> >> if(is_null($condition)){
> >> $where = " WHERE 1 ";
> >> }else{
> >> $conditionString = implode('AND', $condition);
> >> $where = " WHERE $conditionString ";
> >> }
>
> You're reposing the question? Or ?
>
No. I just wanted to get the thread into comp.lang.php too
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 11:27:01 von Schraalhans Keukenmeester
strawberry wrote:
> On Apr 13, 9:08 am, Schraalhans Keukenmeester
> wrote:
>> strawberry wrote:
>>> On Apr 13, 1:51 am, "strawberry" wrote:
>>>> I'm trying to extend a class (written by others) and was wondering if
>>>> there's a standard a way of passing a $_GET request to a class. Here's
>>>> what I have (see below). If I hardcode $where, something like
>>>> $where = " WHERE `firstname` = 'John' "
>>>> and then pass it as an argument into the scope of a 'select' function
>>>> then it works fine, but otherwise the query just appears to hang.
>>>> I'm deliberately not including the class or class extender at this
>>>> stage, but I'll add them to this thread later if required.
>>>> foreach ($_GET as $condition_key => $condition_value) {
>>>> $condition[] =" $condition_key = '$condition_value' ";
>>>> }
>>>> if(is_null($condition)){
>>>> $where = " WHERE 1 ";
>>>> }else{
>>>> $conditionString = implode('AND', $condition);
>>>> $where = " WHERE $conditionString ";
>>>> }
>> You're reposing the question? Or ?
>>
>
>
> No. I just wanted to get the thread into comp.lang.php too
>
The hardcoded version has single quotes around key and value, your $_GET
parsed version hasn't in this example. Could that be the cause? Does the
query run with results if NO conditions are set?
Sh.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 11:27:01 von Schraalhans Keukenmeester
strawberry wrote:
> On Apr 13, 9:08 am, Schraalhans Keukenmeester
> wrote:
>> strawberry wrote:
>>> On Apr 13, 1:51 am, "strawberry" wrote:
>>>> I'm trying to extend a class (written by others) and was wondering if
>>>> there's a standard a way of passing a $_GET request to a class. Here's
>>>> what I have (see below). If I hardcode $where, something like
>>>> $where = " WHERE `firstname` = 'John' "
>>>> and then pass it as an argument into the scope of a 'select' function
>>>> then it works fine, but otherwise the query just appears to hang.
>>>> I'm deliberately not including the class or class extender at this
>>>> stage, but I'll add them to this thread later if required.
>>>> foreach ($_GET as $condition_key => $condition_value) {
>>>> $condition[] =" $condition_key = '$condition_value' ";
>>>> }
>>>> if(is_null($condition)){
>>>> $where = " WHERE 1 ";
>>>> }else{
>>>> $conditionString = implode('AND', $condition);
>>>> $where = " WHERE $conditionString ";
>>>> }
>> You're reposing the question? Or ?
>>
>
>
> No. I just wanted to get the thread into comp.lang.php too
>
The hardcoded version has single quotes around key and value, your $_GET
parsed version hasn't in this example. Could that be the cause? Does the
query run with results if NO conditions are set?
Sh.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 12:07:21 von zac.carey
On Apr 13, 10:27 am, Schraalhans Keukenmeester
wrote:
> strawberry wrote:
> > On Apr 13, 9:08 am, Schraalhans Keukenmeester
> > wrote:
> >> strawberry wrote:
> >>> On Apr 13, 1:51 am, "strawberry" wrote:
> >>>> I'm trying to extend a class (written by others) and was wondering if
> >>>> there's a standard a way of passing a $_GET request to a class. Here's
> >>>> what I have (see below). If I hardcode $where, something like
> >>>> $where = " WHERE `firstname` = 'John' "
> >>>> and then pass it as an argument into the scope of a 'select' function
> >>>> then it works fine, but otherwise the query just appears to hang.
> >>>> I'm deliberately not including the class or class extender at this
> >>>> stage, but I'll add them to this thread later if required.
> >>>> foreach ($_GET as $condition_key => $condition_value) {
> >>>> $condition[] =" $condition_key = '$condition_value' ";
> >>>> }
> >>>> if(is_null($condition)){
> >>>> $where = " WHERE 1 ";
> >>>> }else{
> >>>> $conditionString = implode('AND', $condition);
> >>>> $where = " WHERE $conditionString ";
> >>>> }
> >> You're reposing the question? Or ?
>
> > No. I just wanted to get the thread into comp.lang.php too
>
> The hardcoded version has single quotes around key and value, your $_GET
> parsed version hasn't in this example. Could that be the cause? Does the
> query run with results if NO conditions are set?
>
> Sh.
? It's true that the column name was missing the backticks - but that
would only be an issue when using a reserved word. Anyway, I've added
them:
.../test.php?Planet=Mars
echo $where;// WHERE `Planet` = 'Mars'
However, if I add the following line to the class...
global $where;
And then deliberately enter a non-existent column name I get this...
Unknown column 'firstname' in 'where clause'
.... so the implication is that the query is getting through.
I really need to be sure that I'm looking at the exact same query as
that being executed by mysql. Is there an obvious way to do that, or
would one need to examine the class in more detail?
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 12:07:21 von zac.carey
On Apr 13, 10:27 am, Schraalhans Keukenmeester
wrote:
> strawberry wrote:
> > On Apr 13, 9:08 am, Schraalhans Keukenmeester
> > wrote:
> >> strawberry wrote:
> >>> On Apr 13, 1:51 am, "strawberry" wrote:
> >>>> I'm trying to extend a class (written by others) and was wondering if
> >>>> there's a standard a way of passing a $_GET request to a class. Here's
> >>>> what I have (see below). If I hardcode $where, something like
> >>>> $where = " WHERE `firstname` = 'John' "
> >>>> and then pass it as an argument into the scope of a 'select' function
> >>>> then it works fine, but otherwise the query just appears to hang.
> >>>> I'm deliberately not including the class or class extender at this
> >>>> stage, but I'll add them to this thread later if required.
> >>>> foreach ($_GET as $condition_key => $condition_value) {
> >>>> $condition[] =" $condition_key = '$condition_value' ";
> >>>> }
> >>>> if(is_null($condition)){
> >>>> $where = " WHERE 1 ";
> >>>> }else{
> >>>> $conditionString = implode('AND', $condition);
> >>>> $where = " WHERE $conditionString ";
> >>>> }
> >> You're reposing the question? Or ?
>
> > No. I just wanted to get the thread into comp.lang.php too
>
> The hardcoded version has single quotes around key and value, your $_GET
> parsed version hasn't in this example. Could that be the cause? Does the
> query run with results if NO conditions are set?
>
> Sh.
? It's true that the column name was missing the backticks - but that
would only be an issue when using a reserved word. Anyway, I've added
them:
.../test.php?Planet=Mars
echo $where;// WHERE `Planet` = 'Mars'
However, if I add the following line to the class...
global $where;
And then deliberately enter a non-existent column name I get this...
Unknown column 'firstname' in 'where clause'
.... so the implication is that the query is getting through.
I really need to be sure that I'm looking at the exact same query as
that being executed by mysql. Is there an obvious way to do that, or
would one need to examine the class in more detail?
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 12:10:47 von Captain Paralytic
On 13 Apr, 01:51, "strawberry" wrote:
> I'm trying to extend a class (written by others) and was wondering if
> there's a standard a way of passing a $_GET request to a class. Here's
> what I have (see below). If I hardcode $where, something like
>
> $where = " WHERE `firstname` = 'John' "
>
> and then pass it as an argument into the scope of a 'select' function
> then it works fine, but otherwise the query just appears to hang.
>
> I'm deliberately not including the class or class extender at this
> stage, but I'll add them to this thread later if required.
>
> foreach ($_GET as $condition_key => $condition_value) {
> $condition[] =" $condition_key = '$condition_value' ";
> }
> if(is_null($condition)){
> $where = " WHERE 1 ";
> }else{
> $conditionString = implode('AND', $condition);
> $where = " WHERE $conditionString ";
> }
echo the final query and pop it into phpmyadmin. One thing I notice is
that you are not puting backtics round the field name ($condition_key)
in your loop.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 12:52:22 von zac.carey
On Apr 13, 11:10 am, "Captain Paralytic"
wrote:
> On 13 Apr, 01:51, "strawberry" wrote:
>
>
>
> > I'm trying to extend a class (written by others) and was wondering if
> > there's a standard a way of passing a $_GET request to a class. Here's
> > what I have (see below). If I hardcode $where, something like
>
> > $where = " WHERE `firstname` = 'John' "
>
> > and then pass it as an argument into the scope of a 'select' function
> > then it works fine, but otherwise the query just appears to hang.
>
> > I'm deliberately not including the class or class extender at this
> > stage, but I'll add them to this thread later if required.
>
> > foreach ($_GET as $condition_key => $condition_value) {
> > $condition[] =" $condition_key = '$condition_value' ";
> > }
> > if(is_null($condition)){
> > $where = " WHERE 1 ";
> > }else{
> > $conditionString = implode('AND', $condition);
> > $where = " WHERE $conditionString ";
> > }
>
> echo the final query and pop it into phpmyadmin. One thing I notice is
> that you are not puting backtics round the field name ($condition_key)
> in your loop.
You read my mind :-)
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 12:54:55 von Captain Paralytic
On 13 Apr, 11:52, "strawberry" wrote:
> On Apr 13, 11:10 am, "Captain Paralytic"
> wrote:
>
>
>
>
>
> > On 13 Apr, 01:51, "strawberry" wrote:
>
> > > I'm trying to extend a class (written by others) and was wondering if
> > > there's a standard a way of passing a $_GET request to a class. Here's
> > > what I have (see below). If I hardcode $where, something like
>
> > > $where = " WHERE `firstname` = 'John' "
>
> > > and then pass it as an argument into the scope of a 'select' function
> > > then it works fine, but otherwise the query just appears to hang.
>
> > > I'm deliberately not including the class or class extender at this
> > > stage, but I'll add them to this thread later if required.
>
> > > foreach ($_GET as $condition_key => $condition_value) {
> > > $condition[] =" $condition_key = '$condition_value' ";
> > > }
> > > if(is_null($condition)){
> > > $where = " WHERE 1 ";
> > > }else{
> > > $conditionString = implode('AND', $condition);
> > > $where = " WHERE $conditionString ";
> > > }
>
> > echo the final query and pop it into phpmyadmin. One thing I notice is
> > that you are not puting backtics round the field name ($condition_key)
> > in your loop.
>
> You read my mind :-)- Hide quoted text -
>
> - Show quoted text -
I thought all developers had telepathy functionality. That's how we
manage to build things even though the users don't know what they want.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 13:57:25 von zac.carey
On Apr 13, 11:54 am, "Captain Paralytic"
wrote:
> On 13 Apr, 11:52, "strawberry" wrote:
>
>
>
> > On Apr 13, 11:10 am, "Captain Paralytic"
> > wrote:
>
> > > On 13 Apr, 01:51, "strawberry" wrote:
>
> > > > I'm trying to extend a class (written by others) and was wondering if
> > > > there's a standard a way of passing a $_GET request to a class. Here's
> > > > what I have (see below). If I hardcode $where, something like
>
> > > > $where = " WHERE `firstname` = 'John' "
>
> > > > and then pass it as an argument into the scope of a 'select' function
> > > > then it works fine, but otherwise the query just appears to hang.
>
> > > > I'm deliberately not including the class or class extender at this
> > > > stage, but I'll add them to this thread later if required.
>
> > > > foreach ($_GET as $condition_key => $condition_value) {
> > > > $condition[] =" $condition_key = '$condition_value' ";
> > > > }
> > > > if(is_null($condition)){
> > > > $where = " WHERE 1 ";
> > > > }else{
> > > > $conditionString = implode('AND', $condition);
> > > > $where = " WHERE $conditionString ";
> > > > }
>
> > > echo the final query and pop it into phpmyadmin. One thing I notice is
> > > that you are not puting backtics round the field name ($condition_key)
> > > in your loop.
>
> > You read my mind :-)- Hide quoted text -
>
> > - Show quoted text -
>
> I thought all developers had telepathy functionality. That's how we
> manage to build things even though the users don't know what they want.
Well, here's the class extender, as provided by Olav at Drastic Tools.
The only change I've made to it is to add the $where argument to the
function...
class mysrc extends drasticsrcmysql {
protected function select($where){ //previously select()
$query = "SELECT * FROM $this->table $where "; //previously "SELECT *
FROM $this->table"
$res = mysql_query($query . $this->orderbystr, $this->conn) or
die(mysql_error());
return ($res);
}
If I modify those lines as follows...
$query = "SELECT * FROM $this->table $where ". $this->orderbystr;
$res = mysql_query($query . $this->orderbystr, $this->conn) or
die(mysql_error());
....and pass it a genuine column name, the results page hangs. Aside
from throwing up the predictable 'headers' warning, echoing $query
produces...
SELECT * FROM my_table WHERE `foo` = 'bar' ORDER BY id
....and if I pass it a false column name I get...
Unknown column 'foo' in 'where clause'.
So, now I'm thinking I should scrutinise that orderbystr more closely?
Maybe something else is going on in the class at that point.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 14:36:19 von zac.carey
On Apr 13, 2:12 pm, Jerry Stuckle wrote:
> Schraalhans Keukenmeester wrote:
> > strawberry wrote:
> >> On Apr 13, 1:51 am, "strawberry" wrote:
> >>> I'm trying to extend a class (written by others) and was wondering if
> >>> there's a standard a way of passing a $_GET request to a class. Here's
> >>> what I have (see below). If I hardcode $where, something like
>
> >>> $where = " WHERE `firstname` = 'John' "
>
> >>> and then pass it as an argument into the scope of a 'select' function
> >>> then it works fine, but otherwise the query just appears to hang.
>
> >>> I'm deliberately not including the class or class extender at this
> >>> stage, but I'll add them to this thread later if required.
>
> >>> foreach ($_GET as $condition_key => $condition_value) {
> >>> $condition[] =" $condition_key = '$condition_value' ";
> >>> }
> >>> if(is_null($condition)){
> >>> $where = " WHERE 1 ";
> >>> }else{
> >>> $conditionString = implode('AND', $condition);
> >>> $where = " WHERE $conditionString ";
> >>> }
>
> > You're reposing the question? Or ?
>
> > I am not sure myself which is preferrable: having the class handle the
> > $_GET superglobal array itself, which makes the client code programming
> > even easier, or leaving it to the client code, allowing for more custom
> > use. I've seen both examples, each has their practical (dis-)advantages.
>
> > If someone has a good argument on why to prefer one over the other (or
> > even an entirely different way) I'd also be helped!
>
> > Sh.
>
> The program should handle it, not the class.
>
> You should make your classes as generic as possible. If you parse the
> values in the program then pass the parsed values to the object, your
> code can be used in a number of ways. If you parse it in the class,
> you're restricted to using the $_GET array - and those specific index
> (field) names.
>
> For instance - what happens if you have the data stored in $_POST
> instead of $_GET? Or $_SESSION? You can argue that you'd just pass the
> proper superglobal to the object.
>
> However, what happens if on one page you have a checkbox "Sex" - and on
> another page it's called "Gender"? Or maybe even just "sex"? You can't
> do it if you process the superglobal. You can argue that you can make
> it a condition that the field must be named "Sex". But I don't like
> unwarranted restrictions like that.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================
Sorry Jerry, I'm confused. With the exception of "global $where",
everything I've presented so far is in the program (test.php). Can you
give me (or point me to) a practical illustration of what I should be
doing to provide this functionality. Obviously if you need/would like
to see more code, I can paste it here.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 14:36:19 von zac.carey
On Apr 13, 2:12 pm, Jerry Stuckle wrote:
> Schraalhans Keukenmeester wrote:
> > strawberry wrote:
> >> On Apr 13, 1:51 am, "strawberry" wrote:
> >>> I'm trying to extend a class (written by others) and was wondering if
> >>> there's a standard a way of passing a $_GET request to a class. Here's
> >>> what I have (see below). If I hardcode $where, something like
>
> >>> $where = " WHERE `firstname` = 'John' "
>
> >>> and then pass it as an argument into the scope of a 'select' function
> >>> then it works fine, but otherwise the query just appears to hang.
>
> >>> I'm deliberately not including the class or class extender at this
> >>> stage, but I'll add them to this thread later if required.
>
> >>> foreach ($_GET as $condition_key => $condition_value) {
> >>> $condition[] =" $condition_key = '$condition_value' ";
> >>> }
> >>> if(is_null($condition)){
> >>> $where = " WHERE 1 ";
> >>> }else{
> >>> $conditionString = implode('AND', $condition);
> >>> $where = " WHERE $conditionString ";
> >>> }
>
> > You're reposing the question? Or ?
>
> > I am not sure myself which is preferrable: having the class handle the
> > $_GET superglobal array itself, which makes the client code programming
> > even easier, or leaving it to the client code, allowing for more custom
> > use. I've seen both examples, each has their practical (dis-)advantages.
>
> > If someone has a good argument on why to prefer one over the other (or
> > even an entirely different way) I'd also be helped!
>
> > Sh.
>
> The program should handle it, not the class.
>
> You should make your classes as generic as possible. If you parse the
> values in the program then pass the parsed values to the object, your
> code can be used in a number of ways. If you parse it in the class,
> you're restricted to using the $_GET array - and those specific index
> (field) names.
>
> For instance - what happens if you have the data stored in $_POST
> instead of $_GET? Or $_SESSION? You can argue that you'd just pass the
> proper superglobal to the object.
>
> However, what happens if on one page you have a checkbox "Sex" - and on
> another page it's called "Gender"? Or maybe even just "sex"? You can't
> do it if you process the superglobal. You can argue that you can make
> it a condition that the field must be named "Sex". But I don't like
> unwarranted restrictions like that.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================
Sorry Jerry, I'm confused. With the exception of "global $where",
everything I've presented so far is in the program (test.php). Can you
give me (or point me to) a practical illustration of what I should be
doing to provide this functionality. Obviously if you need/would like
to see more code, I can paste it here.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 15:12:51 von Jerry Stuckle
Schraalhans Keukenmeester wrote:
> strawberry wrote:
>> On Apr 13, 1:51 am, "strawberry" wrote:
>>> I'm trying to extend a class (written by others) and was wondering if
>>> there's a standard a way of passing a $_GET request to a class. Here's
>>> what I have (see below). If I hardcode $where, something like
>>>
>>> $where = " WHERE `firstname` = 'John' "
>>>
>>> and then pass it as an argument into the scope of a 'select' function
>>> then it works fine, but otherwise the query just appears to hang.
>>>
>>> I'm deliberately not including the class or class extender at this
>>> stage, but I'll add them to this thread later if required.
>>>
>>> foreach ($_GET as $condition_key => $condition_value) {
>>> $condition[] =" $condition_key = '$condition_value' ";
>>> }
>>> if(is_null($condition)){
>>> $where = " WHERE 1 ";
>>> }else{
>>> $conditionString = implode('AND', $condition);
>>> $where = " WHERE $conditionString ";
>>> }
>>
> You're reposing the question? Or ?
>
> I am not sure myself which is preferrable: having the class handle the
> $_GET superglobal array itself, which makes the client code programming
> even easier, or leaving it to the client code, allowing for more custom
> use. I've seen both examples, each has their practical (dis-)advantages.
>
> If someone has a good argument on why to prefer one over the other (or
> even an entirely different way) I'd also be helped!
>
> Sh.
The program should handle it, not the class.
You should make your classes as generic as possible. If you parse the
values in the program then pass the parsed values to the object, your
code can be used in a number of ways. If you parse it in the class,
you're restricted to using the $_GET array - and those specific index
(field) names.
For instance - what happens if you have the data stored in $_POST
instead of $_GET? Or $_SESSION? You can argue that you'd just pass the
proper superglobal to the object.
However, what happens if on one page you have a checkbox "Sex" - and on
another page it's called "Gender"? Or maybe even just "sex"? You can't
do it if you process the superglobal. You can argue that you can make
it a condition that the field must be named "Sex". But I don't like
unwarranted restrictions like that.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 15:12:51 von Jerry Stuckle
Schraalhans Keukenmeester wrote:
> strawberry wrote:
>> On Apr 13, 1:51 am, "strawberry" wrote:
>>> I'm trying to extend a class (written by others) and was wondering if
>>> there's a standard a way of passing a $_GET request to a class. Here's
>>> what I have (see below). If I hardcode $where, something like
>>>
>>> $where = " WHERE `firstname` = 'John' "
>>>
>>> and then pass it as an argument into the scope of a 'select' function
>>> then it works fine, but otherwise the query just appears to hang.
>>>
>>> I'm deliberately not including the class or class extender at this
>>> stage, but I'll add them to this thread later if required.
>>>
>>> foreach ($_GET as $condition_key => $condition_value) {
>>> $condition[] =" $condition_key = '$condition_value' ";
>>> }
>>> if(is_null($condition)){
>>> $where = " WHERE 1 ";
>>> }else{
>>> $conditionString = implode('AND', $condition);
>>> $where = " WHERE $conditionString ";
>>> }
>>
> You're reposing the question? Or ?
>
> I am not sure myself which is preferrable: having the class handle the
> $_GET superglobal array itself, which makes the client code programming
> even easier, or leaving it to the client code, allowing for more custom
> use. I've seen both examples, each has their practical (dis-)advantages.
>
> If someone has a good argument on why to prefer one over the other (or
> even an entirely different way) I'd also be helped!
>
> Sh.
The program should handle it, not the class.
You should make your classes as generic as possible. If you parse the
values in the program then pass the parsed values to the object, your
code can be used in a number of ways. If you parse it in the class,
you're restricted to using the $_GET array - and those specific index
(field) names.
For instance - what happens if you have the data stored in $_POST
instead of $_GET? Or $_SESSION? You can argue that you'd just pass the
proper superglobal to the object.
However, what happens if on one page you have a checkbox "Sex" - and on
another page it's called "Gender"? Or maybe even just "sex"? You can't
do it if you process the superglobal. You can argue that you can make
it a condition that the field must be named "Sex". But I don't like
unwarranted restrictions like that.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 16:38:42 von Vince Morgan
"strawberry" wrote in message
news:1176450464.524325.95990@n59g2000hsh.googlegroups.com...
> On Apr 13, 1:51 am, "strawberry" wrote:
> > I'm trying to extend a class (written by others) and was wondering if
> > there's a standard a way of passing a $_GET request to a class. Here's
> > what I have (see below). If I hardcode $where, something like
> >
> > $where = " WHERE `firstname` = 'John' "
> >
> > and then pass it as an argument into the scope of a 'select' function
> > then it works fine, but otherwise the query just appears to hang.
> >
> > I'm deliberately not including the class or class extender at this
> > stage, but I'll add them to this thread later if required.
> >
> > foreach ($_GET as $condition_key => $condition_value) {
> > $condition[] =" $condition_key = '$condition_value' ";
> > }
> > if(is_null($condition)){
> > $where = " WHERE 1 ";
> > }else{
> > $conditionString = implode('AND', $condition);
> > $where = " WHERE $conditionString ";
The line above is looking for a value from a field named $conditionString.
You seem to have missed the append operator.
$where = "WHERE "."'".$conditionString."'";
Or
$where = "WHERE ".$conditionString;
> > }
>
>
HTH
Vince
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 16:38:42 von Vince Morgan
"strawberry" wrote in message
news:1176450464.524325.95990@n59g2000hsh.googlegroups.com...
> On Apr 13, 1:51 am, "strawberry" wrote:
> > I'm trying to extend a class (written by others) and was wondering if
> > there's a standard a way of passing a $_GET request to a class. Here's
> > what I have (see below). If I hardcode $where, something like
> >
> > $where = " WHERE `firstname` = 'John' "
> >
> > and then pass it as an argument into the scope of a 'select' function
> > then it works fine, but otherwise the query just appears to hang.
> >
> > I'm deliberately not including the class or class extender at this
> > stage, but I'll add them to this thread later if required.
> >
> > foreach ($_GET as $condition_key => $condition_value) {
> > $condition[] =" $condition_key = '$condition_value' ";
> > }
> > if(is_null($condition)){
> > $where = " WHERE 1 ";
> > }else{
> > $conditionString = implode('AND', $condition);
> > $where = " WHERE $conditionString ";
The line above is looking for a value from a field named $conditionString.
You seem to have missed the append operator.
$where = "WHERE "."'".$conditionString."'";
Or
$where = "WHERE ".$conditionString;
> > }
>
>
HTH
Vince
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 19:23:43 von Rami Elomaa
Vince Morgan kirjoitti:
> "strawberry" wrote in message
> news:1176450464.524325.95990@n59g2000hsh.googlegroups.com...
>> On Apr 13, 1:51 am, "strawberry" wrote:
>>> I'm trying to extend a class (written by others) and was wondering if
>>> there's a standard a way of passing a $_GET request to a class. Here's
>>> what I have (see below). If I hardcode $where, something like
>>>
>>> $where = " WHERE `firstname` = 'John' "
>>>
>>> and then pass it as an argument into the scope of a 'select' function
>>> then it works fine, but otherwise the query just appears to hang.
>>>
>>> I'm deliberately not including the class or class extender at this
>>> stage, but I'll add them to this thread later if required.
>>>
>>> foreach ($_GET as $condition_key => $condition_value) {
>>> $condition[] =" $condition_key = '$condition_value' ";
>>> }
>>> if(is_null($condition)){
>>> $where = " WHERE 1 ";
>>> }else{
>>> $conditionString = implode('AND', $condition);
>>> $where = " WHERE $conditionString ";
>
> The line above is looking for a value from a field named $conditionString.
No, it doesn't. Variables inside a double quoted string are parsed. It's
perfectly valid code. The example you gave would also work, but it's
totally unnecessary.
Just try this and you'll learn something fun:
$cat = 'dog';
echo "I have an animal in my hand and the animal is a $cat.
";
echo 'I have an animal in my hand and the animal is a $cat.
';
?>
--
Rami.Elomaa@gmail.com
"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 19:23:43 von Rami Elomaa
Vince Morgan kirjoitti:
> "strawberry" wrote in message
> news:1176450464.524325.95990@n59g2000hsh.googlegroups.com...
>> On Apr 13, 1:51 am, "strawberry" wrote:
>>> I'm trying to extend a class (written by others) and was wondering if
>>> there's a standard a way of passing a $_GET request to a class. Here's
>>> what I have (see below). If I hardcode $where, something like
>>>
>>> $where = " WHERE `firstname` = 'John' "
>>>
>>> and then pass it as an argument into the scope of a 'select' function
>>> then it works fine, but otherwise the query just appears to hang.
>>>
>>> I'm deliberately not including the class or class extender at this
>>> stage, but I'll add them to this thread later if required.
>>>
>>> foreach ($_GET as $condition_key => $condition_value) {
>>> $condition[] =" $condition_key = '$condition_value' ";
>>> }
>>> if(is_null($condition)){
>>> $where = " WHERE 1 ";
>>> }else{
>>> $conditionString = implode('AND', $condition);
>>> $where = " WHERE $conditionString ";
>
> The line above is looking for a value from a field named $conditionString.
No, it doesn't. Variables inside a double quoted string are parsed. It's
perfectly valid code. The example you gave would also work, but it's
totally unnecessary.
Just try this and you'll learn something fun:
$cat = 'dog';
echo "I have an animal in my hand and the animal is a $cat.
";
echo 'I have an animal in my hand and the animal is a $cat.
';
?>
--
Rami.Elomaa@gmail.com
"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 20:17:11 von zac.carey
On Apr 13, 6:23 pm, Rami Elomaa wrote:
> Vince Morgan kirjoitti:
>
>
>
> > "strawberry" wrote in message
> >news:1176450464.524325.95990@n59g2000hsh.googlegroups.com.. .
> >> On Apr 13, 1:51 am, "strawberry" wrote:
> >>> I'm trying to extend a class (written by others) and was wondering if
> >>> there's a standard a way of passing a $_GET request to a class. Here's
> >>> what I have (see below). If I hardcode $where, something like
>
> >>> $where =3D " WHERE `firstname` =3D 'John' "
>
> >>> and then pass it as an argument into the scope of a 'select' function
> >>> then it works fine, but otherwise the query just appears to hang.
>
> >>> I'm deliberately not including the class or class extender at this
> >>> stage, but I'll add them to this thread later if required.
>
> >>> foreach ($_GET as $condition_key =3D> $condition_value) {
> >>> $condition[] =3D" $condition_key =3D '$condition_value' ";
> >>> }
> >>> if(is_null($condition)){
> >>> $where =3D " WHERE 1 ";
> >>> }else{
> >>> $conditionString =3D implode('AND', $condition);
> >>> $where =3D " WHERE $conditionString ";
>
> > The line above is looking for a value from a field named $conditionStri=
ng.
>
> No, it doesn't. Variables inside a double quoted string are parsed. It's
> perfectly valid code. The example you gave would also work, but it's
> totally unnecessary.
>
> Just try this and you'll learn something fun:
>
>
>
> $cat =3D 'dog';
>
> echo "I have an animal in my hand and the animal is a $cat.
";
> echo 'I have an animal in my hand and the animal is a $cat.
';
>
> ?>
>
> --
> Rami.Elo...@gmail.com
>
> "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
> usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Thanks Rami,
I knew from the echoes that my statements were valid. Unfortunately,
the mysrc class doesn't seem to agree with me :-(
To recap. No matter what I try I just can't seem to pass this...
foreach ($_GET as $condition_key =3D> $condition_value) {
$condition[] =3D" `$condition_key` =3D '$condition_value' ";
$condition_keys[] =3D "$condition_key";
$condition_values[] =3D "'$condition_value'";
}
if(is_null($condition)){
$conditionString =3D " 1 ";
}else{
$conditionString =3D implode('AND', $condition);
$conditionKeysString =3D implode(',',$condition_keys);
$conditionValuesString =3D implode(',',$condition_values);
}
as arguments to this...
class mysrc extends drasticsrcmysql {
protected function select($conditionString){
$query =3D "SELECT * FROM $this->table WHERE $conditionString";
$res =3D mysql_query($query . $this->orderbystr, $this->conn) or
die(mysql_error());
return ($res);
}
protected function add($conditionKeysString,$conditionValuesString){
$query =3D "INSERT INTO $this->table ($conditionKeysString) VALUES
($conditionValuesString)";
mysql_query($query, $this->conn) or die(mysql_error());
if (mysql_affected_rows($this->conn) == 1) return(true); else
return(false);
}
...unless I pass a bogus column name: Unknown column
'false_column_name' in 'where clause'
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 20:17:11 von zac.carey
On Apr 13, 6:23 pm, Rami Elomaa wrote:
> Vince Morgan kirjoitti:
>
>
>
> > "strawberry" wrote in message
> >news:1176450464.524325.95990@n59g2000hsh.googlegroups.com.. .
> >> On Apr 13, 1:51 am, "strawberry" wrote:
> >>> I'm trying to extend a class (written by others) and was wondering if
> >>> there's a standard a way of passing a $_GET request to a class. Here's
> >>> what I have (see below). If I hardcode $where, something like
>
> >>> $where =3D " WHERE `firstname` =3D 'John' "
>
> >>> and then pass it as an argument into the scope of a 'select' function
> >>> then it works fine, but otherwise the query just appears to hang.
>
> >>> I'm deliberately not including the class or class extender at this
> >>> stage, but I'll add them to this thread later if required.
>
> >>> foreach ($_GET as $condition_key =3D> $condition_value) {
> >>> $condition[] =3D" $condition_key =3D '$condition_value' ";
> >>> }
> >>> if(is_null($condition)){
> >>> $where =3D " WHERE 1 ";
> >>> }else{
> >>> $conditionString =3D implode('AND', $condition);
> >>> $where =3D " WHERE $conditionString ";
>
> > The line above is looking for a value from a field named $conditionStri=
ng.
>
> No, it doesn't. Variables inside a double quoted string are parsed. It's
> perfectly valid code. The example you gave would also work, but it's
> totally unnecessary.
>
> Just try this and you'll learn something fun:
>
>
>
> $cat =3D 'dog';
>
> echo "I have an animal in my hand and the animal is a $cat.
";
> echo 'I have an animal in my hand and the animal is a $cat.
';
>
> ?>
>
> --
> Rami.Elo...@gmail.com
>
> "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
> usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Thanks Rami,
I knew from the echoes that my statements were valid. Unfortunately,
the mysrc class doesn't seem to agree with me :-(
To recap. No matter what I try I just can't seem to pass this...
foreach ($_GET as $condition_key =3D> $condition_value) {
$condition[] =3D" `$condition_key` =3D '$condition_value' ";
$condition_keys[] =3D "$condition_key";
$condition_values[] =3D "'$condition_value'";
}
if(is_null($condition)){
$conditionString =3D " 1 ";
}else{
$conditionString =3D implode('AND', $condition);
$conditionKeysString =3D implode(',',$condition_keys);
$conditionValuesString =3D implode(',',$condition_values);
}
as arguments to this...
class mysrc extends drasticsrcmysql {
protected function select($conditionString){
$query =3D "SELECT * FROM $this->table WHERE $conditionString";
$res =3D mysql_query($query . $this->orderbystr, $this->conn) or
die(mysql_error());
return ($res);
}
protected function add($conditionKeysString,$conditionValuesString){
$query =3D "INSERT INTO $this->table ($conditionKeysString) VALUES
($conditionValuesString)";
mysql_query($query, $this->conn) or die(mysql_error());
if (mysql_affected_rows($this->conn) == 1) return(true); else
return(false);
}
...unless I pass a bogus column name: Unknown column
'false_column_name' in 'where clause'
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 20:40:21 von Steve
"strawberry" wrote in message
news:1176488231.812171.25480@o5g2000hsb.googlegroups.com...
On Apr 13, 6:23 pm, Rami Elomaa wrote:
============
foreach ($_GET as $condition_key => $condition_value) {
$condition[] =" `$condition_key` = '$condition_value' ";
$condition_keys[] = "$condition_key";
$condition_values[] = "'$condition_value'";
}
if(is_null($condition)){
$conditionString = " 1 ";
}else{
$conditionString = implode('AND', $condition);
$conditionKeysString = implode(',',$condition_keys);
$conditionValuesString = implode(',',$condition_values);
============
have you thought of always having criteria of WHERE 1 = 1 ? that way you can
avoid having if/else logic. you can then always impode with AND. i'd also
recommend that you array_walk the columns ($cond_keys) and the values
($cond_values) so that you can back-tick the columns and escape the ticks
that may be present in the values. otherwise, you may have quite a volitile
little query. :)
i'd also watch out for treating $_GET as global AND trustworthy. it's better
to define the fields in your code and then set their prospective values from
$_GET/$_POST/$_REQUEST/whatever. array_walk is great for that as well.
if i wanted to hack and ruin your site (down your mysql instance), i could
introduce my own little condition and have it loop infinitely.
just a thought.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 20:40:21 von Steve
"strawberry" wrote in message
news:1176488231.812171.25480@o5g2000hsb.googlegroups.com...
On Apr 13, 6:23 pm, Rami Elomaa wrote:
============
foreach ($_GET as $condition_key => $condition_value) {
$condition[] =" `$condition_key` = '$condition_value' ";
$condition_keys[] = "$condition_key";
$condition_values[] = "'$condition_value'";
}
if(is_null($condition)){
$conditionString = " 1 ";
}else{
$conditionString = implode('AND', $condition);
$conditionKeysString = implode(',',$condition_keys);
$conditionValuesString = implode(',',$condition_values);
============
have you thought of always having criteria of WHERE 1 = 1 ? that way you can
avoid having if/else logic. you can then always impode with AND. i'd also
recommend that you array_walk the columns ($cond_keys) and the values
($cond_values) so that you can back-tick the columns and escape the ticks
that may be present in the values. otherwise, you may have quite a volitile
little query. :)
i'd also watch out for treating $_GET as global AND trustworthy. it's better
to define the fields in your code and then set their prospective values from
$_GET/$_POST/$_REQUEST/whatever. array_walk is great for that as well.
if i wanted to hack and ruin your site (down your mysql instance), i could
introduce my own little condition and have it loop infinitely.
just a thought.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 20:47:54 von Rami Elomaa
strawberry kirjoitti:
> On Apr 13, 6:23 pm, Rami Elomaa wrote:
>> Vince Morgan kirjoitti:
>>
>>
>>
>>> "strawberry" wrote in message
>>> news:1176450464.524325.95990@n59g2000hsh.googlegroups.com...
>>>> On Apr 13, 1:51 am, "strawberry" wrote:
>>>>> I'm trying to extend a class (written by others) and was wondering if
>>>>> there's a standard a way of passing a $_GET request to a class. Here's
>>>>> what I have (see below). If I hardcode $where, something like
>>>>> $where = " WHERE `firstname` = 'John' "
>>>>> and then pass it as an argument into the scope of a 'select' function
>>>>> then it works fine, but otherwise the query just appears to hang.
>>>>> I'm deliberately not including the class or class extender at this
>>>>> stage, but I'll add them to this thread later if required.
>>>>> foreach ($_GET as $condition_key => $condition_value) {
>>>>> $condition[] =" $condition_key = '$condition_value' ";
>>>>> }
>>>>> if(is_null($condition)){
>>>>> $where = " WHERE 1 ";
>>>>> }else{
>>>>> $conditionString = implode('AND', $condition);
>>>>> $where = " WHERE $conditionString ";
>>> The line above is looking for a value from a field named $conditionString.
>> No, it doesn't. Variables inside a double quoted string are parsed. It's
>> perfectly valid code. The example you gave would also work, but it's
>> totally unnecessary.
>>
>> Just try this and you'll learn something fun:
>>
>>
>>
>> $cat = 'dog';
>>
>> echo "I have an animal in my hand and the animal is a $cat.
";
>> echo 'I have an animal in my hand and the animal is a $cat.
';
>>
>> ?>
>>
>> --
>> Rami.Elo...@gmail.com
>>
>> "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
>> usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
>
> Thanks Rami,
>
> I knew from the echoes that my statements were valid.
Yes, and I never doubted that, I was just replying to Vince who was
giving you silly advices.. :)
> Unfortunately,
> the mysrc class doesn't seem to agree with me :-(
>
> To recap. No matter what I try I just can't seem to pass this...
>
> foreach ($_GET as $condition_key => $condition_value) {
> $condition[] =" `$condition_key` = '$condition_value' ";
> $condition_keys[] = "$condition_key";
> $condition_values[] = "'$condition_value'";
> }
> if(is_null($condition)){
> $conditionString = " 1 ";
> }else{
> $conditionString = implode('AND', $condition);
> $conditionKeysString = implode(',',$condition_keys);
> $conditionValuesString = implode(',',$condition_values);
> }
>
> as arguments to this...
>
> class mysrc extends drasticsrcmysql {
> protected function select($conditionString){
> $query = "SELECT * FROM $this->table WHERE $conditionString";
> $res = mysql_query($query . $this->orderbystr, $this->conn) or
> die(mysql_error());
> return ($res);
> }
>
> protected function add($conditionKeysString,$conditionValuesString){
> $query = "INSERT INTO $this->table ($conditionKeysString) VALUES
> ($conditionValuesString)";
> mysql_query($query, $this->conn) or die(mysql_error());
> if (mysql_affected_rows($this->conn) == 1) return(true); else
> return(false);
> }
>
> ...unless I pass a bogus column name: Unknown column
> 'false_column_name' in 'where clause'
>
I'd say the query works fine, just that the resultset is not visible
outside the method for some reason. The error message you get would
indicate that the query is passed and executed, and the problem may lie
elsewhere. I seem to recall having a similar problem when passing a
result set from one function to another. I don't remember the situation
exactly, but as I recall it, the resultset would disappear, perhaps
because it only existed in the scope of that function. So if you are
testing the resultset outside the class, it might be that the resultset
has already been cleared, even though it was executed in a civilized
manner. Study the resultset within the method to see if it exists
there... If it does, then you've at least discovered the problem and the
next step is to make it persist. HTH.
--
Rami.Elomaa@gmail.com
"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 20:47:54 von Rami Elomaa
strawberry kirjoitti:
> On Apr 13, 6:23 pm, Rami Elomaa wrote:
>> Vince Morgan kirjoitti:
>>
>>
>>
>>> "strawberry" wrote in message
>>> news:1176450464.524325.95990@n59g2000hsh.googlegroups.com...
>>>> On Apr 13, 1:51 am, "strawberry" wrote:
>>>>> I'm trying to extend a class (written by others) and was wondering if
>>>>> there's a standard a way of passing a $_GET request to a class. Here's
>>>>> what I have (see below). If I hardcode $where, something like
>>>>> $where = " WHERE `firstname` = 'John' "
>>>>> and then pass it as an argument into the scope of a 'select' function
>>>>> then it works fine, but otherwise the query just appears to hang.
>>>>> I'm deliberately not including the class or class extender at this
>>>>> stage, but I'll add them to this thread later if required.
>>>>> foreach ($_GET as $condition_key => $condition_value) {
>>>>> $condition[] =" $condition_key = '$condition_value' ";
>>>>> }
>>>>> if(is_null($condition)){
>>>>> $where = " WHERE 1 ";
>>>>> }else{
>>>>> $conditionString = implode('AND', $condition);
>>>>> $where = " WHERE $conditionString ";
>>> The line above is looking for a value from a field named $conditionString.
>> No, it doesn't. Variables inside a double quoted string are parsed. It's
>> perfectly valid code. The example you gave would also work, but it's
>> totally unnecessary.
>>
>> Just try this and you'll learn something fun:
>>
>>
>>
>> $cat = 'dog';
>>
>> echo "I have an animal in my hand and the animal is a $cat.
";
>> echo 'I have an animal in my hand and the animal is a $cat.
';
>>
>> ?>
>>
>> --
>> Rami.Elo...@gmail.com
>>
>> "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
>> usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
>
> Thanks Rami,
>
> I knew from the echoes that my statements were valid.
Yes, and I never doubted that, I was just replying to Vince who was
giving you silly advices.. :)
> Unfortunately,
> the mysrc class doesn't seem to agree with me :-(
>
> To recap. No matter what I try I just can't seem to pass this...
>
> foreach ($_GET as $condition_key => $condition_value) {
> $condition[] =" `$condition_key` = '$condition_value' ";
> $condition_keys[] = "$condition_key";
> $condition_values[] = "'$condition_value'";
> }
> if(is_null($condition)){
> $conditionString = " 1 ";
> }else{
> $conditionString = implode('AND', $condition);
> $conditionKeysString = implode(',',$condition_keys);
> $conditionValuesString = implode(',',$condition_values);
> }
>
> as arguments to this...
>
> class mysrc extends drasticsrcmysql {
> protected function select($conditionString){
> $query = "SELECT * FROM $this->table WHERE $conditionString";
> $res = mysql_query($query . $this->orderbystr, $this->conn) or
> die(mysql_error());
> return ($res);
> }
>
> protected function add($conditionKeysString,$conditionValuesString){
> $query = "INSERT INTO $this->table ($conditionKeysString) VALUES
> ($conditionValuesString)";
> mysql_query($query, $this->conn) or die(mysql_error());
> if (mysql_affected_rows($this->conn) == 1) return(true); else
> return(false);
> }
>
> ...unless I pass a bogus column name: Unknown column
> 'false_column_name' in 'where clause'
>
I'd say the query works fine, just that the resultset is not visible
outside the method for some reason. The error message you get would
indicate that the query is passed and executed, and the problem may lie
elsewhere. I seem to recall having a similar problem when passing a
result set from one function to another. I don't remember the situation
exactly, but as I recall it, the resultset would disappear, perhaps
because it only existed in the scope of that function. So if you are
testing the resultset outside the class, it might be that the resultset
has already been cleared, even though it was executed in a civilized
manner. Study the resultset within the method to see if it exists
there... If it does, then you've at least discovered the problem and the
next step is to make it persist. HTH.
--
Rami.Elomaa@gmail.com
"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 23:17:20 von zac.carey
On Apr 13, 7:40 pm, "Steve" wrote:
> "strawberry" wrote in message
>
> news:1176488231.812171.25480@o5g2000hsb.googlegroups.com...
> On Apr 13, 6:23 pm, Rami Elomaa wrote:
>
> ============
>
> foreach ($_GET as $condition_key => $condition_value) {
> $condition[] =" `$condition_key` = '$condition_value' ";
> $condition_keys[] = "$condition_key";
> $condition_values[] = "'$condition_value'";}
>
> if(is_null($condition)){
> $conditionString = " 1 ";}else{
>
> $conditionString = implode('AND', $condition);
> $conditionKeysString = implode(',',$condition_keys);
> $conditionValuesString = implode(',',$condition_values);
>
> ============
>
> have you thought of always having criteria of WHERE 1 = 1 ? that way you can
> avoid having if/else logic. you can then always impode with AND.
Good idea, what would that actually look like?
> i'd also
> recommend that you array_walk the columns ($cond_keys) and the values
> ($cond_values) so that you can back-tick the columns and escape the ticks
> that may be present in the values. otherwise, you may have quite a volitile
> little query. :)
>
At the moment, the intention is to deploy this on a tiny office
intranet so the security risks are neglible. That said, it's obviously
very sensible advice, however, the author of the class
has just informed that he's shortly going to publish a new version
with this functionality built in - so I think I might just wait to see
what that's all about.
> i'd also watch out for treating $_GET as global AND trustworthy. it's better
> to define the fields in your code and then set their prospective values from
> $_GET/$_POST/$_REQUEST/whatever. array_walk is great for that as well.
> if i wanted to hack and ruin your site (down your mysql instance), i could
> introduce my own little condition and have it loop infinitely.
That would be mean. Well, in the event of that happening, at least now
I'd know where to come knocking.
> just a thought.
Thanks guys. It looks like this one will resolve itself in due course.
Still, I'd like to figure out what's going on for the next time I run
into this kind of problem.
Cheers.
Re: $_GET request -> variable -> object? -> function
am 13.04.2007 23:17:20 von zac.carey
On Apr 13, 7:40 pm, "Steve" wrote:
> "strawberry" wrote in message
>
> news:1176488231.812171.25480@o5g2000hsb.googlegroups.com...
> On Apr 13, 6:23 pm, Rami Elomaa wrote:
>
> ============
>
> foreach ($_GET as $condition_key => $condition_value) {
> $condition[] =" `$condition_key` = '$condition_value' ";
> $condition_keys[] = "$condition_key";
> $condition_values[] = "'$condition_value'";}
>
> if(is_null($condition)){
> $conditionString = " 1 ";}else{
>
> $conditionString = implode('AND', $condition);
> $conditionKeysString = implode(',',$condition_keys);
> $conditionValuesString = implode(',',$condition_values);
>
> ============
>
> have you thought of always having criteria of WHERE 1 = 1 ? that way you can
> avoid having if/else logic. you can then always impode with AND.
Good idea, what would that actually look like?
> i'd also
> recommend that you array_walk the columns ($cond_keys) and the values
> ($cond_values) so that you can back-tick the columns and escape the ticks
> that may be present in the values. otherwise, you may have quite a volitile
> little query. :)
>
At the moment, the intention is to deploy this on a tiny office
intranet so the security risks are neglible. That said, it's obviously
very sensible advice, however, the author of the class
has just informed that he's shortly going to publish a new version
with this functionality built in - so I think I might just wait to see
what that's all about.
> i'd also watch out for treating $_GET as global AND trustworthy. it's better
> to define the fields in your code and then set their prospective values from
> $_GET/$_POST/$_REQUEST/whatever. array_walk is great for that as well.
> if i wanted to hack and ruin your site (down your mysql instance), i could
> introduce my own little condition and have it loop infinitely.
That would be mean. Well, in the event of that happening, at least now
I'd know where to come knocking.
> just a thought.
Thanks guys. It looks like this one will resolve itself in due course.
Still, I'd like to figure out what's going on for the next time I run
into this kind of problem.
Cheers.
Re: $_GET request -> variable -> object? -> function
am 14.04.2007 01:48:25 von Vince Morgan
"Rami Elomaa" wrote in message
news:evoeao$qn8$1@nyytiset.pp.htv.fi...
> Vince Morgan kirjoitti:
> > "strawberry" wrote in message
> > news:1176450464.524325.95990@n59g2000hsh.googlegroups.com...
> >> On Apr 13, 1:51 am, "strawberry" wrote:
> >>> I'm trying to extend a class (written by others) and was wondering if
> >>> there's a standard a way of passing a $_GET request to a class. Here's
> >>> what I have (see below). If I hardcode $where, something like
> >>>
> >>> $where = " WHERE `firstname` = 'John' "
> >>>
> >>> and then pass it as an argument into the scope of a 'select' function
> >>> then it works fine, but otherwise the query just appears to hang.
> >>>
> >>> I'm deliberately not including the class or class extender at this
> >>> stage, but I'll add them to this thread later if required.
> >>>
> >>> foreach ($_GET as $condition_key => $condition_value) {
> >>> $condition[] =" $condition_key = '$condition_value' ";
> >>> }
> >>> if(is_null($condition)){
> >>> $where = " WHERE 1 ";
> >>> }else{
> >>> $conditionString = implode('AND', $condition);
> >>> $where = " WHERE $conditionString ";
> >
> > The line above is looking for a value from a field named
$conditionString.
>
> No, it doesn't. Variables inside a double quoted string are parsed. It's
> perfectly valid code. The example you gave would also work, but it's
> totally unnecessary.
>
> Just try this and you'll learn something fun:
>
>
>
> $cat = 'dog';
>
> echo "I have an animal in my hand and the animal is a $cat.
";
> echo 'I have an animal in my hand and the animal is a $cat.
';
>
Well, errr, I'm still learning php. Will be for some time I see ;)
I did know that double quotes were parsed, but didn't know that the engine
would recognise "$conditionString" as a var.
Fortunately I know how to make scrambled eggs correctly, I think, so I'll
just go scrape some of my face and have breakfast.
Thanks,
Vince
Re: $_GET request -> variable -> object? -> function
am 14.04.2007 01:48:25 von Vince Morgan
"Rami Elomaa" wrote in message
news:evoeao$qn8$1@nyytiset.pp.htv.fi...
> Vince Morgan kirjoitti:
> > "strawberry" wrote in message
> > news:1176450464.524325.95990@n59g2000hsh.googlegroups.com...
> >> On Apr 13, 1:51 am, "strawberry" wrote:
> >>> I'm trying to extend a class (written by others) and was wondering if
> >>> there's a standard a way of passing a $_GET request to a class. Here's
> >>> what I have (see below). If I hardcode $where, something like
> >>>
> >>> $where = " WHERE `firstname` = 'John' "
> >>>
> >>> and then pass it as an argument into the scope of a 'select' function
> >>> then it works fine, but otherwise the query just appears to hang.
> >>>
> >>> I'm deliberately not including the class or class extender at this
> >>> stage, but I'll add them to this thread later if required.
> >>>
> >>> foreach ($_GET as $condition_key => $condition_value) {
> >>> $condition[] =" $condition_key = '$condition_value' ";
> >>> }
> >>> if(is_null($condition)){
> >>> $where = " WHERE 1 ";
> >>> }else{
> >>> $conditionString = implode('AND', $condition);
> >>> $where = " WHERE $conditionString ";
> >
> > The line above is looking for a value from a field named
$conditionString.
>
> No, it doesn't. Variables inside a double quoted string are parsed. It's
> perfectly valid code. The example you gave would also work, but it's
> totally unnecessary.
>
> Just try this and you'll learn something fun:
>
>
>
> $cat = 'dog';
>
> echo "I have an animal in my hand and the animal is a $cat.
";
> echo 'I have an animal in my hand and the animal is a $cat.
';
>
Well, errr, I'm still learning php. Will be for some time I see ;)
I did know that double quotes were parsed, but didn't know that the engine
would recognise "$conditionString" as a var.
Fortunately I know how to make scrambled eggs correctly, I think, so I'll
just go scrape some of my face and have breakfast.
Thanks,
Vince
Re: $_GET request -> variable -> object? -> function
am 14.04.2007 04:48:10 von Jerry Stuckle
strawberry wrote:
> On Apr 13, 2:12 pm, Jerry Stuckle wrote:
>> Schraalhans Keukenmeester wrote:
>>> strawberry wrote:
>>>> On Apr 13, 1:51 am, "strawberry" wrote:
>>>>> I'm trying to extend a class (written by others) and was wondering if
>>>>> there's a standard a way of passing a $_GET request to a class. Here's
>>>>> what I have (see below). If I hardcode $where, something like
>>>>> $where = " WHERE `firstname` = 'John' "
>>>>> and then pass it as an argument into the scope of a 'select' function
>>>>> then it works fine, but otherwise the query just appears to hang.
>>>>> I'm deliberately not including the class or class extender at this
>>>>> stage, but I'll add them to this thread later if required.
>>>>> foreach ($_GET as $condition_key => $condition_value) {
>>>>> $condition[] =" $condition_key = '$condition_value' ";
>>>>> }
>>>>> if(is_null($condition)){
>>>>> $where = " WHERE 1 ";
>>>>> }else{
>>>>> $conditionString = implode('AND', $condition);
>>>>> $where = " WHERE $conditionString ";
>>>>> }
>>> You're reposing the question? Or ?
>>> I am not sure myself which is preferrable: having the class handle the
>>> $_GET superglobal array itself, which makes the client code programming
>>> even easier, or leaving it to the client code, allowing for more custom
>>> use. I've seen both examples, each has their practical (dis-)advantages.
>>> If someone has a good argument on why to prefer one over the other (or
>>> even an entirely different way) I'd also be helped!
>>> Sh.
>> The program should handle it, not the class.
>>
>> You should make your classes as generic as possible. If you parse the
>> values in the program then pass the parsed values to the object, your
>> code can be used in a number of ways. If you parse it in the class,
>> you're restricted to using the $_GET array - and those specific index
>> (field) names.
>>
>> For instance - what happens if you have the data stored in $_POST
>> instead of $_GET? Or $_SESSION? You can argue that you'd just pass the
>> proper superglobal to the object.
>>
>> However, what happens if on one page you have a checkbox "Sex" - and on
>> another page it's called "Gender"? Or maybe even just "sex"? You can't
>> do it if you process the superglobal. You can argue that you can make
>> it a condition that the field must be named "Sex". But I don't like
>> unwarranted restrictions like that.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Sorry Jerry, I'm confused. With the exception of "global $where",
> everything I've presented so far is in the program (test.php). Can you
> give me (or point me to) a practical illustration of what I should be
> doing to provide this functionality. Obviously if you need/would like
> to see more code, I can paste it here.
>
First of all, don't use $global. It creates a dependency between your
function and other parts of the code. If the function needs a variable,
pass it in the function call (by reference, if necessary).
As for an example:
function validateGender() {
if ($_GET['sex'] == 'M' or $_GET['sex'] == 'F')
return true;
else
return false;
}
...
...
if (validateGender()) ...
(Yes, I know you can do this with a ternary operator, but this is an
example).
The problem here is you have a required relationship between the form
and the function. Your form would have to have:
You couldn't have, for instance, "method=post". Nor could you have on
your radio buttons name="gender", or even name="Sex". And you couldn't
use it except from the form.
However, coding it as:
function validateGender($sex) {
if ($sex == 'M' or $sex == 'F')
return true;
else
return false;
}
...
...
if (validateGender($_GET['sex']) ...
This second one is more generic - it takes a value from any source, and
the original name can be anything (legal).
The second one is far more usable because it is far less restrictive
than the first one.
Make sense?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: $_GET request -> variable -> object? -> function
am 14.04.2007 04:48:10 von Jerry Stuckle
strawberry wrote:
> On Apr 13, 2:12 pm, Jerry Stuckle wrote:
>> Schraalhans Keukenmeester wrote:
>>> strawberry wrote:
>>>> On Apr 13, 1:51 am, "strawberry" wrote:
>>>>> I'm trying to extend a class (written by others) and was wondering if
>>>>> there's a standard a way of passing a $_GET request to a class. Here's
>>>>> what I have (see below). If I hardcode $where, something like
>>>>> $where = " WHERE `firstname` = 'John' "
>>>>> and then pass it as an argument into the scope of a 'select' function
>>>>> then it works fine, but otherwise the query just appears to hang.
>>>>> I'm deliberately not including the class or class extender at this
>>>>> stage, but I'll add them to this thread later if required.
>>>>> foreach ($_GET as $condition_key => $condition_value) {
>>>>> $condition[] =" $condition_key = '$condition_value' ";
>>>>> }
>>>>> if(is_null($condition)){
>>>>> $where = " WHERE 1 ";
>>>>> }else{
>>>>> $conditionString = implode('AND', $condition);
>>>>> $where = " WHERE $conditionString ";
>>>>> }
>>> You're reposing the question? Or ?
>>> I am not sure myself which is preferrable: having the class handle the
>>> $_GET superglobal array itself, which makes the client code programming
>>> even easier, or leaving it to the client code, allowing for more custom
>>> use. I've seen both examples, each has their practical (dis-)advantages.
>>> If someone has a good argument on why to prefer one over the other (or
>>> even an entirely different way) I'd also be helped!
>>> Sh.
>> The program should handle it, not the class.
>>
>> You should make your classes as generic as possible. If you parse the
>> values in the program then pass the parsed values to the object, your
>> code can be used in a number of ways. If you parse it in the class,
>> you're restricted to using the $_GET array - and those specific index
>> (field) names.
>>
>> For instance - what happens if you have the data stored in $_POST
>> instead of $_GET? Or $_SESSION? You can argue that you'd just pass the
>> proper superglobal to the object.
>>
>> However, what happens if on one page you have a checkbox "Sex" - and on
>> another page it's called "Gender"? Or maybe even just "sex"? You can't
>> do it if you process the superglobal. You can argue that you can make
>> it a condition that the field must be named "Sex". But I don't like
>> unwarranted restrictions like that.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Sorry Jerry, I'm confused. With the exception of "global $where",
> everything I've presented so far is in the program (test.php). Can you
> give me (or point me to) a practical illustration of what I should be
> doing to provide this functionality. Obviously if you need/would like
> to see more code, I can paste it here.
>
First of all, don't use $global. It creates a dependency between your
function and other parts of the code. If the function needs a variable,
pass it in the function call (by reference, if necessary).
As for an example:
function validateGender() {
if ($_GET['sex'] == 'M' or $_GET['sex'] == 'F')
return true;
else
return false;
}
...
...
if (validateGender()) ...
(Yes, I know you can do this with a ternary operator, but this is an
example).
The problem here is you have a required relationship between the form
and the function. Your form would have to have:
You couldn't have, for instance, "method=post". Nor could you have on
your radio buttons name="gender", or even name="Sex". And you couldn't
use it except from the form.
However, coding it as:
function validateGender($sex) {
if ($sex == 'M' or $sex == 'F')
return true;
else
return false;
}
...
...
if (validateGender($_GET['sex']) ...
This second one is more generic - it takes a value from any source, and
the original name can be anything (legal).
The second one is far more usable because it is far less restrictive
than the first one.
Make sense?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: $_GET request -> variable -> object? -> function
am 14.04.2007 04:50:45 von Steve
"strawberry" wrote in message
news:1176499040.433336.181510@y80g2000hsf.googlegroups.com.. .
| On Apr 13, 7:40 pm, "Steve" wrote:
| > "strawberry" wrote in message
| >
| > news:1176488231.812171.25480@o5g2000hsb.googlegroups.com...
| > On Apr 13, 6:23 pm, Rami Elomaa wrote:
| >
| > ============
| >
| > foreach ($_GET as $condition_key => $condition_value) {
| > $condition[] =" `$condition_key` = '$condition_value' ";
| > $condition_keys[] = "$condition_key";
| > $condition_values[] = "'$condition_value'";}
| >
| > if(is_null($condition)){
| > $conditionString = " 1 ";}else{
| >
| > $conditionString = implode('AND', $condition);
| > $conditionKeysString = implode(',',$condition_keys);
| > $conditionValuesString = implode(',',$condition_values);
| >
| > ============
| >
| > have you thought of always having criteria of WHERE 1 = 1 ? that way you
can
| > avoid having if/else logic. you can then always impode with AND.
|
| Good idea, what would that actually look like?
like this, perhaps:
function parseCriterion(&$seek, $column, $condition)
{
$seek = '`' .
$column .
'` ' .
$condition .
' ' .
"'" .
str_replace("'", "''", $seek) .
"'";
}
array_walk($_GET, 'parseCriterion', '=');
$conditions = array_values($_GET);
$sql = "
SELECT Bar
FROM Foo
WHERE 1 = 1
";
$criterion = implode('AND ', $conditions);
$sql .= $criterion;
so, we've made a one-line function to massage $_GET and it will be
enumerated with native code rather than a loop we write...which should speed
it up. we've reduced the amount of code to maintain and have included much
more functionality than the original - all in 5 lines of code...well, 6 (the
single line in our array_walk callback).
does that make sense? if there are no conditions, implode will not have the
slightest effect on $sql.
| > i'd also
| > recommend that you array_walk the columns ($cond_keys) and the values
| > ($cond_values) so that you can back-tick the columns and escape the
ticks
| > that may be present in the values. otherwise, you may have quite a
volitile
| > little query. :)
| >
|
| At the moment, the intention is to deploy this on a tiny office
| intranet so the security risks are neglible. That said, it's obviously
| very sensible advice, however, the author of the class
| has just informed that he's shortly going to publish a new version
| with this functionality built in - so I think I might just wait to see
| what that's all about.
well if this is the author's first attempt, i'd really look at the next as
it may be equally hackable and as verbosely written. remember, the less you
write, the faster it executes. it doesn't mean you sacrifice anything
either...just means you know what you're doing. :)
| > i'd also watch out for treating $_GET as global AND trustworthy. it's
better
| > to define the fields in your code and then set their prospective values
from
| > $_GET/$_POST/$_REQUEST/whatever. array_walk is great for that as well.
| > if i wanted to hack and ruin your site (down your mysql instance), i
could
| > introduce my own little condition and have it loop infinitely.
|
| That would be mean. Well, in the event of that happening, at least now
| I'd know where to come knocking.
lol. what's that url again?
| > just a thought.
|
| Thanks guys. It looks like this one will resolve itself in due course.
| Still, I'd like to figure out what's going on for the next time I run
| into this kind of problem.
what was the problem again? i don't really know that you need an object for
this portion of code. i think you need an object that stores the db column
name, it's textual description, validation (data type, required, unique in
db, the current value, etc.), maxlength, html input type...all that will
define for the developer what it is that is being done, from where the input
is taken...AND most importantly, you can use very little code in order to
produce the html, and you can apply validation very simply and formally. you
can even walk an array of these 'input' objects for both.
;^)
Re: $_GET request -> variable -> object? -> function
am 14.04.2007 04:50:45 von Steve
"strawberry" wrote in message
news:1176499040.433336.181510@y80g2000hsf.googlegroups.com.. .
| On Apr 13, 7:40 pm, "Steve" wrote:
| > "strawberry" wrote in message
| >
| > news:1176488231.812171.25480@o5g2000hsb.googlegroups.com...
| > On Apr 13, 6:23 pm, Rami Elomaa wrote:
| >
| > ============
| >
| > foreach ($_GET as $condition_key => $condition_value) {
| > $condition[] =" `$condition_key` = '$condition_value' ";
| > $condition_keys[] = "$condition_key";
| > $condition_values[] = "'$condition_value'";}
| >
| > if(is_null($condition)){
| > $conditionString = " 1 ";}else{
| >
| > $conditionString = implode('AND', $condition);
| > $conditionKeysString = implode(',',$condition_keys);
| > $conditionValuesString = implode(',',$condition_values);
| >
| > ============
| >
| > have you thought of always having criteria of WHERE 1 = 1 ? that way you
can
| > avoid having if/else logic. you can then always impode with AND.
|
| Good idea, what would that actually look like?
like this, perhaps:
function parseCriterion(&$seek, $column, $condition)
{
$seek = '`' .
$column .
'` ' .
$condition .
' ' .
"'" .
str_replace("'", "''", $seek) .
"'";
}
array_walk($_GET, 'parseCriterion', '=');
$conditions = array_values($_GET);
$sql = "
SELECT Bar
FROM Foo
WHERE 1 = 1
";
$criterion = implode('AND ', $conditions);
$sql .= $criterion;
so, we've made a one-line function to massage $_GET and it will be
enumerated with native code rather than a loop we write...which should speed
it up. we've reduced the amount of code to maintain and have included much
more functionality than the original - all in 5 lines of code...well, 6 (the
single line in our array_walk callback).
does that make sense? if there are no conditions, implode will not have the
slightest effect on $sql.
| > i'd also
| > recommend that you array_walk the columns ($cond_keys) and the values
| > ($cond_values) so that you can back-tick the columns and escape the
ticks
| > that may be present in the values. otherwise, you may have quite a
volitile
| > little query. :)
| >
|
| At the moment, the intention is to deploy this on a tiny office
| intranet so the security risks are neglible. That said, it's obviously
| very sensible advice, however, the author of the class
| has just informed that he's shortly going to publish a new version
| with this functionality built in - so I think I might just wait to see
| what that's all about.
well if this is the author's first attempt, i'd really look at the next as
it may be equally hackable and as verbosely written. remember, the less you
write, the faster it executes. it doesn't mean you sacrifice anything
either...just means you know what you're doing. :)
| > i'd also watch out for treating $_GET as global AND trustworthy. it's
better
| > to define the fields in your code and then set their prospective values
from
| > $_GET/$_POST/$_REQUEST/whatever. array_walk is great for that as well.
| > if i wanted to hack and ruin your site (down your mysql instance), i
could
| > introduce my own little condition and have it loop infinitely.
|
| That would be mean. Well, in the event of that happening, at least now
| I'd know where to come knocking.
lol. what's that url again?
| > just a thought.
|
| Thanks guys. It looks like this one will resolve itself in due course.
| Still, I'd like to figure out what's going on for the next time I run
| into this kind of problem.
what was the problem again? i don't really know that you need an object for
this portion of code. i think you need an object that stores the db column
name, it's textual description, validation (data type, required, unique in
db, the current value, etc.), maxlength, html input type...all that will
define for the developer what it is that is being done, from where the input
is taken...AND most importantly, you can use very little code in order to
produce the html, and you can apply validation very simply and formally. you
can even walk an array of these 'input' objects for both.
;^)
Re: $_GET request -> variable -> object? -> function
am 14.04.2007 15:42:53 von Jerry Stuckle
strawberry wrote:
> On Apr 13, 6:23 pm, Rami Elomaa wrote:
>> Vince Morgan kirjoitti:
>>
>>
>>
>>> "strawberry" wrote in message
>>> news:1176450464.524325.95990@n59g2000hsh.googlegroups.com...
>>>> On Apr 13, 1:51 am, "strawberry" wrote:
>>>>> I'm trying to extend a class (written by others) and was wondering if
>>>>> there's a standard a way of passing a $_GET request to a class. Here's
>>>>> what I have (see below). If I hardcode $where, something like
>>>>> $where = " WHERE `firstname` = 'John' "
>>>>> and then pass it as an argument into the scope of a 'select' function
>>>>> then it works fine, but otherwise the query just appears to hang.
>>>>> I'm deliberately not including the class or class extender at this
>>>>> stage, but I'll add them to this thread later if required.
>>>>> foreach ($_GET as $condition_key => $condition_value) {
>>>>> $condition[] =" $condition_key = '$condition_value' ";
>>>>> }
>>>>> if(is_null($condition)){
>>>>> $where = " WHERE 1 ";
>>>>> }else{
>>>>> $conditionString = implode('AND', $condition);
>>>>> $where = " WHERE $conditionString ";
>>> The line above is looking for a value from a field named $conditionString.
>> No, it doesn't. Variables inside a double quoted string are parsed. It's
>> perfectly valid code. The example you gave would also work, but it's
>> totally unnecessary.
>>
>> Just try this and you'll learn something fun:
>>
>>
>>
>> $cat = 'dog';
>>
>> echo "I have an animal in my hand and the animal is a $cat.
";
>> echo 'I have an animal in my hand and the animal is a $cat.
';
>>
>> ?>
>>
>> --
>> Rami.Elo...@gmail.com
>>
>> "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
>> usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
>
> Thanks Rami,
>
> I knew from the echoes that my statements were valid. Unfortunately,
> the mysrc class doesn't seem to agree with me :-(
>
> To recap. No matter what I try I just can't seem to pass this...
>
> foreach ($_GET as $condition_key => $condition_value) {
> $condition[] =" `$condition_key` = '$condition_value' ";
> $condition_keys[] = "$condition_key";
> $condition_values[] = "'$condition_value'";
> }
> if(is_null($condition)){
> $conditionString = " 1 ";
> }else{
> $conditionString = implode('AND', $condition);
> $conditionKeysString = implode(',',$condition_keys);
> $conditionValuesString = implode(',',$condition_values);
> }
>
> as arguments to this...
>
> class mysrc extends drasticsrcmysql {
> protected function select($conditionString){
> $query = "SELECT * FROM $this->table WHERE $conditionString";
> $res = mysql_query($query . $this->orderbystr, $this->conn) or
> die(mysql_error());
> return ($res);
> }
>
> protected function add($conditionKeysString,$conditionValuesString){
> $query = "INSERT INTO $this->table ($conditionKeysString) VALUES
> ($conditionValuesString)";
> mysql_query($query, $this->conn) or die(mysql_error());
> if (mysql_affected_rows($this->conn) == 1) return(true); else
> return(false);
> }
>
> ...unless I pass a bogus column name: Unknown column
> 'false_column_name' in 'where clause'
>
Without trying to figure out what all this convoluted code does (I'd
never do it that way - simplicity is better) - what do you get when you
echo $query?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: $_GET request -> variable -> object? -> function
am 14.04.2007 15:42:53 von Jerry Stuckle
strawberry wrote:
> On Apr 13, 6:23 pm, Rami Elomaa wrote:
>> Vince Morgan kirjoitti:
>>
>>
>>
>>> "strawberry" wrote in message
>>> news:1176450464.524325.95990@n59g2000hsh.googlegroups.com...
>>>> On Apr 13, 1:51 am, "strawberry" wrote:
>>>>> I'm trying to extend a class (written by others) and was wondering if
>>>>> there's a standard a way of passing a $_GET request to a class. Here's
>>>>> what I have (see below). If I hardcode $where, something like
>>>>> $where = " WHERE `firstname` = 'John' "
>>>>> and then pass it as an argument into the scope of a 'select' function
>>>>> then it works fine, but otherwise the query just appears to hang.
>>>>> I'm deliberately not including the class or class extender at this
>>>>> stage, but I'll add them to this thread later if required.
>>>>> foreach ($_GET as $condition_key => $condition_value) {
>>>>> $condition[] =" $condition_key = '$condition_value' ";
>>>>> }
>>>>> if(is_null($condition)){
>>>>> $where = " WHERE 1 ";
>>>>> }else{
>>>>> $conditionString = implode('AND', $condition);
>>>>> $where = " WHERE $conditionString ";
>>> The line above is looking for a value from a field named $conditionString.
>> No, it doesn't. Variables inside a double quoted string are parsed. It's
>> perfectly valid code. The example you gave would also work, but it's
>> totally unnecessary.
>>
>> Just try this and you'll learn something fun:
>>
>>
>>
>> $cat = 'dog';
>>
>> echo "I have an animal in my hand and the animal is a $cat.
";
>> echo 'I have an animal in my hand and the animal is a $cat.
';
>>
>> ?>
>>
>> --
>> Rami.Elo...@gmail.com
>>
>> "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
>> usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
>
> Thanks Rami,
>
> I knew from the echoes that my statements were valid. Unfortunately,
> the mysrc class doesn't seem to agree with me :-(
>
> To recap. No matter what I try I just can't seem to pass this...
>
> foreach ($_GET as $condition_key => $condition_value) {
> $condition[] =" `$condition_key` = '$condition_value' ";
> $condition_keys[] = "$condition_key";
> $condition_values[] = "'$condition_value'";
> }
> if(is_null($condition)){
> $conditionString = " 1 ";
> }else{
> $conditionString = implode('AND', $condition);
> $conditionKeysString = implode(',',$condition_keys);
> $conditionValuesString = implode(',',$condition_values);
> }
>
> as arguments to this...
>
> class mysrc extends drasticsrcmysql {
> protected function select($conditionString){
> $query = "SELECT * FROM $this->table WHERE $conditionString";
> $res = mysql_query($query . $this->orderbystr, $this->conn) or
> die(mysql_error());
> return ($res);
> }
>
> protected function add($conditionKeysString,$conditionValuesString){
> $query = "INSERT INTO $this->table ($conditionKeysString) VALUES
> ($conditionValuesString)";
> mysql_query($query, $this->conn) or die(mysql_error());
> if (mysql_affected_rows($this->conn) == 1) return(true); else
> return(false);
> }
>
> ...unless I pass a bogus column name: Unknown column
> 'false_column_name' in 'where clause'
>
Without trying to figure out what all this convoluted code does (I'd
never do it that way - simplicity is better) - what do you get when you
echo $query?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: $_GET request -> variable -> object? -> function
am 17.04.2007 00:06:41 von zac.carey
On Apr 14, 2:42 pm, Jerry Stuckle wrote:
> strawberry wrote:
> > On Apr 13, 6:23 pm, Rami Elomaa wrote:
> >> Vince Morgan kirjoitti:
>
> >>> "strawberry" wrote in message
> >>>news:1176450464.524325.95990@n59g2000hsh.googlegroups.com ...
> >>>> On Apr 13, 1:51 am, "strawberry" wrote:
> >>>>> I'm trying to extend a class (written by others) and was wondering =
if
> >>>>> there's a standard a way of passing a $_GET request to a class. Her=
e's
> >>>>> what I have (see below). If I hardcode $where, something like
> >>>>> $where =3D " WHERE `firstname` =3D 'John' "
> >>>>> and then pass it as an argument into the scope of a 'select' functi=
on
> >>>>> then it works fine, but otherwise the query just appears to hang.
> >>>>> I'm deliberately not including the class or class extender at this
> >>>>> stage, but I'll add them to this thread later if required.
> >>>>> foreach ($_GET as $condition_key =3D> $condition_value) {
> >>>>> $condition[] =3D" $condition_key =3D '$condition_value' ";
> >>>>> }
> >>>>> if(is_null($condition)){
> >>>>> $where =3D " WHERE 1 ";
> >>>>> }else{
> >>>>> $conditionString =3D implode('AND', $condition);
> >>>>> $where =3D " WHERE $conditionString ";
> >>> The line above is looking for a value from a field named $conditionSt=
ring.
> >> No, it doesn't. Variables inside a double quoted string are parsed. It=
's
> >> perfectly valid code. The example you gave would also work, but it's
> >> totally unnecessary.
>
> >> Just try this and you'll learn something fun:
>
> >>
>
> >> $cat =3D 'dog';
>
> >> echo "I have an animal in my hand and the animal is a $cat.
";
> >> echo 'I have an animal in my hand and the animal is a $cat.
';
>
> >> ?>
>
> >> --
> >> Rami.Elo...@gmail.com
>
> >> "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmil=
taan
> >> usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
>
> > Thanks Rami,
>
> > I knew from the echoes that my statements were valid. Unfortunately,
> > the mysrc class doesn't seem to agree with me :-(
>
> > To recap. No matter what I try I just can't seem to pass this...
>
> > foreach ($_GET as $condition_key =3D> $condition_value) {
> > $condition[] =3D" `$condition_key` =3D '$condition_value' ";
> > $condition_keys[] =3D "$condition_key";
> > $condition_values[] =3D "'$condition_value'";
> > }
> > if(is_null($condition)){
> > $conditionString =3D " 1 ";
> > }else{
> > $conditionString =3D implode('AND', $condition);
> > $conditionKeysString =3D implode(',',$condition_keys);
> > $conditionValuesString =3D implode(',',$condition_values);
> > }
>
> > as arguments to this...
>
> > class mysrc extends drasticsrcmysql {
> > protected function select($conditionString){
> > $query =3D "SELECT * FROM $this->table WHERE $conditionString";
> > $res =3D mysql_query($query . $this->orderbystr, $this->conn) or
> > die(mysql_error());
> > return ($res);
> > }
>
> > protected function add($conditionKeysString,$conditionValuesString){
> > $query =3D "INSERT INTO $this->table ($conditionKeysString) VALUES
> > ($conditionValuesString)";
> > mysql_query($query, $this->conn) or die(mysql_error());
> > if (mysql_affected_rows($this->conn) == 1) return(true); else
> > return(false);
> > }
>
> > ...unless I pass a bogus column name: Unknown column
> > 'false_column_name' in 'where clause'
>
> Without trying to figure out what all this convoluted code does (I'd
> never do it that way - simplicity is better) - what do you get when you
> echo $query?
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================
This from the author:
"The grid uses an Ajax call to get the data into it.
This Ajax call is built within the javascript code and it calls the
same .php file from which the javascript was invoked with a number of
new GET variables. This second call again reaches your code and then
this code stores the new GET variables into the defaultcols setting,
which causes it to build a WHERE string that does not make sense."
So now I'm going to try using cookies instead,
Re: $_GET request -> variable -> object? -> function
am 17.04.2007 00:06:41 von zac.carey
On Apr 14, 2:42 pm, Jerry Stuckle wrote:
> strawberry wrote:
> > On Apr 13, 6:23 pm, Rami Elomaa wrote:
> >> Vince Morgan kirjoitti:
>
> >>> "strawberry" wrote in message
> >>>news:1176450464.524325.95990@n59g2000hsh.googlegroups.com ...
> >>>> On Apr 13, 1:51 am, "strawberry" wrote:
> >>>>> I'm trying to extend a class (written by others) and was wondering =
if
> >>>>> there's a standard a way of passing a $_GET request to a class. Her=
e's
> >>>>> what I have (see below). If I hardcode $where, something like
> >>>>> $where =3D " WHERE `firstname` =3D 'John' "
> >>>>> and then pass it as an argument into the scope of a 'select' functi=
on
> >>>>> then it works fine, but otherwise the query just appears to hang.
> >>>>> I'm deliberately not including the class or class extender at this
> >>>>> stage, but I'll add them to this thread later if required.
> >>>>> foreach ($_GET as $condition_key =3D> $condition_value) {
> >>>>> $condition[] =3D" $condition_key =3D '$condition_value' ";
> >>>>> }
> >>>>> if(is_null($condition)){
> >>>>> $where =3D " WHERE 1 ";
> >>>>> }else{
> >>>>> $conditionString =3D implode('AND', $condition);
> >>>>> $where =3D " WHERE $conditionString ";
> >>> The line above is looking for a value from a field named $conditionSt=
ring.
> >> No, it doesn't. Variables inside a double quoted string are parsed. It=
's
> >> perfectly valid code. The example you gave would also work, but it's
> >> totally unnecessary.
>
> >> Just try this and you'll learn something fun:
>
> >>
>
> >> $cat =3D 'dog';
>
> >> echo "I have an animal in my hand and the animal is a $cat.
";
> >> echo 'I have an animal in my hand and the animal is a $cat.
';
>
> >> ?>
>
> >> --
> >> Rami.Elo...@gmail.com
>
> >> "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmil=
taan
> >> usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
>
> > Thanks Rami,
>
> > I knew from the echoes that my statements were valid. Unfortunately,
> > the mysrc class doesn't seem to agree with me :-(
>
> > To recap. No matter what I try I just can't seem to pass this...
>
> > foreach ($_GET as $condition_key =3D> $condition_value) {
> > $condition[] =3D" `$condition_key` =3D '$condition_value' ";
> > $condition_keys[] =3D "$condition_key";
> > $condition_values[] =3D "'$condition_value'";
> > }
> > if(is_null($condition)){
> > $conditionString =3D " 1 ";
> > }else{
> > $conditionString =3D implode('AND', $condition);
> > $conditionKeysString =3D implode(',',$condition_keys);
> > $conditionValuesString =3D implode(',',$condition_values);
> > }
>
> > as arguments to this...
>
> > class mysrc extends drasticsrcmysql {
> > protected function select($conditionString){
> > $query =3D "SELECT * FROM $this->table WHERE $conditionString";
> > $res =3D mysql_query($query . $this->orderbystr, $this->conn) or
> > die(mysql_error());
> > return ($res);
> > }
>
> > protected function add($conditionKeysString,$conditionValuesString){
> > $query =3D "INSERT INTO $this->table ($conditionKeysString) VALUES
> > ($conditionValuesString)";
> > mysql_query($query, $this->conn) or die(mysql_error());
> > if (mysql_affected_rows($this->conn) == 1) return(true); else
> > return(false);
> > }
>
> > ...unless I pass a bogus column name: Unknown column
> > 'false_column_name' in 'where clause'
>
> Without trying to figure out what all this convoluted code does (I'd
> never do it that way - simplicity is better) - what do you get when you
> echo $query?
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================
This from the author:
"The grid uses an Ajax call to get the data into it.
This Ajax call is built within the javascript code and it calls the
same .php file from which the javascript was invoked with a number of
new GET variables. This second call again reaches your code and then
this code stores the new GET variables into the defaultcols setting,
which causes it to build a WHERE string that does not make sense."
So now I'm going to try using cookies instead,