簡體   English   中英

需要幫助mysqli PHP選擇Statement

[英]Need Help With mysqli PHP select Statement

我在這里有幾個問題,所以任何幫助都將不勝感激。 我這里有三頁。

//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;

如果可能的話。 誰能幫幫我嗎?

你需要的一切都在mysqli_prepare文檔中解釋。

您需要在mysqli對象上執行bind_paramexecute方法:

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

然后執行語句:

$DBH->execute();

仔細看看mysqli API

來自php.net的第一個例子。

<?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

你應該改變你的功能:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM