需要有关mysqli PHP select语句的帮助吗

需要有关mysqli PHP select语句的帮助吗,php,mysqli,prepared-statement,Php,Mysqli,Prepared 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

我这里有几个问题,所以非常感谢您的帮助。我这里有三页

//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对象上执行
bind_param
execute
方法:

$DBH->bind_param("ii", $start, $end);
然后执行以下语句:

$DBH->execute();
只要仔细看一下这张照片就可以了

从第一个例子

<?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();
?>
对这样的事情:

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

谢谢从第3页调用函数时,应如何绑定参数?我应该在第2页这样做,但在第3页设置变量吗?变量将发生变化。例如,只需将代码的第一行添加到第2页,然后在调用第3页中的函数时设置变量。例如:第3页-$start=0$结束=5;选择信息();
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;
}