Page 1 of 1

coding error php - Can't retrieve info from mysql db

Posted: Sun Aug 14, 2011 3:52 pm
by simon_porter00
Hello All,
There's something I'm missing (coding wise, I know I'm missing plenty in the head) in the php coding for retrieving info from the db.
Some information on the webpage can be seen, namely the $gdzie command, but the rest $row['Name'] etc isn't working.
I've tried re-writing the code in full php to no avail.
I should let you know that I play with php with trial and error and this was a project done for me, which I'm just trying to make some amendments to by myself.

Anyway, here's the code:

Code: Select all

<?php  
if(count($_POST)==0)header("location: index.html");
list($kto,$gdzie)=explode('_',key($_POST));
//echo $kto;
//echo $gdzie;
require_once("Dbconn");
//print_r($countys);die;
if($kto=='schools'){
$query = "select sc.ID,sc.Name,sl.district,sl.town
from schools sc 
left join schools_location sl on sc.ID=sl.school_id 
where sl.county='$gdzie' group by sc.ID";
}

$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))

//print_r($data);//die;
?>
<?php if($kto=='schools'):?>
<h2>Schools - <?php echo $gdzie;?></h2>
<table class="sortable" id="anyid" width="100%">
<tr><th align="left">Name:</th><th align="left">Town:</th><th align="left" class="unsortable">District:</th><th align="left" class="unsortable">Details:</th></tr>

<tr><td><?php echo $row['Name']?></td>
<td><?php echo $row['town']?></td>
<td><?php echo $row['district']?></td>
<td><a href="schoolsprofile.php?ID=<?php echo $row['ID']?>">School's profile</a></td></tr>

</table>
<?php endif;?>
Many TIA

Re: coding error php - Can't retrieve info from mysql db

Posted: Sun Aug 14, 2011 4:05 pm
by oscardog
So when you var_dump $result you receive an empty array? (If you've not tried it, I suggest you do!). Another thing to try is to output the SQL statement and paste it into the SQL command-line in phpMyAdmin and see if you get a result set.

As a note, it would be best to wrap your while loop in an if, to check if a result set has been returned. For example...

Code: Select all

$result = mysql_query($query) or die(mysql_error());

if(mysql_num_rows($result) >= 1) {
while($row = mysql_fetch_assoc($result))

?>
<?php if($kto=='schools'):?>
<h2>Schools - <?php echo $gdzie;?></h2>
<table class="sortable" id="anyid" width="100%">
<tr><th align="left">Name:</th><th align="left">Town:</th><th align="left" class="unsortable">District:</th><th align="left" class="unsortable">Details:</th></tr>

<tr><td><?php echo $row['Name']?></td>
<td><?php echo $row['town']?></td>
<td><?php echo $row['district']?></td>
<td><a href="schoolsprofile.php?ID=<?php echo $row['ID']?>">School's profile</a></td></tr>

</table>
<?php endif;
} ?>
And, after writing that, I just noticed your while loop has no curly braces. I've never tried without curly braces, but I'm pretty sure it needs curly braces as it would have no idea where to finish looping!