views:

180

answers:

4

Hello,

I have this code

while($row = mysql_fetch_row($result))
{
echo '<tr>';
$pk = $row[0]['ARTICLE_NO'];

foreach($row as $key => $value)
{
echo '<td><a href="#" onclick="GetAuctionData(\''.$pk.'\')">' . $value . '</a></td>';
}

which gets pk. pk is then passed on to the axjax part with this:

function GetAuctionData(pk)
{
.....
var url="get_auction.php?"
url=url+"cmd=GetAuctionData&pk="+pk;

And finally used in a separate php file with:

$pk = $_GET["pk"];
$sql="SELECT * FROM Auctions WHERE ARTICLE_NO ='$pk'";

the second php file works fine when using it by itself and passing parameters. Likewise there are no errors anywhere. The problem seems to be with passing or generating $pk, as the links in the output file result in $pk being incremednted by 2, eg 4, 6, 8 etc

I can not understand why this is happening.

+1  A: 

I don't think this does what you expect:

$pk = $row[0]['ARTICLE_NO'];

Try with:

$pk = $row['ARTICLE_NO'];
troelskn
Then I get an undefined index article_no error
Joshxtothe4
+2  A: 

As troelskn says, it looks like you are a bit confused as to what mysql_fetch_row is returning.

mysql_fetch_row will return $article[0], $article[1] etc

there are also:

mysql_fetch_assoc // return $article['ARTICLE_NO'], $article['otherfield'] etc

mysql_fetch_array // returns an array that is effectively the above two array_merge'd
mysql_fetch_object // returns a stdclass object, as if mysql_fetch_assoc had been passed to get_object_vars()

you need to refactor a bit, in light of this ....

benlumley
+6  A: 

mysql_fetch_row link does not have subarrays. It will return the first field as 0, next as 1, etc.

Try with

$pk = $row[0];

This can easily be used with your foreach

while($row = mysql_fetch_assoc($result))
$pk = $row['ARTICLE_NO'];

or this gives you both associative and numbered array.

while($row = mysql_fetch_array($result, MYSQL_BOTH))
$pk = $row['ARTICLE_NO'];

EDIT: Based on

$result = mysql_query("SELECT SELLER_ID, ACCESSSTARTS, ARTICLE_NAME FROM {$table}");

You have to include the row you want a value from. ;)

$result = mysql_query("ARTICLE_NO, SELECT SELLER_ID, ACCESSSTARTS, ARTICLE_NAME FROM {$table}");

BTW: Im pretty sure this nested loop will not produce what you want. You'll get 3 links to each article_no. The first with seller_id as text, the second text is accessstarts, and the last link with the same href will have the text article_name.

Maybe something like this?

while($row = mysql_fetch_assoc($result))
{
    $pk = $row['ARTICLE_NO'];
    echo '<td><a href="#" onclick="GetAuctionData(\''.$pk.'\')">' . $row['ARTICLE_NAME'] . '</a></td>';
}
OIS
From this I get an undefined index article_no error
Joshxtothe4
I got ARTICLE_NO from $pk = $row[0]['ARTICLE_NO'];'article_no' should be the name of the field/attribute in yer table.And remember mysql_fetch_array will return every field and value twice, for both numerical and field/attribute key.
OIS
ARTICLE_NO is the first column in my table, but changing it to $pk = $row['ARTICLE_NO']; gives the index error. Am I using the wrong quotes or something?
Joshxtothe4
If you want to use mysql\_fetch\_row you have to use the field/attribute index in the sql result, ie 0. Look at my first code example.You have to use mysql\_fetch\_assoc if you want to retrieve the value by the field/attribute name. Look at my second example.
OIS
A: 

The other responses here should answer your problem sufficiently, but I just wanted to add an extra debugging tip to could help you should you run into a similar problem in the future.

You can do some very basic debugging by merely printing to the screen information about the variables you're working with. (A PHP IDE will often have more powerful debugging features, but for something like this problem, this method will be fine)

Your original code:

while($row = mysql_fetch_row($result))
{
    echo '<tr>';
    $pk = $row[0]['ARTICLE_NO'];

    foreach($row as $key => $value)
    {
        echo '<td><a href="#" onclick="GetAuctionData(\''.$pk.'\')">' . $value . '</a></td>';
    }
}

You notice that the GetAuctionData function isn't getting its variable properly. Add this line:

$pk = $row[0]['ARTICLE_NO'];
var_dump($pk);

If you look at the output when you run the file, you'd probably see it saying that $pk is null. Hmmm, that didn't work. Let's change it a bit:

$pk = $row[0]['ARTICLE_NO'];
var_dump($row);

You'd now see something like this:

array(2) {
  [0]=> string(2) "12"
  [1]=> string(7) "myValue"
}

Aha! Now that you can see exactly what you're working with, you'd see that the code you wanted to use in the first place was:

$pk = $row[0];

var_dump() is your friend. If you're working with arrays, print_r() gives you similar information, but formatted nicely.

Good luck.

nickf
Thanks for your well written response. My problem is that I cannot use row[o] as I am not including ARTICLE_NO in the result set that I want to display. Must I include it from the original query, which is :$result = mysql_query("SELECT SELLER_ID, ACCESSSTARTS, ARTICLE_NAME FROM {$table}");
Joshxtothe4
however I think that would mean I could not iterate through rows to show the data, as I don't want article_no being displayed
Joshxtothe4
this would be just for testing and debugging, not for when the site goes live. you'd remove the var_dump() lines once you figured out the problem.
nickf