me102 Posted August 31, 2006 Share Posted August 31, 2006 My question is there an easyer way to get larg amounts of data from a sql database heres the code i'm using now.[code]$SQL = "select * from online_logs where forumid='$forum_id' order by `id` desc limit 1";$result = mysql_query( $SQL );while( $row = mysql_fetch_array( $result ) ) { $set_ip = $row["ip"]; $set_userid = $row["userid"]; $set_id2 = $row["id2"]; }[/code]instead of listing each row and giving them a value for ex. $set_ip = $row["ip"];Any ideas? Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted August 31, 2006 Share Posted August 31, 2006 Well it seems like you are limiting your query to just one row in your sql query:[code]$SQL = "select * from online_logs where forumid='$forum_id' order by `id` desc limit 1";[/code]Take off the "limit 1" and you should get every matching row:[code]$SQL = "select * from online_logs where forumid='$forum_id' order by `id` desc";[/code] Quote Link to comment Share on other sites More sharing options...
me102 Posted August 31, 2006 Author Share Posted August 31, 2006 No what I mean is I dont want to write this out,[code]$set_ip = $row["ip"];$set_userid = $row["userid"];$set_id2 = $row["id2"];[/code] Quote Link to comment Share on other sites More sharing options...
extrovertive Posted August 31, 2006 Share Posted August 31, 2006 [quote author=me102 link=topic=106342.msg425154#msg425154 date=1156998288]My question is there an easyer way to get larg amounts of data from a sql database heres the code i'm using now.[code]$SQL = "select * from online_logs where forumid='$forum_id' order by `id` desc limit 1";$result = mysql_query( $SQL );while( $row = mysql_fetch_array( $result ) ) { $set_ip = $row["ip"]; $set_userid = $row["userid"]; $set_id2 = $row["id2"]; }[/code]instead of listing each row and giving them a value for ex. $set_ip = $row["ip"];Any ideas?[/quote]If you already know what columns, you are getting, no need to use "select *..." just use $SQL = "select ip, userid, id2 from online_logs where forumid='$forum_id' order by `id`";then to execute the query:[code]$result = mysql_query( $SQL ); while(list($set_ip, $set_userid, $set_id2) = mysql_fetch_array( $result ) ) { //something here }[/code]Or, if you want the column field to be a variable in your PHP script:[code]$result = mysql_query( $SQL ); while($row = mysql_fetch_array( $result ) ) { foreach($row as $variable=>$value) { $$variable = $value; } /* After the loop terminates, you'll have the variables $ip, $userid, $id2 with its respective values */ //something here }[/code] Quote Link to comment Share on other sites More sharing options...
fenway Posted August 31, 2006 Share Posted August 31, 2006 Definitely use the former, if at all possible -- polluting the namespace is a bad idea (even though PHP doesn't really have them). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.