0

I use an XML request to get all the news from the database. So far good, but I always get back only the last or first value in my loop.

function SendData($pass) :void {

    if($pass) {

        $host = "Database_:3360";
        $username = "root";
        $password = "root";
        $dbname = "db";
        $dbquery = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
        $stmt = $dbquery->prepare("SELECT * FROM tab_news ORDER BY `date` DESC");
        $stmt->execute();
    
        $count = $stmt->rowCount();
        
        if($count === 0){
            $news[] = 'Es gibt noch keine News...';
        } else {
            $news = $stmt->fetchAll(PDO::FETCH_ASSOC)
        }
    } 

    echo json_encode(
        array(
            'allowed' => $pass,
            'count' => $count,
            'news' => $news
        )
    );
}

I try it now for 1h but do not get the desired result. What is wrong ?

  • What type is your var $stmt ? – Aleksey Vaganov Jan 23 '22 at 16:34
  • @AlekseyVaganov It is created at the fetch part – TheCallofDuty Jan 23 '22 at 16:37
  • When you say _"I use an XML request"_, do you mean a `xmlhttprequest`, a.k.a. Ajax? – M. Eriksson Jan 23 '22 at 16:48
  • 1
    I'm assuming that `$stmt` is a PDO statement? In that case, I don't see how the second code block could only return the first if you get multiple records back. Please show some more context (including the query and example data). The first code block doesn't make much sense tbh. It just fetches one record and keeps adding the same record to an array x amount of times. However, there is an easier way. Just do `$news = $stmt->fetchAll(PDO::FETCH_ASSOC)`. No need to loop through the data. – M. Eriksson Jan 23 '22 at 16:50
  • @M.Eriksson yes i mean xmlhttprequest. `$stmt = $dbquery->prepare("SELECT * FROM tab_news ORDER BY `date` DESC"); $stmt->execute();` Your line `$news = $stmt->fetchAll(PDO::FETCH_ASSOC)` brings an endless loop – TheCallofDuty Jan 23 '22 at 16:59
  • 1
    Please [edit](https://stackoverflow.com/posts/70824185/edit) your question to include all relevant code instead of posting snippets of it in comments. We need to get a fill overview, not just a line here and there. Regarding `fetchAll()`, you should not put it in a while loop. It's _instead_ of your loop. See [the manual](https://www.php.net/manual/en/pdostatement.fetchall) for examples. – M. Eriksson Jan 23 '22 at 17:01
  • @M.Eriksson the full code is in. Thank you (Y) – TheCallofDuty Jan 23 '22 at 17:14
  • There's no way that code would cause an infinite loop. If that happens, it happens somewhere else. A heads up regarding [rowCount()](https://www.php.net/manual/en/pdostatement.rowcount.php). From the manual: _"For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement"_. Also, if `$pass` is false, neiter `$count` or `$news` will be defined in your `echo`-statement. – M. Eriksson Jan 23 '22 at 17:30
  • @M.Eriksson okay its no endless loop, but i got no response. Hm, even if I remove the row count and just execute the query and give me $news, I get no response. – TheCallofDuty Jan 23 '22 at 17:53
  • While developing locally, make sure that you [display all errors](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?rq=1) or check the error log when you only get a white page. You're missing the `;` after `fetchAll()` which will cause a syntax error. – M. Eriksson Jan 23 '22 at 17:57
  • @M.Eriksson I use a CMS and get no errors or similar output. Logging is active & also the SQL server shows no errors in the logs. – TheCallofDuty Jan 23 '22 at 18:10
  • The SQL server won't show you PHP errors. You need to either check the web servers error log or if the CMS has some logging. Without error logging, debugging will be a pain. Regarding the current error, any decent IDE would have showed it as well. Either way, did you add the `;`? – M. Eriksson Jan 23 '22 at 18:13
  • Yes the `;` I have had inside from the beginning. I use VSCode and have no errors. I will look at the whole thing again tomorrow. Thanks anyway for your help – TheCallofDuty Jan 23 '22 at 18:16
  • _"Yes the ; I have had inside from the beginning"_ - So the posted code isn't the code you actually have? Well, then it's literally impossible for us to know eventual typos/misses you have in the real code. – M. Eriksson Jan 23 '22 at 18:17
  • @M.Eriksson For sure its the right code. Only the ; is missing. – TheCallofDuty Jan 23 '22 at 19:00

0 Answers0