开发者

Need Help With mysqli PHP select Statement

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 th开发者_StackOverflow中文版e 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?


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


Everything you need is explained in the mysqli_prepare documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜