0

Actually I want a output of more than one row and its giving me only one. This is my code and it returns only one row. But when I run the same query SELECT DISTINCTROW metal, $fsym from metals ORDER BY id DESC LIMIT 4 in phpmyadmin it gives me my desired output of 4 rows.

function readAnother()
{
    $fsym =    $_GET['fsym'];
    echo $fsym . '<br>';
    $query = "SELECT DISTINCTROW metal, $fsym from metals ORDER BY id DESC LIMIT 4";
    $stmt = $this->conn->prepare($query);

    $stmt->bindParam(1,$this->fsym);
    $stmt->execute();

    $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $this->metal = $row['metal'];
    $this->price_eur = $row[$fsym];

}
10
  • have you inspected the output - such as var_dump( $row ); within the function? Commented Jul 17, 2018 at 6:27
  • In var_dump its returning only one row. Commented Jul 17, 2018 at 6:29
  • When i did $row = $stmt->fetchAll(PDO::FETCH_ASSOC); i am getting all the four rows as my desired output but getting another error now Commented Jul 17, 2018 at 6:33
  • Array ( [0] => Array ( [metal] => palladium [price_eur] => 756.21 ) [1] => Array ( [metal] => platinum [price_eur] => 675.59 ) [2] => Array ( [metal] => silver [price_eur] => 13.6 ) [3] => Array ( [metal] => gold [price_eur] => 100.38 ) ) Notice: Undefined index: metal in C:\xampp\htdocs\metalsapi\object.php on line 53 Notice: Undefined index: price_eur in C:\xampp\htdocs\metalsapi\object.php on line 54 Commented Jul 17, 2018 at 6:34
  • Add the Updated code on question Commented Jul 17, 2018 at 6:34

2 Answers 2

0

As per discussion on comment: Use fetchall

You can try below code

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $this->metal = $row['metal']; // by this it will add the last 4th record metal value in the variable 
    $this->price_eur = $row[$fsym];
}

I will recommend that if you want to use all rows 'metal' then use the proper array

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$finalArray = [];
foreach ($rows as $row) {
    $finalArray[] = ['metal' => $row['metal'], 'price_eur'=> $row[$fsym]]

}
print_r($finalArray);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you soooooo muchh this second one worked.. Thank you i learnt a new thing today. @DsRaj
Reload the page and check again because it didn't show here: How to mark as answer
I don't have a 15 reputation, may be that's y its not taking
I did it... Thank you again.
0

It's better to use following function: http://php.net/manual/ru/pdostatement.fetch.php Like that:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $this->metal = $row['metal'];
    $this->price_eur = $row[$fsym];
}

If you want to use fetchAll then use it like that:

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $this->metal = $row['metal'];
    $this->price_eur = $row[$fsym];
}

2 Comments

yea Its done thank you for this valueable help. @Nikita Leshchev
@PayalTripathy then mark my answer as "answer" pls :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.