multidimensional array building a query

Ask about a PHP problem here.
Post Reply
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

multidimensional array building a query

Post 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?
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: multidimensional array building a query

Post 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.
Image
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: multidimensional array building a query

Post by bowersbros »

why do you start with array[1] ?
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: multidimensional array building a query

Post 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 ?
Image
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: multidimensional array building a query

Post 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.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: multidimensional array building a query

Post 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 ;)
Image
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: multidimensional array building a query

Post 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? ;)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: multidimensional array building a query

Post by jacek »

bowersbros wrote:/edit. kind of makes that array_is_multi function useless now though? ;)
Well it might be useful for something else ;)
Image
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: multidimensional array building a query

Post 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 :P

and added a little //maybe one day comment ;)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
Post Reply