|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
MySQL 5 - Returning a array from a database connect
So I'm looking through my code and it occurs to me that i chiefly use the same 4 lines of code over and over again to connect to different tables in the same database and i got to thinking "is their a way to turn this into a PHP custom function and only call that?"
PHP Code:
And basically all i really would change is the table name and the related variable names but if i made a custom function i could simply that. But would i be able to return a workable array though a function. Something like PHP Code:
then then i could just run PHP Code:
What's more would this custom function produce something i could loop though a while or for loop to get more then one entry?
__________________
29 years of creative writing 13 years of HTML 10 years of Photoshop 6 years of PHP/MySQL And I never knew Photoshop could do HTML until 2004! You learn something new every day. |
|
#2
|
|||
|
|||
|
there is nothing wrong with returning an array from a function. One of the query functions from my custom database class looks like this:
PHP Code:
it will actually process the query and get a multi-dimensional array of the result and return that. then I only have to work with the array. It also can take a second argument that would be a column name and then makes that the key value for the 1st dimension if needed. I also have a query1 function that will just return the first row from a result and that figures out how many columns and if only 1, it return that value, if more than 1 then it return an array of that row. pretty handy. using this I could query for all users for example: $users = $DB->querya("SELECT * FROM Users", 'username'); taking advantage of the 2nd argument I could then access each user like: echo $users['someonesUserName']['username']; a sloppy example, but it works. also note that this is part of a complete class. so by itself this function won't work because of all the $this calls and self calls (the connection is static). |
|
#3
|
|||
|
|||
|
well looks promising, a bit complicated but i'll mull over your sample i think it could streamline my code quite a bit once i muddle though all the specifics
my only concern is wouldi need to query the database multiple times to get data from the same table ie $users = $DB->querya("SELECT * FROM Users where xxxx", 'username'); $users = $DB->querya("SELECT * FROM Users where xxxx", 'password'); $users = $DB->querya("SELECT * FROM Users where xxxx", 'address'); |
|
#4
|
|||
|
|||
|
nope. if you leave off the second argument it would normally just be an array of each row. so if you had a table like:
Code:
table: users id | username | firstName | lastName 1 | test | test | person 2 | user | john | smith 3 | name | susan | jones 4 | admin | brian | jackson if I query for: $users = $DB->querya("SELECT * FROM USERS"); my array ($users) would look like: Code:
Array
(
[0] => Array
(
[id] => 1
[username] => test
[firstName] => test
[lastName] => person
)
[1] => Array
(
[id] => 1
[username] => user
[firstName] => john
[lastName] => smith
)
[2] => Array
(
[id] => 1
[username] => name
[firstName] => susan
[lastName] => jones
)
[3] => Array
(
[id] => 1
[username] => admin
[firstName] => brian
[lastName] => jackson
)
)
if I queried like so: $users = $DB->querya("SELECT * FROM USERS", 'username'); then $users would look like: Code:
Array
(
[test] => Array
(
[id] => 1
[username] => test
[firstName] => test
[lastName] => person
)
[user] => Array
(
[id] => 1
[username] => user
[firstName] => john
[lastName] => smith
)
[name] => Array
(
[id] => 1
[username] => name
[firstName] => susan
[lastName] => jones
)
[admin] => Array
(
[id] => 1
[username] => admin
[firstName] => brian
[lastName] => jackson
)
)
the difference is in how I can access that data after that. the first user in the first example can be accessed by $users[0], in the second one I have just switched the numeric array with the username as the key. so I can get a specific user like the one named admin by using $users['admin']. probably a bad example, but if there are not a lot of users (like 10) then I can just query for all the users and do something like if(isset($users['admin']) echo "user admin found."; Last edited by IAmALlama : August 11th, 2009 at 06:13 PM. |
|
#5
|
|||
|
|||
|
Quote:
i see, and thats a lot more functional then what i was doing. thanks that looks very promising Edit: so looking at the function (i have very limited experiences with classes) im guess all the $this-> have to be changed. But honestly i'm not sure how shouldi be trying to take this int oa stand along function or just figure out classes instead? PHP Code:
My horibly butched atempt to make it a stand alone function Last edited by LLX : August 11th, 2009 at 07:22 PM. |
|
#6
|
|||
|
|||
|
for the most part you got it. all the calls to $this->somevar just means that it is referencing a function/variable outside of the current function but still inside the class container. I didn't test this, but this is what you should use. I also commented everything.
PHP Code:
|
|
#7
|
|||
|
|||
|
thanks Llama, i'll toss in a coment credit to you somewhere even if only i can see it. This is a nice productivity booster
|
|
#8
|
|||
|
|||
|
a thought occured to me
So now i have my multi dimensional array but how to i dynamlly post data from it. Is it as simple as for ($x = 0; $users = $DB->querya("SELECT * FROM USERS", 'username'); $x++){$user[$x]['username'];} ? |
|
#9
|
|||
|
|||
|
I don't understand what you mean by "post data from it" but if you want to just loop through the data, use a foreach loop.
PHP Code:
also just know that the column names and keys from the resulting array are case sensitive. so in the second argument where you specify a key value, it has to be the same case as the column name in the database. or if you specify the columns in your query (select col1,col2...etc), the case has to match the column in the select. |
|
#10
|
|||
|
|||
|
yup that what i wanted, since they were be cases when i want a single roy pull and a entire table pulled from the querry. Thanks again.
|
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > MySQL 5 - Returning a array from a database connect |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|