Jump to content


Photo

Mysql question


  • Please log in to reply
4 replies to this topic

#1 me102

me102
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 31 August 2006 - 04:24 AM

My question is there an easyer way to get larg amounts of data from a sql database heres the code i'm using now.


$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"];
	}

instead of listing each row and giving them a value for ex. $set_ip = $row["ip"];


Any ideas?

#2 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 31 August 2006 - 04:29 AM

Well it seems like you are limiting your query to just one row in your sql query:

$SQL = "select * from online_logs where forumid='$forum_id' order by `id` desc limit 1";

Take off the "limit 1" and you should get every matching row:

$SQL = "select * from online_logs where forumid='$forum_id' order by `id` desc";

What is the CoronaMatrix?

#3 me102

me102
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 31 August 2006 - 04:49 AM

No what I mean is I dont want to write this out,
$set_ip = $row["ip"];
$set_userid = $row["userid"];
$set_id2 = $row["id2"];


#4 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 31 August 2006 - 07:25 AM

My question is there an easyer way to get larg amounts of data from a sql database heres the code i'm using now.


$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"];
	}

instead of listing each row and giving them a value for ex. $set_ip = $row["ip"];


Any ideas?


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:

$result = mysql_query( $SQL );
            while(list($set_ip, $set_userid, $set_id2) = mysql_fetch_array( $result ) ) 
	{
	//something here
	}

Or, if you want the column field to be a variable in your PHP script:
$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
	}




#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 31 August 2006 - 01:45 PM

Definitely use the former, if at all possible -- polluting the namespace is a bad idea (even though PHP doesn't really have them).
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users