Chat Ajax and PHP

0

I'm trying to send some data from my database depending on the sender's id and the id of the receiver but this one throws me an error.

  

Fatal error: Call to a member function fetch () on boolean in line 16

EDITED

<?php 

require 'functions.php';

$host = "localhost";
$user = "root";
$pass = "";
$db_name = "empleos";

$conexion = new PDO('mysql:host=localhost;dbname='.$db_name, $user, $pass);

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

            while ($row = $run->fetch(PDO::FETCH_ASSOC)) :

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

            <?php endwhile; ?>

the $ us variable comes from a file called functions.php

@$us = $_SESSION['usuario'][0];

previously worked perfectly but I put the conditionals in the query WHERE receiver = $us AND emitter = emitter and it was there when he started to throw that error

in my database I have it in the following way

    
asked by Cesar Gutierrez Davalos 29.06.2017 в 00:59
source

2 answers

0
  

Try using queries prepared for your query since the one   you have is vulnerable to sql injection

The error is in your sentence, emmiter = emitter you should have passed a variable emmiter = $emitter

Try this better:

<?php 
    require 'functions.php';
    //use contstantes para añadir un poco de seguridad y evitar posibles modificaciones que algún hacker podría hacer
    DEFINE('HOSTDB',"host=localhost;dbname=empleos");
    DEFINE('UNAME',"root");
    DEFINE('PASS', '');

    $conexion = new PDO(HOSTDB, UNAME, PASS);
    $stmt = "SELECT * FROM mensajes WHERE receiver = :us ORDER BY id ASC";

    $result = $conexion->prepare($stmt); //preparas la consulta
    $result->execute(array(":us"=>$us)); //le pasas parametros y ejectutas

    while ($row = $result->fetch(PDO::FETCH_BOTH)) : <-- Aqui tira el error

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

<?php endwhile; ?>
    
answered by 29.06.2017 в 01:58
0

The error is appearing because the statement of your query is wrong, so when executing the function query() is probably returning false , and hence the error " Call to a member function fetch () on boolean "which means that you can not execute the fetch() function on a boolean.

When you use PDO objects to manage your connections and queries to the database, you have to parameterize your variables in only one way, you can not mix them.

As your code is not inclined for one or another way of parameterizing, I present two options. (We will only see the code of the query, because the creation of your object PDO and your connection all is fine)

1. Placeholder

$query = 'SELECT * FROM mensajes WHERE receiver = ? AND emitter = ? ORDER BY id DESC';  
$run = $conexion->prepare($query);
$run->execute([$us, $emitter]);

You can notice that first you have to prepare the query and the already prepared query is sent to execute. You can also notice that when you send the query, you must send it an array with the information that will replace the question marks. The order of the array that you send with the function execute must be the same order in which you want them to appear in the question marks. After this minimum change, your code will remain the same, and you should not have any problems.

2. Marker by name

$query = 'SELECT * FROM mensajes WHERE receiver = :us AND emitter = :emitter ORDER BY id DESC';  
$run = $conexion->prepare($query);
$run->execute(["us" => $us,"emitter" => $emitter]);

This form differs only in that instead of a question mark, you can put a name that identifies the variable. The behavior is the same and as you can see, instead of the execute function receiving a simple fix, it receives an associative arrangement, where each name that you put in your query is related to a variable. For this case, it does not matter what order the associative arrangement is in.

Either of these two options should correct the error you have and you could run your code as normal without any extra change. Greetings.

    
answered by 29.06.2017 в 16:39