简体   繁体   中英

Using php to select with prepared statement and echoing the result or turning it into variable

I'm working on a localhost project. Even though I might not need it, but while learning php and mysql I figured I should use prepared statements. I'm not quite getting through with it even though I've found some great guides online.

What I'm looking for is simply to echo the name from my database. Later on I'd need to turn it into variable, but since I'm already stuck with this, I figured I'd ask here.

require_once "config.php";

if ($link->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if ($stmt = $mysqli->prepare("SELECT name FROM users")) {
    $stmt->execute();
    $stmt->bind_result($name);

    while ($stmt->fetch()) {
        echo ("$name");
    }
    $stmt->close();
}
$mysqli->close();

If this line is correct

if ($link->connect_error) {

then this line is WRONG

if ($stmt = $mysqli->prepare("SELECT name FROM users")) {

and this statement is also WRONG

$conn->connect_error

as the connection variable is either $link or $mysqli or $conn but it is unlikely to be all og them!

Or maybe both are wrong. The answer will be in the code you didn't show us in config.php , so check what variable you used in that script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM