Page 1 of 1
multidimensional array building a query
Posted: Mon May 23, 2011 2:37 pm
by bowersbros
Right, i thought i could do it, but my head hurts currently.. so..
Any advice?
basically, if i have an array setup like
array('users' => array('username' => 'value','password','email'));
I need to have it so that It gets built into a mysql query as so:
SELECT * FROM users WHERE username = 'value' AND password = 'value' AND email = 'value'";
for example (written badly, purely for ease of use)
But..
Also, i need to know, is tehre a way to use 1 query to run multiple table checks? or will I need to run a query per array key? (like if the array was setup to have 'users' and 'login' or something?
Re: multidimensional array building a query
Posted: Mon May 23, 2011 2:45 pm
by jacek
foreach ($array[1] as $column => $value){
$conditions[] = "`{$column} = '{$value}'";
}
$sql = 'SELECT `things` FROM `' . $array[0] . '` WHERE ' . implode(" AND ", $conditions);
Something like this would do it I think...
And I have no idea what you mean by the second thing.
Re: multidimensional array building a query
Posted: Mon May 23, 2011 2:46 pm
by bowersbros
why do you start with array[1] ?
Re: multidimensional array building a query
Posted: Mon May 23, 2011 2:48 pm
by jacek
bowersbros wrote:why do you start with array[1] ?
Because you did
array('users' => array('username' => 'value','password','email'));
is element 0 not the table name ?
Re: multidimensional array building a query
Posted: Mon May 23, 2011 2:49 pm
by bowersbros
elemt 0 is the table name, but (but the key is the table name, not the value) then the value of 0 is the multi array, where the key to them are the field names and the values to those respectively are the values to check for.
Re: multidimensional array building a query
Posted: Mon May 23, 2011 2:52 pm
by jacek
Oh I see, sorry I misread.
It might be better not to structure it like this, it's making it too awkward to work with
Re: multidimensional array building a query
Posted: Mon May 23, 2011 2:52 pm
by bowersbros
Okay, so we'll have to do it as Normal array or static string. got it ;D
/edit. kind of makes that array_is_multi function useless now though?
Re: multidimensional array building a query
Posted: Mon May 23, 2011 2:56 pm
by jacek
bowersbros wrote:/edit. kind of makes that array_is_multi function useless now though?
Well it might be useful for something else
Re: multidimensional array building a query
Posted: Mon May 23, 2011 2:58 pm
by bowersbros
jacek wrote:bowersbros wrote:/edit. kind of makes that array_is_multi function useless now though?
Well it might be useful for something else
used it anyway, so if one is submitted it will fail
and added a little //maybe one day comment