Forum Moderators: coopster
I have a tariff which is atm 44 rows with 15 columns of numbers. Now i am trying to pull as individual price from the tariff based on user input:
my code
<?php
if (isset($ID)){
$ID = $HTTP_GET_VARS['ID'];
$band = $_SESSION['band'];
$conn = db_connect();
$query = mysql_query("SELECT * FROM tarriff");
$cost = mysql_result($query,$ID,$band);
?>
<td colspan="2"><h4>The cost for this period is £ <? print $cost;?></h4></td>
<? }?>
</tr>
BUT, they all work but the last one in the database, i added a new row to test it and its always the last row, now i am not very good at php and gather somehow my result is returning the wrong row somehow.
Please help
regards Smad
string mysql_result [php.net] ( resource result, int row [, mixed field] )
rowThe row number from the result that's being retrieved. Row numbers start at 0.
Are you starting at 0, or 1 with your ID?
You should build that into your original query, as coopster showed.
if you have the ID then you can do as in msg6, but I think you need to select *
$query = mysql_query("SELECT * FROM tarriff WHERE ID='$ID'");
this should only bring up a single row as I am hoping your ID's are unique. Then you can extract the row. I will show the value with a print_r so you can try it and see the result
if ($row = mysql_fetch_array($query)) {
echo '<pre>';
print_r()$row;
echo '</pre>';
}
this gives you all values from the row wth the ID you specified in your query. Now you can grab whatever value you need by specifying the offset from the array.
echo $row['a'];
or whichever you need