2012-07-17 5 views
-1

내 오류가 어디에 있는지는 확실하지 않지만 레코드 요청은 항상 동일한 것을 반환합니다. (잘 작동)MySQL 요청은 항상 동일한 레코드를 반환합니다.

우선 목록을 요청 - 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() .'}}'; 
} 

답변

2

당신은 당신이 단지 전체 sites 테이블을 가져 오기 때문에, 목록의 id 값을 사용하지 첫 번째 행을 해내 결코 , 그리고 그것을 돌려 보내라.

어딘가에 WHERE 절이 있어야합니다. id 필드를 바인딩하고 있지만 쿼리에 :id 매개 변수가 없으므로 기본적으로 연기가 나고 있습니다. 여러 행을 다시 얻을 것으로 기대하지 않는 한

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

ORDER BY은 무의미하다. 한 행 결과가 이미 주문되었습니다.

관련 문제