views:

131

answers:

2
$query = "select * from a ...";
if($result = mysql_query($query,$con))
{while($row = mysql_fetch_array($result);)
  $arr[0][] = $row;
}
$query = "select * from b ...";
if($result = mysql_query($query,$con))
{
 while($row = mysql_fetch_array($result);)
  $arr[1][] = $row;
}
$query = "select * from c ...";
if($result = mysql_query($query,$con))
{
 while($row = mysql_fetch_array($result);)
  $arr[2][] = $row;
}

The 3 queries retrieves multiple rows with different structure, 3 queries need 3 round-trip, 10 queries will need 10 round-trip.

How will you optimize this case?

+1  A: 

Select the columns you actually need instead of select *

Other than that, not much to say without knowing what data you need and how they relate.

nos
A: 

You could try a UNION:

$query = "SELECT 'a' AS tablename, a.* FROM a
UNION
SELECT 'b' AS tablename, b.* FROM b
UNION
SELECT 'c' AS tablename, c.* FROM c";
if($result = mysql_query($query,$con))
{while($row = mysql_fetch_array($result);)

switch ($row['tablename']) {
    case "a":
        $arr[0][] = $row;
        break;
    case "b":
$arr[1][] = $row;
        break;
    case "c":
        $arr[2][] = $row;
        break;
}
}

pardon my php, it's not my area of expertise :)

Chris McCall
Seems it will work only when column numbers are the same.What if not?
Shore
Then you're screwed.
Chris McCall
You mean no solution?
Shore
You have to do n things, which are all different. That means you need n instructions. Why not get your accept rate above 35% instead of peppering answers with comments?
Chris McCall
I don't think there is a good answer for this question yet.
Shore