Jump to content

[SOLVED] Looping through a set of database results


abch624

Recommended Posts

Hi guys I have the code bellow which works just fine

<?php $tbl_personal="personaldetails"; // Table name for personal details
$tbl_profile="profiledetails"; // Table name for profile details
$tbl_cities="tblcities"; // Table name for city name
$tbl_countries="tblcountries"; // Table name for country name
$tbl_branches="tblbranches"; // Table name for branch name
$tbl_hidden="hidden"; // Table name for hidden values

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

//Variable from url
$query = $_GET['query'];

// Variables sent from form
$searchfield=$_POST['searchField'];

echo $sql = "SELECT P.firstName, P.lastName, P.bid, P.lid, D.profilePicture FROM $tbl_personal P JOIN $tbl_profile D ON (P.profileDetailsID = D.profileDetailsID) JOIN $tbl_cities C ON (P.lid = C.lid) JOIN $tbl_branches B ON (P.bid = B.bid) WHERE P.firstName LIKE '%$query%' OR P.lastName LIKE '%$query%' OR D.activity LIKE '%$query%' OR D.interests LIKE '%$query%' OR D.aboutMe LIKE '%$query%' OR D.languagesSpoken LIKE '%$query%' OR D.countries LIKE '%$query%' OR B.name LIKE '%$query%' OR C.name LIKE '%$query%'";
echo "<hr>";
if ($result = mysql_query($sql)) {
$row=mysql_fetch_array($result);
foreach ($row['firstName'] as $t) {echo $t; echo "<hr>";}
$profilepicture = $row['profilePicture'];
}
else {
echo mysql_error();
}

?>

 

My problem is the foreach loop. If you have a look at the attached picture of PHPmyAdmin I have attached it shows there are 9 coloums resulted with this. I want to get and display all these fields I get into a table.

 

Please help how can I loop through this so to get all results not just the first one.

 

Please help

 

Cheers - Zahid

 

[attachment deleted by admin]

Have you considered using a while loop?

 

if (mysql_num_rows($sql) > 0) {
    while ($row=mysql_fetch_array($sql)) {
        echo $row[firstName]; echo "<hr>";
        $profilepicture = $row['profilePicture'];
    } 
}
else {
echo mysql_error();
}

Archived

This topic is now archived and is closed to further replies.

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