I get an error Call to undefined method PDOStatement :: store_result ()

1

This is the error

  

Fatal error: Uncaught Error: Call to undefined method   PDOStatement :: store_result () in   C: \ xampp \ htdocs \ helpProgramming \ core \ controllers \ searchWController.php: 44   Stack trace: # 0 C: \ xampp \ htdocs \ helpProgramming \ index.php (6):   include () # 1 {main} thrown in   C: \ xampp \ htdocs \ helpProgramming \ core \ controllers \ searchWController.php   on line 44

Here is my code;

 <?php
                   // Instancia de la conexión con los datos requeridos
                $pdo = new PDO('mysql:host=localhost;dbname=help_programing', 'root', '');
                $likeparametro= "%$BusquedaGet%";
                $stmt  = $pdo->prepare("SELECT *FROM pageswebsites 
                                      WHERE titulo LIKE ? ORDER BY ID DESC");
                //Ejecutamos la consulta , pasamos el parámetro para el like, 
                //que será una variable que ya se construyó anteriormente
                $stmt->execute([$likeparametro]);
                $stmt->store_result();
                $data = $stmt->fetchAll();//Obtenemos los resultados
                //Iteramos sobre estos resultados.

                if($data->num_rows <= 1){
                    echo 'no hay resultados';
                } else {
                    foreach($data as $row) {
                    ?>
                    <a class="contresult" href="?view=pagew&id=<?php echo $row['viewPage']; ?>&cateinfo=<?php echo $row['categoria']; ?>">
                    <div class="conResult">
                        <div class="titulocon">
                            <h2><?php echo $row['titulo']; ?></h2>
                            <hr>
                        </div>
                        <div class="contcon">
                            <p><?php echo $row['content']; ?></p>
                        </div>
                    </div>
                    </a>
                <?php
                }}
                ?>

What I want to do is that when there are no results, the echo appears, but in doing so, it gives me this error. What can I do?

    
asked by Dev. Joel 29.12.2017 в 22:06
source

2 answers

1

You have several errors in your code.

First error

Already pointed out @DevJoel in his answer: store_result() is a method of the extension MySQLi , which is not necessary at all in PDO.

Second error

You try to make a comparison about num_rows <= 1 ... If it equals 1 means that you found a row.

Third error

You are underestimating the fetchAll method.

Let's read the documentation on this method:

  

PDOStatement::fetchAll - Returns an array that contains all rows in the result set

     

Return values

     

PDOStatement::fetchAll() returns an array that contains all the   remaining rows of the result set. The array represents each   row as an array with values of the columns, or as an object with   properties corresponding to each column name. It is returned   an empty array if there are zero results to obtain, or FALSE in case   of failure.

     

Using this method to obtain large result sets will result in strong demand from the system and, possibly, the   network resources . Instead of retrieving all the data and   manipulate them in PHP, consider using the database server   to manipulate the result sets. For example, you can   use the WHERE and ORDER BY clauses in SQL statements to restrict   the result before recovering them and processing them with PHP.

     

fetchAll in the PHP Manual

From what is stated in the documentation we can deduce two things:

  • Using the same variable $data you can know if the query showed results or not. That is, you do not need anything to use num_rows

  • Do not use fetchAll if you expect many results in your query. The code will not fail (or yes), but it can have performance consequences.

  • Assuming you do not expect millions of results (or several tens of thousands) in your query, your code could be optimized in this way:

     <?php
                       // Instancia de la conexión con los datos requeridos
                    $pdo = new PDO('mysql:host=localhost;dbname=help_programing', 'root', '');
                    $likeparametro= "%$BusquedaGet%";
                    $stmt  = $pdo->prepare("SELECT * FROM pageswebsites 
                                          WHERE titulo LIKE ? ORDER BY ID DESC");
                    //Ejecutamos la consulta , pasamos el parámetro para el like, 
                    //que será una variable que ya se construyó anteriormente
                    $stmt->execute([$likeparametro]);
                    $data = $stmt->fetchAll();//Obtenemos los resultados
                    //Iteramos sobre estos resultados.
    
                    if(!$data){ //Evalúas $data directamente
                        echo 'no hay resultados';
                    } else {
                        foreach($data as $row) {
                        ?>
                        <a class="contresult" href="?view=pagew&id=<?php echo $row['viewPage']; ?>&cateinfo=<?php echo $row['categoria']; ?>">
                        <div class="conResult">
                            <div class="titulocon">
                                <h2><?php echo $row['titulo']; ?></h2>
                                <hr>
                            </div>
                            <div class="contcon">
                                <p><?php echo $row['content']; ?></p>
                            </div>
                        </div>
                        </a>
                    <?php
                    }
                }
    ?>
    
        
    answered by 30.12.2017 / 00:32
    source
    0

    The error is clear store_result () does not exist in PDO since you do not need to transfer the results of your sentence, simply get the data as you are doing with fetchAll and work with them as you wish.

    $stmt->execute(array($likeparametro));
    //$stmt->store_result(); // Eliminar
    $data = $stmt->fetchAll();
    
        
    answered by 29.12.2017 в 23:26