MySQL请求总是返回相同的记录


MySQL request always returns the same record

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

首先,我要列表(工作正常)-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() .'}}'; 
}

您从不使用列表的id值,所以您只需获取ENTIRE sites表,取出第一行,然后返回。

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

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

ORDER BY毫无意义,除非您希望返回多行。一行结果已排序。