Problem with sitemap.php

Hi everyone,

I am trying to create a sitemap.php so that I can control the URLs to index, but I am having a problem that I cannot understand. Originally I had:

$statement = $conexion->prepare("SELECT * FROM listado_precios ORDER BY Agregado DESC");
    $statement->execute();
    $todos_juegos_xbox_one = $statement->fetchAll();

    foreach ($todos_juegos_xbox_one as $Xbox_one) {
        echo "<url>";
          echo "<loc>http://www.websitename.com/single.php?ID=" . $Xbox_one['ID'] . "</loc>";
          echo "<lastmod>" . substr($Xbox_one['Agregado'],0,10) . "</lastmod>";
          echo "<priority>0.8</priority>";
        echo "</url>";
    }

But as I have changed to another table, I wanted to use the new data just changing to:

$statement = $conexion->prepare("SELECT * FROM info_XboxOne ORDER BY agregado DESC");
    $statement->execute();
    $todos_juegos_xbox_one = $statement->fetchAll();

    foreach ($todos_juegos_xbox_one as $Xbox_one) {
        echo "<url>";
          echo "<loc>http://www.laxtore.com/single.php?ID=" . $Xbox_one['ID'] . "</loc>";
          echo "<lastmod>" . substr($Xbox_one['agregado'],0,10) . "</lastmod>";
          echo "<priority>0.8</priority>";
        echo "</url>";
    }

But it throws me the following error:

Error de lectura XML: no se encuentra el elemento raíz
Ubicación: http://localhost/xboxone/sitemap.php
Número de línea 4, columna 1:
^

Translates to:-

XML read error: root element not found
Location: http: //localhost/xboxone/sitemap.php1
Line number 4, column 1:

The problem is the table name! I have tried with other tables I have in the same database, even with random names and it is working, but not with info_XboxOne table name. What is happening?

look at ur info_XboxOne, table is an aggregate column there?

If you put quotes around the table name in the query, does it make any difference? If you use that table name in any other query, does it work there? For example, the code you use to add the entries to the table before you extract them here - is that working?

I have tried using the table name in another statement and it works fine. With the comas is not giving me errors but is not taking any data from the database.

As it’s a different table, is the column name for your ORDER BY clause still correct? What happens if you type that query in to phpmyadmin (or whatever you use) and execute it from there?

I think the problem is the amount of data…because I am given this error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in /Applications/XAMPP/xamppfiles/htdocs/xboxone/index.php on line 15

OK, hard to tell then as I can’t tell which is line 15. If it’s the fetchAll() line, you could iterate through the results individually with fetch() instead.

I was trying with this code but i keep having the problem in the fetchAll():

$statement = $conexion->prepare("SELECT * FROM info_XboxOne ORDER BY agregado DESC");
    $statement->execute();
    $todos_juegos_xbox_one = $statement->fetchAll();

    $total = count($todos_juegos_xbox_one);

    for ($i=0; $i <$total ; $i++) {
        $statement = $conexion->prepare("SELECT * FROM info_XboxOne ORDER BY agregado DESC LIMIT $i,$i+1");
        $statement->execute();
        $xbox_individual = $statement->fetch();

        echo "<url>";
          echo "<loc>http://www.laxtore.com/Juegos/Xbox-One/" . $xbox_individual['ID'] . "/" . limpia_url($xbox_individual['Juego']) . "/</loc>";
          echo "<lastmod>" . substr($xbox_individual['agregado'],0,10) . "</lastmod>";
          echo "<priority>0.8</priority>";
        echo "</url>";
    }

Is there any other way to know the amount of lines I have in the table? Just to iterate.

You don’t need to know how many lines there are just to retrieve them all. Just do something like:

$statement->execute();
while ($xbox_individual = $statement->fetch()) { 
  // display the values from $xbox_individual
  }

You can find out the number of rows returned using rowCount(), but if your only use is for the loop, it’s not needed.

$statement->execute();
$count = $connexion->rowCount();

In any case, in your code above, you run the query every time you go through the loop, so you’ll just display the same (first) result over and over.

1 Like

Thanks!! I have some errors but its for other reasons

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.