2010-12-16 4 views
0

여기 몇 가지 질문이 있으므로 도움을 주시면 대단히 감사하겠습니다. 여기에 3 페이지가 있습니다.mysqli PHP 도움말 선택 문의

//Page 1 - Constants 

$dbhost = "database.url.com"; //Just made up 
$dbname = "dbname"; 
$dbuser = "dbuser"; 
$dbpass = "123456"; 


//Page 2 - The Function 

//This is where i need to write the function select information from the database. 

include ("include/page1.php"); 
$DBH = new mysqli($dbhost, $dbuser, $dbpass, $dbname); 
function selectInfo(){ 
$stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?"); 
} 
//This function obviously is not complete because I keep recieving different error messages. I guess i cannot figure out how to write a prepared select statement. Need some help here. 


//Page 3 - Where the function is called and where the user would be 
include ("include/page2.php"); 

//need to be able to call the function here with variables set. 

$start = 0; 
$end = 5; 
selectInfo(); 
echo the data that i need in the database. 

이것은 아마도 완전히 혼란스러워 보일 것입니다.하지만 여기에서하려고하는 아이디어를 얻을 수 있기를 바랍니다. 가능한 경우 데이터를 가져와

echo $stmt->title; 
echo $stmt->id; 

과 같이 표시 할 수 있도록 데이터를 가져오고 싶습니다. 아무도 나를 도울 수 있습니까?

답변

1

당신은 당신의 mysqli 개체의 bind_paramexecute 방법을 실행해야합니다

$DBH->bind_param("ii", $start, $end); 

그리고는 문을 실행 :

$DBH->execute(); 

는 그냥 mysqli API에 가까운 모습을 가지고있다.

php.net 첫 번째 예. 이 같은 것으로

function selectInfo(){ 
    $stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?"); 
} 

:

function selectInfo($limit, $offset){ 
    $stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?"); 
    $stmt->bind_param("ii", $limit, $offset); 
    $stmt->execute(); 

    // Other stuff to get your values from this query 
    ... 

    // Return the object with the results 
    return $values; 
} 
+0

덕분에 당신은 당신의 기능을 변경해야합니다

<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5"; if ($stmt = $mysqli->prepare($query)) { /* execute statement */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($name, $code); /* fetch values */ while ($stmt->fetch()) { printf ("%s (%s)\n", $name, $code); } /* close statement */ $stmt->close(); } /* close connection */ $mysqli->close(); ?> 

@mcbeav! 3 페이지에서 함수를 호출 할 때 매개 변수를 어떻게 바인딩해야합니까? 2 페이지에서 그렇게해야 하나 3 페이지에 변수를 설정합니까? 변수가 변경됩니다. 예를 들어 페이지 번호 2에 코드의 첫 번째 행을 추가 한 다음 페이지 3의 함수를 호출 할 때 변수를 설정합니다. ex : Page - 3 - $ start = 0; $ end = 5; selectInfo(); – mcbeav

1

필요한 모든 작업은 mysqli_prepare 설명서에 설명되어 있습니다.