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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.