Click to See Complete Forum and Search --> : More effecient coding...


novemberGrey
08-01-2007, 11:14 AM
Is there a more effecient way to do this??

// Retrieve all the data from the "example" table
$result1 = mysql_query("SELECT * FROM home WHERE id='1'");
$result2 = mysql_query("SELECT * FROM home WHERE id='2'");

// store the record of the "example" table into $row
$row1 = mysql_fetch_array( $result1);
$row2 = mysql_fetch_array( $result2);
// Print out the contents of the entry

$id1 = $row1['id'];
$title1 = $row1['title'];
$content1 = $row1['content'];
$image1 = $row1['image'];

$id2 = $row2['id'];
$title2 = $row2['title'];
$content2 = $row2['content'];
$image2 = $row2['image'];
?>

gregorious
08-01-2007, 11:24 AM
I am not sure if this helps; but it would seem to make debug easier.



// Retrieve all the data from table 1
$result1 = mysql_query("SELECT * FROM home WHERE id='1'");

$row1 = mysql_fetch_array( $result1);
$id1 = $row1['id'];
$title1 = $row1['title'];
$content1 = $row1['content'];
$image1 = $row1['image'];



// Retrieve all the data from table 2
$result2 = mysql_query("SELECT * FROM home WHERE id='2'");

$row2 = mysql_fetch_array( $result2);
$id2 = $row2['id'];
$title2 = $row2['title'];
$content2 = $row2['content'];
$image2 = $row2['image'];

ellisgl
08-01-2007, 11:26 AM
extract()

$result1 = mysql_query("SELECT * FROM `home` WHERE id='1'");
$row1 = mysql_fetch_assoc($result1);
extract($row1);
// do what you want with row 1

$result2 = mysql_query("SELECT * FROM `home `WHERE id='2'");
$row2 = mysql_fetch_assoc($result2);
extract($row2);
// do what you want with row 2

Unless you are trying to compare stuff from the first and second... Then you can change the second extract to be extract($row2, EXTR_PREFIX_SAME, 'b')
so you vars for the second row would be $b_(column name)

novemberGrey
08-01-2007, 11:40 AM
thankyou!!!

ellisgl
08-01-2007, 11:41 AM
No problem!

bokeh
08-01-2007, 11:46 AM
Is there a more effecient way to do this??The real trouble is your code is static. You could do this with one query and a loop.SELECT * FROM `home` WHERE `id`<0 ORDER BY `id` ASC LIMIT 2

ellisgl
08-01-2007, 10:08 PM
Of course - the limit 2 may not be what he is after...

bokeh
08-02-2007, 01:47 AM
Of course - the limit 2 may not be what he is after...And then again it may be!