Closed Thread Icon

Topic awaiting preservation: MySQL query for EVERYthing (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=12474" title="Pages that link to Topic awaiting preservation: MySQL query for EVERYthing (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: MySQL query for EVERYthing <span class="small">(Page 1 of 1)</span>\

 
Red Ninja
Bipolar (III) Inmate

From: Detroit, MI US
Insane since: Mar 2001

posted posted 10-17-2002 15:38

If I were to write mysql_query("select * from table");

How would I put that into a multi-dimensional array in php? The point is that I want to gather everything from a table without having to use a while loop and query the database every time through (If I'm not mistaken, this is pretty much what is done with mysql_fetch_row, isn't it?).

Is there a way to do this? Am I being unreasonable? Does it even matter?

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 10-17-2002 18:15

I don't know of a way to do this without at least one while loop. mysql_fetch_array gets an array for a row but I don't think there's a mysql command for getting the whole result set into a multi-dimensional array. Easy enough without though

$result = mysql_query("SELECT * FROM table");
$multi_array = array();
while($row = mysql_fetch_row($result)) {
array_push($multi_array, mysql_fetch_array($row));
}

Oh I don't know what you mean about "everything" and without seperate queries but if you mean from all tables then you can list all tables seperated by commas

SELECT * FROM table1, table2, table3 ....

You can use the mysql_field_tables function to get all the tables in a db as well.




.:[ Never resist a perfect moment ]:.

[This message has been edited by bitdamaged (edited 10-17-2002).]

Red Ninja
Bipolar (III) Inmate

From: Detroit, MI US
Insane since: Mar 2001

posted posted 10-17-2002 21:11

It was just everything from one table. What I meant was doing a while loop and having to query a different row every time through the while loop. I never hear of anything like that, wondered if anybody had.

However, I did still learn from this post. That was a pretty good little bit of code you gave me there; much shorter than what I was going to write. Doomo, bitu damajiyudu-san.

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 10-17-2002 21:15

RN: I load all the data into arrays for further manipulation (very handy for recursive functions) using code like this:

code:
$data_count = 0;

/* we load the node data into an array */

while ($row = mysql_fetch_array($result)) {

$data_array[$data_count]['id'] = $row['id'];
$data_array[$data_count]['name'] = $row['name'];
ETC

$data_count++;

}



Is that what you were looking for?

___________________
Emps

FAQs: Emperor

Red Ninja
Bipolar (III) Inmate

From: Detroit, MI US
Insane since: Mar 2001

posted posted 10-17-2002 21:47

No. I pretty much was figuring that I'd be doing something along those lines, because there is still a while loop. What I was looking for is probably best described as:

$query = mysql_query("SELECT * FROM table");

some_obscure_php_function($result = mysql_fetch_array($query));

or maybe it would even have been written like this:

$query = mysql_query("SELECT * FROM table");

$result = some_obscure_php_function($query);

Who knows? It probably doesn't exist. I just wondered if it had. I wouldn't have even brought it up if I hadn't seen that "SELECT * FROM table" was a valid query. It seems odd to me that you would be able to select everything and yet it would still yield only one line of information. It's kind of like Planet X. Nobody's ever seen it, but since gravity theory indicates that there is a possibility that it could exist one has to wonder...

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-17-2002 22:46

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 10-18-2002 00:21

Emps, I'm still getting my head around it but I'm pretty sure your code is actually doing the same thing as mine actually. just a little longer.





.:[ Never resist a perfect moment ]:.

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 10-18-2002 17:57

RN,
mysql_fetch_row and the like certainly don't query the database when you call them. Only mysql_query does. That result is then buffered. So calls to the _fetch_xxx functionas read from that buffer. Therefore, I don't see a problem with using a while loop at all.

yours,

Tyberius Prime

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 10-18-2002 18:28

This is how I do it:

code:
//Generates the connection with the database and returns a 
//valid connection for the one calling.
function hookUpDb(){
Global $connection;
/*config connection to database*/
$host = '';
$user = '';
$password = '';
$db = '';
/*connect to the database*/
if(mysql_connect($host,$user,$password)){
$connection = mysql_connect($host,$user,$password);
mysql_select_db($db,$connection);
return $connection;
}else{
print("<b>Sorry, cannot connect to the database.</b>");
}
}

//Use this when you need to use custom made SQL-queries on the database.
//Returns a 2 dimensional array with the result.
function doQueryResult($sql){
Global $connection;
hookUpDb();
if($sql != ""){
$result = mysql_query($sql,$connection);
$item = array();
while($item = mysql_fetch_array($result)){
$items[] = $item;
}
return $items;
mysql_free_result($result);
} else {
print("Need correctly formed SQL that returns a resultset.");
}
mysql_close($connection);
}



My CMS is built around this principle, I use one include to hold all db-access functions, each returns a 2-dimensional array to whatever called it. Then I have one include that holds functions that call on the db-functions, processes the results and turns it into HTML or whatever.
/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 10-18-2002 18:45

bitdamaged: I'm pretty sure it does the same thing I just wanted a bit of extra control over the naming of things in mine script as I have been changing the names of some of my tables, etc. and the way I do it means that you don't have to change the whole script just the SQL and the bit that loads data into an array. I'll swap the neatness for the greater flexibility

___________________
Emps

FAQs: Emperor

Red Ninja
Bipolar (III) Inmate

From: Detroit, MI US
Insane since: Mar 2001

posted posted 10-21-2002 18:39

[edit]Move along. Nothing to see here.[/edit]

[This message has been edited by Red Ninja (edited 10-21-2002).]

« BackwardsOnwards »

Show Forum Drop Down Menu