Jump to content

Mysql question


me102

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/19229-mysql-question/
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/19229-mysql-question/#findComment-83275
Share on other sites

[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]


Link to comment
https://forums.phpfreaks.com/topic/19229-mysql-question/#findComment-83301
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.