Forum Moderators: coopster

Message Too Old, No Replies

Unable to Jump to row

         

Smad

12:18 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



Again php is driving me mad..

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

coopster

12:23 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



string mysql_result [php.net] ( resource result, int row [, mixed field] )


row

The row number from the result that's being retrieved. Row numbers start at 0.

Are you starting at 0, or 1 with your ID?

Smad

12:37 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



my ID starts at 1 but if i replace

$ID in $cost = mysql_result($query,$ID,$band);

with $row from mysql_numrows($query) (i think thats written right) it all goes wrong

Habtom

12:51 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Trying echoing

$ID = $HTTP_GET_VARS['ID'];
$band = $_SESSION['band'];

the values in $ID and $band, and check what values you are holding.

echo $ID;
echo $band;

Habtom

Smad

1:00 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



Yes they give me the right results, the row ID and the column (band)

i think where i am going wrong is the row which is using the $ID which is not the row number really.?

coopster

1:18 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I guess that depends on how you have your table defined and populated. You could always change your query to select only that row.
$query = mysql_query("SELECT band FROM tarriff WHERE ID='$ID'");

Smad

2:44 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



hmm maybe my table is set wrong?

nothing is working atm..

heres an example of my table.
these are the fields:

date,a,b,c,d,e,f,g,h,i,j,k,ID

and in each alphabet number is a cost ie: 345

i cant seem to print a single cost from my table with my code.

jatar_k

6:16 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



there seems to be some confusion here, the row# and the ID are not necessarily the same which is probably why you aren't getting what you want.

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

coopster

6:28 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Thanks for clarifying jatar_k. Smad, are you familiar with the Basics of extracting data from MySQL [webmasterworld.com]?