需要 mysqli PHP select 语句的帮助

发布于 2024-10-07 21:22:17 字数 1052 浏览 3 评论 0原文

我这里有几个问题,因此我们将不胜感激。我这里有三页。

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

如果可能的话。有人可以帮我吗?

I have a couple of questions here, so any help will be greatly appreciated. I have three pages here.

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

This probably looks like a complete mess, but hopefully you can get the idea i am trying to do here. I would like to be able to fetch the data so that i can display it something like

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

if that is possible. Can anyone please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情废物 2024-10-14 21:22:18

您需要在 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;
}

You need to execute the bind_param and execute method on your mysqli object:

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

And then execute the statement:

$DBH->execute();

Just have a close look at the mysqli API.

From php.net first example.

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

You should change your function:

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

To something like this:

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;
}
深海不蓝 2024-10-14 21:22:18

mysqli_prepare 文档中解释了您需要的所有内容。

Everything you need is explained in the mysqli_prepare documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文