0

我想执行一个在回调中返回给我的请求,

事实上,这是一个系统,当我点击某个东西时,它会显示对话,

所以我开始进行查询,当我单击一个元素时,它会将数据发送到返回请求的 php 文件。

下面是我的jQuery代码:

 $( ".load_message" ).click(function() {
            //On marque uniquement l'id de l'expediteur et du destinataire
            // pour chercher les messages expédiés par A vers B ou inversement
            var from = $(this).closest('tr').find('span.from').text();
            var to = $(this).closest('tr').find('span.to').text();
            $.ajax({
                type: 'POST',
                url: 'pages_ajax/fetch-messages.php',
                data: { from: from,  to: to},
                dataType: "json",
                beforeSend:function(){
                    // this is where we append a loading image
                    $('#message_display').empty()
                    $('#message_display').attr('style','text-align:center');
                    $('#message_display').append('<img src="images/loading.gif" alt="Loading..." />');
                },
                success:function(data){
                    // successful request; do something with the data
                    if(data.error==1){
                        $('#message_display').empty().removeAttr('style');

                    }
                    // if faliure
                    else if(data.error==0){
                        // failed request; give feedback to user
                        $('#message_display').empty().removeAttr('style');
                        $('#message_display').attr('class','error').empty().append('<b>Il n\'y a pas de messages à afficher.</b>');
                    }
                },
                error:function(){
                    // failed request; give feedback to user
                    $('#message_display').empty().removeAttr('style');
                    $('#message_display').attr('class','error').empty().append('<b>Il n\'y a pas de messages à afficher.</b>');
                }
            });
        });

在 php 文件中它只是返回我一个 sql 请求

这是我的 php 文件

<?php

session_start();
require_once("../../lib_php/librairie.php");
require_once("../../config/connexion.php");
header('Content-Type: application/json; charset=utf8');

/**
 * Fonction qui retourne une liste de messages
 * @return int
 */
function fetchMessages() {
    $from = mysql_real_escape_string($_POST['from']);
    $to = mysql_real_escape_string($_POST['to']);

    $query = "SELECT `id`, `from`, `to`, `message`, `sent`, `read`, `direction` 
    FROM `cometchat` 
    WHERE `from` = {$from} || `from` = {$to} || `to` = {$to} || `to` = {$from}";
    $data = mysql_query($query);
    if (mysql_num_rows($data) == 0) {
        $data['error'] = 1;
        $data['sql'] = $data;
    } else {
        $data['sql'] = null;
        $data['error'] = 0;
    }

    return $data;
}

echo fetchMessages();

我不知道如何继续使用该查询进行循环以将所有结果显示到 div 中,因为这是我第一次返回查询,通常我只返回一些我应用的变量。但是这次我有太多的数据太显示了。

任何帮助将不胜感激。

4

2 回答 2

0

第 1 步:不要使用 MYSQL_*,出于安全考虑,它已被弃用。第 2 步:使用准备好的语句。

我在这里重写了你的 php:

function fetchMessages() {
    $from = $_POST['from'];
    $to = $_POST['to'];

    $query = $database->prepare("SELECT `id`, `from`, `to`, `message`, `sent`, `read`, `direction` 
    FROM `cometchat` 
    WHERE `from` = ? || `from` = ? || `to` = ? || `to` = ?");
    $query->bind_param('ssss' $from, $to, $to, $from);
    $result=$query->execute();
    $query->data_bind($id, $rfrom, $rto, $message, $sent, $read, $direction);

    $data=""; //initialize the HTML you want in here, any sort of header for the data
    while ($query->fetch()){
      $data=$data.$id." ".$rfrom." ".$rto." ".$message." ".$sent." ".$read." ".$direction."<br/> //or whatever html formatting you want to do
    }
    $data=$data."<br/>" //or whatever tailing html you want to add to the data

    return $data;
}
echo  fetchMessages()
于 2013-11-01T19:44:48.953 回答
0

尝试这个:

function fetchMessages() {
    $from = mysql_real_escape_string($_POST['from']);
    $to = mysql_real_escape_string($_POST['to']);

    $query = "SELECT `id`, `from`, `to`, `message`, `sent`, `read`, `direction` 
    FROM `cometchat` 
    WHERE `from` = {$from} OR `from` = {$to} OR `to` = {$to} OR `to` = {$from}";

    $results = mysql_fetch_assoc(mysql_query($query));

    $data = array();
    if (!sizeof($results)) {
        $data['error'] = 1;
        $data['sql'] = $query;
    } else {
        $data['sql'] = null;
        $data['error'] = 0;
        $data['results'] = $results;
    }

    return $data;
}

用法:

$data = fetchMessages();
if ($data['error']) {
    // show error
}
else
{
    foreach ($data['results'] as $result) {
        // debug
        print_r($result);
    }
}
于 2013-11-01T19:37:27.967 回答