2013-04-18 6 views
-1

누가 조언을 줄 수 있습니까? 왜 쿼리가 내게 기대 값을 제공 할 수 없습니까? 감사.MySQL 준비 문 - 선택

$mysqli = new mysqli($GLOBALS["mysql_host"], $GLOBALS["mysql_user"],   $GLOBALS["mysql_passwd"], $GLOBALS["mysql_database"]); 
$stmt = $mysqli->prepare("SELECT one FROM table ORDER BY date DESC LIMIT 1"); 
$last = $stmt->bind_result($one); 
$stmt->execute(); 
$stmt->close(); 
$mysqli->close(); 

Echo $last; //it should be "abc" 
+0

PDO를 사용하지 마십시오 : 댓글을 남기 –

+0

귀하의 질문은 "누가 내게 조언을 줄 수 있습니까?"입니다. 자네가 필요한 걸 이해하는 사람. 이를 위해 귀하의 질문은 모호하지 않아야합니다. – Cubetastic

답변

4

난 당신이 execute에있는 다음 mysql_stmt -objects에 fetch를 호출 생각합니다. 결과 (행)가 여러 개있을 수 있습니다.

fetch을 입력하면 커서가 앞으로 나아갑니다.

문서 : mysqli mysqli-stmt

내가 할 수있는
<?php 
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); 

if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

/* prepare statement */ 
if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) { 
    $stmt->execute(); 

    /* bind variables to prepared statement */ 
    $stmt->bind_result($col1, $col2); 

    /* fetch values */ 
    while ($stmt->fetch()) { 
     printf("%s %s\n", $col1, $col2); 
    } 

    /* close statement */ 
    $stmt->close(); 
} 
/* close connection */ 
$mysqli->close(); 

?> 
0

.
어드바이스는 평범하고 단순하게 될 것이다 : -1 @ mysqli

를 사용하는 대신

$stmt = $pdo->prepare("SELECT one FROM table ORDER BY date DESC LIMIT 1"); 
$stmt->execute(); 
$last = $stmt->fetchColumn(); 

echo $last; //it should be "abc" 

, 간단한 청소 및 작동

+1

MySQLi의 문제점은 무엇입니까? –

+0

사실, 나는 PDO도 선호합니다. 좋은 추상화 수준이 있습니다. [nettuts에서 좋은 설명이 있습니다] (http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/) –