MySQL请求始终返回相同的记录

时间:2012-07-17 16:55:14

标签: php jquery mysql pdo

我不确定我的错误在哪里,但记录请求总是返回相同的错误。

首先我要求列表(工作正常) - list.js

 $('#listPage').bind('pageinit', function(event) {
    getList();
});

function getList() {
    $.getJSON(serviceURL + 'getlist.php', function(data) {
        $('#list li').remove();
        list= data.items;
        //edit function below based on db table
        $.each(list, function(index, sites) {
            $('#list').append('<li><a href="details.html?id=' + sites.id + '">' +
                    '<h4>' + sites.title + '</h4>' +
                    '<p>' + sites.address + '</p></a></li>'
                    );
        });
        $('#list').listview('refresh');
    });
}

getlist.php

include 'config.php';
//change FROM to ____
$sql = "SELECT id, title, address, picture1 FROM sites ORDER BY title";

try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->query($sql);  
    $details = $stmt->fetchAll(PDO::FETCH_OBJ);
    $dbh = null;
    echo '{"items":'. json_encode($details) .'}'; 
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

然后在下面的某个地方,记录搞砸了。 - 的 details.js

$('#detailsPage').live('pageshow', function(event) {
    var id = getUrlVars()["id"];
    $.getJSON(serviceURL + 'getdetails.php?id='+ id, displayDetail);
});

function displayDetail(data) {
    var sites = data.item;
    console.log(sites);
        $('#pic').attr('src', 'http://hh.lpbp.net/assets/uploads/files/' + sites.picture1);
    $('#title').text(sites.title);
    $('#address').text(sites.address);
    $('#pic2').attr('src', 'http://hh.lpbp.net/assets/uploads/files/' + sites.picture2);
    if (sites.phone) {
        $('#actionList').append('<li><h3>Phone #:</h3>' + '<p>' + '<a href="tel:' + sites.phone + '">'+ sites.phone + '</a></p></li>');
    }
    if (sites.website) {
        $('#actionList').append('<li><h3>Website</h3>' + '<p>' + '<a href="#" onClick="Ti.App.fireEvent(\'openURL\', { url:\'http://' +sites.website +'\'}); return false;">' + sites.website + '</a></p></li>');
    }
    if (sites.description) {
        $('#actionList').append('<li><h3>Description</h3>' + '<p>' + sites.description + '</p></li>');

    }
    $('#actionList').listview('refresh');

}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

getdetails.php

include 'config.php';
//Change FROM ___
$sql = "SELECT id, title, address, phone, website, description, picture1, picture2 FROM sites ORDER BY title";

try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare($sql);  
    $stmt->bindParam("id", $_GET[id]);
    $stmt->execute();
    $detail = $stmt->fetchObject();  
    $dbh = null;
    echo '{"item":'. json_encode($detail) .'}'; 
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

1 个答案:

答案 0 :(得分:2)

您永远不会使用列表的id值,因此您只需获取整个sites表,拉出第一行,然后返回。

在那里你应该有一个WHERE条款。您绑定了id字段,但查询中没有:id参数,所以基本上你是冒烟了。

$sql = "SELECT blah,blah,blah FROM sites WHERE id = :id";
                                        ^^^^^^^^^^^^^^^

ORDER BY毫无意义,除非您期望获得多行。已经订购了一行结果。