Messages with php and ajax

2

At the moment of wanting to do a fetch_array send me this error and I can not solve it

  

Fatal error: Call to undefined method PDOStatement :: fetch_array ()

my code so far is the following

<?php 

            $query = "SELECT * FROM mensajes WHERE receiver = $us ORDER BY id DESC";
            $run = $conexion->query($query);

            while ($row = $run->fetch_array()) : <--- aqui marca el error

            ?>
            <!--  -->
              <div id="chat_data">
                <span style="color:green"><?php echo $row['nombre']; ?> </span>
                <span style="color:brown"><?php echo $row['message']; ?></span>
                <span style="float:right"><?php echo fecha($mensas['send']); ?></span>
              </div>

            <?php endwhile; ?>
            </div>
            <form action="single-mensajes.php" method="POST">
              <input type="text" name="name_empresa" placeholder="Ingresa el nombre" value="<?php echo($_SESSION['usuario'][3])  ?>">
              <textarea name="message" id="" cols="30" rows="1" placeholder="Ingresa el mensaje"></textarea>
              <input type="hidden" value="<?php echo $row['receiver']; ?>">
              <input type="hidden" value="<?php echo $row['emitter']; ?>">
              <input type="submit" name="submit" value="send">
            </form>

            <?php

            if (isset($_POST['submit'])) {

              $name = $_POST['name'];
              $message = $_POST['message'];
              $receiver = $_POST['receiver'];
              $emitter = $_POST['emitter'];

              $query = "INSERT INTO mensajes (emitter, receiver, message, name) VALUES('$emitter', '$receiver', '$message', '$name')";

              $run = $conexion->query($query);

              if ($run) {
                echo "<embed loop='false' src='chat.way' hidden='true' autoplay='true' />";
              }
            }            

            ?>
            </div>
    
asked by Cesar Gutierrez Davalos 27.06.2017 в 01:13
source

1 answer

3

Change the following line:

while($row = $run->fetch_array()){

for the following:

while($row = $run->fetchAll(PDO::FETCH_BOTH)){

What happens here is that if you use fetch as you said @ A.Cedano you are only getting a result regardless of whether the query returns 2 or more.

Regarding: PDO::FETCH__PALABRA you can use PDO::FETCH_BOTH , which returns an array with associative indexes and with numerical indexes or PDO::FETCH_ASSOC that only allows you to use associative indexes

    
answered by 27.06.2017 / 08:26
source