Variable not being passed?

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 receives pk. pk is then passed to the axjax part like this:

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

      

And finally it is 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 used by itself and passes parameters. Likewise, there are no errors. The problem is in passing or generating $ pk, as references in the output file cause $ pk to be incremented by 2, like 4, 6, 8, etc.

I don't understand why this is happening.

0


source to share


4 answers


mysql_fetch_row the link has no subarrays. It will return the first field as 0, the next as 1, etc.

Try

$pk = $row[0];

      

This can be easily used with your foreach

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

      

or it gives you both an associative and a 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 line you want to get the value from.;)

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

      

BTW: I'm pretty sure this nested loop won't do what you want. You will get 3 links for each article_no. The first one with seller_id as text, the second text is sendstarts, 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>';
}

      

+5


source


As troelskn says, it looks like you are a little confused about what mysql_fetch_row returns.

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 reorganize a bit, in light of this ....

+1


source


I don't think this does what you expect:

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

      

Try:

$pk = $row['ARTICLE_NO'];

      

0


source


The other answers here should answer your problem enough, but I just wanted to add an additional debugger to help you if you run into a similar problem in the future.

You can do very simple debugging by simply printing the information on the screen about the variables you are working with. (PHP IDE will often have more powerful debugging features, but for something like this problem, this method will be fine)

Source:

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 have noticed that the GetAuctionData function is not getting the correct variable. Add this line:

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

      

If you look at the output when you run the file, you will probably see that $ pk is NULL. Hmm, it didn't work. Let's change a little:

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

      

Now you will see something like this:

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

      

Aha! Now that you can see what you are working with, you will see that the code you wanted to use in the first place:

$pk = $row[0];

      

var_dump()

is your friend. If you are working with arrays, print_r()

gives you similar information, but perfectly formatted.

Good luck.

-1


source







All Articles