Jump to content

Reuse MYSQL query result in PHP


sp@rky13

Recommended Posts

How can I reuse a query result on the same page. I need the same query result in 2 different places on the page. How can I duplicate it without rerunning the query. The query involves having this on the page:

 

<?php if ($advanced=='on')
	include "advanced.html";
	else
	include "vpt.html";?>

 

How can I put the result of that in a second place?

Link to comment
Share on other sites

The two different syntaxes are functionally equivalent for that specific code. Single statements don't need to be enclosed in {} and include is not a function so the () are not required.

 

If your question is how to reuse the result set from one query after your have iterated over it once, see this link - http://us.php.net/mysql_data_seek

Link to comment
Share on other sites

That's weird because it works fine. so that's how you're meant to write it? I'll change it now

 

No, its not how your meant to write it. The code you had was fine.

 

As to your question, once your finished with your result in the first area you can use [mysql_data_seek[/m] to rewind the pointer back to the start and use the resource again in a second location.

Link to comment
Share on other sites

Ok, well the thing is, I'm using loads of includes. So that:

 

<?php if ($advanced=='on')
      include "advanced.html";
      else
      include "vpt.html";?>

if advanced is on goes to:

 

<?php if ($vpt =='village')
echo $vpt_sub;
elseif ($vpt =='player')
include "player_28_advanced.html";
else
include "tribe_28_advanced.html";?>

 

and then say it's tribe it goes to:

<?php if ($x=='on' AND $y=='')
include "tribe_28_advanced_x.html";
else if ($y=='on' AND $x=='')
include "tribe_28_advanced_y.html";
else if ($y=='on' AND $x=='on')
include "tribe_28_advanced_xy.html";
else if ($k=='on')
include "tribe_28_advanced_k.html";
?>

and then say only x is on it goes to this:

 

<?php
$con = mysql_connect("localhost","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("wwwspark_tribalwars", $con);

$result = mysql_query("SELECT * FROM ally_en28 WHERE tag = '$vpt_sub'");

while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$result2 = mysql_query("SELECT * FROM players_en28 WHERE ally = '".$id."'");
while($row2 = mysql_fetch_array($result2))
{
$id2 = $row2['id'];
$result3 = mysql_query("SELECT * FROM villages_en28 WHERE player = '".$id2."' AND x BETWEEN $x1 AND $x2");

while($row3 = mysql_fetch_array($result3))
{
echo $row3['x']."|".$row3['y']." ";
}

}
}
mysql_close($con);
?>

Link to comment
Share on other sites

Yes. You don't need curly braces {} around simply if statements. Also, include is not a function and therefore doesn't require its arguments be enclosed within () braces.

 

It just looks better. Having

 

if($_COOKIE['snap']== "on" )

$snap = "on";

 

looks wrong. But having { is like having a border around the code to easily see the blocks

 

if($_COOKIE['snap']== "on" ){

$snap = "on";

}

Link to comment
Share on other sites

Yes. You don't need curly braces {} around simply if statements. Also, include is not a function and therefore doesn't require its arguments be enclosed within () braces.

 

It just looks better. Having

 

if($_COOKIE['snap']== "on" )

$snap = "on";

 

looks wrong. But having { is like having a border around the code to easily see the blocks

 

if($_COOKIE['snap']== "on" ){

$snap = "on";

}

 

it might look wrong in your opinion, but its not in php's.

Link to comment
Share on other sites

No, ok. So what I have is 2 places that need to show the exact same thing. In the first place it will actually find the result. In the second it just needs to as such copy and paste the result that was echoed in the first place

 

EDIT: Look here: http://sparky13.co.cc/test/page_28.php?spear=0&sword=0&axe=0&archer=0&scout=0&lc=0&ma=0&hc=0&ram=0&cat=0&paladin=0&noble=0&village%2Fplayer%2Ftribe=tribe&village%2Fplayer%2Ftribe_submit=farm-u&x1=0&x=on&x2=0&y1=0&y=on&y2=0&k1=0&Submit=Make+Fake+Script

 

See how in 2 places it has those strings of numbers. They are the same string of numbers. That is where my include thing is but I want to copy across the string of numbers from when i get the result the first time to save on load time

Link to comment
Share on other sites

Look here: http://sparky13.co.cc/test/page_28.php?spear=0&sword=0&axe=0&archer=0&scout=0&lc=0&ma=0&hc=0&ram=0&cat=0&paladin=0&noble=0&village%2Fplayer%2Ftribe=tribe&village%2Fplayer%2Ftribe_submit=farm-u&x1=0&x=on&x2=0&y1=0&y=on&y2=0&k1=0&Submit=Make+Fake+Script

 

See how in 2 places it has those strings of numbers. They are the same string of numbers. That is where my include thing is but I want to copy across the string of numbers from when i get the result the first time to save on load time

 

I don't get what you mean. Sorry

Link to comment
Share on other sites

A simple example.

 

// get the result once
$sql = "SELECT a, b FROM foo";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    // use the result the first time
    while ($row = mysql_fetch_assoc($result)) {
      echo $row['a'] . $row['b'];
    }
  }
}

// now, somewhere else on the page we need to use the result again.
if (is_resource($result)) {
  // rewind the resources internal pointer.
  $result = mysql_data_seek($result);
  while ($row = mysql_fetch_assoc($result)) {
    echo $row['a'] . $row['b'];
  }
}

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.