Jump to content

Archived

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

me102

Mysql question

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?

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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]


Share this post


Link to post
Share on other sites
Definitely use the former, if at all possible -- polluting the namespace is a bad idea (even though PHP doesn't really have them).

Share this post


Link to post
Share on other sites

×

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.