0
votes

I'm getting an error:

Warning: Invalid argument supplied for foreach() in /home/pawno/domains/rgl.lt/public_html/vvp/index.php on line 96

Line 96 == 20 in this excerpt.

CONFIG PAGE:

<?php
$databasehost = "localhost";
$databasename = "db";
$databaseuser = "user";
$databasepass = "pass";

try {
    $db = new PDO("mysql:host=$databasehost;dbname=$databasename",
      $databaseuser, $databasepass);
}
catch (PDOException $e) {
    echo 'Nepavyko prisijungti prie duomenų bazės. 
      Praneškite apie šią klaidą administracijai. 
      Klaidos kodas: ' . $e->getMessage();
}
?>

INDEX PAGE:

<div class = "vvpbutton">
    <?php
    $sql = "SELECT * FROM players 
      WHERE Vardas = ".$_SESSION[ 'Vardas' ]."";
    foreach ($db->query($sql) as $row)
    {?>
    <button type="button" class="vvpbuttonss btn btn-danger">» PATIRTIES TAŠKAI: <?=$row['Xp']?> XP</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» IEŠKOMUMAS: 0 žvaigžd.</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» PASLAUGŲ VALIUTA: 0 kred.</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» ADMIN: Yra ( 2015.02.25 )</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» VIP: Nėra</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» PINIGAI RANKOSE: 0 €</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» PINIGAI BANKE: 0 €</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» DARBAS: Policininkas</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» UŽBLOKUOTAS: Ne</button>
    <?php
    }
    ?>
</div>
1
You should include your code in your question. - jeroen
there is a pastebin, i don't get this code system :D - edgarasb
You should setup PDO to throw exceptions so that it will tell you what is wrong with your (failing...) query. Is the Vardas column an integer? If not you need to quote it or - even better- use a prepared statement. - jeroen
foreach() expects an array, its more like you are trying to use a string or something else. Make sure that $db->query($sql) is an array before trying to use it with the foreach() function - Nelson Owalo
@jeroen: the column name literally translates to English as "first name", so there's a pretty good guess it's not an integer. Can you expand your comment to an answer, or provide a link to an existing one? - DCoder

1 Answers

2
votes

That mark-up is just bad bro, You don't know the return value of $db->query($sql), so why are you assuming its an array?

First store the return data of the query in a variable;

$rows = $db->query($sql);

Then check if its an array before trying to use it as one;

if(is_array($rows)){
  //do something if the return data is an array
  foreach ($rows as $row){?>
    <button type="button" class="vvpbuttonss btn btn-danger">» PATIRTIES TAŠKAI: <?=$row['Xp']?> XP</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» IEŠKOMUMAS: 0 žvaigžd.</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» PASLAUGŲ VALIUTA: 0 kred.</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» ADMIN: Yra ( 2015.02.25 )</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» VIP: Nėra</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» PINIGAI RANKOSE: 0 €</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» PINIGAI BANKE: 0 €</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» DARBAS: Policininkas</button><br />
    <button type="button" class="vvpbuttonss btn btn-danger">» UŽBLOKUOTAS: Ne</button>
  <?php }
} else {?> <p>The provided data isn't an array</p> <?php }

Though I have a nagging feeling that your return data isn't an array.