PHP错误:为什么不是这个工作,我不能看到数据导致我的网页?

问题描述 投票:-2回答:1

我'建立一个新的wampserver并创建了一个表一个新的数据库名为“用户”

阿帕奇2.4.23和7.1.10 PHP和MySQL 5.7.14

<?php

    $server = 'localhost';
    $serverUsername = 'root';
    $serverPassword = '';
    $dbName = 'test';

    $connection = mysqli_connect($server,$serverUsername,$serverPassword);
    msqli_select_db($dbName);
    if(!$connection){
        echo 'connection failed to database '.mysqli_connect_error();
    }
    $sql = "SELECT * FROM users";
    $query = mysqli_query($sql);
    while($row = mysqli_fetch_array($query)){
        print_r($row);
    }

?>

我的价值确实是在数据库中,但没有运行代码后,出现在页面

php mysqli
1个回答
1
投票

看一看意见提的修复

$server = 'localhost';
$serverUsername = 'root';
$serverPassword = '';
$dbName = 'test';

// FIX 1
// You need to mention the database name as the last argument
$connection = mysqli_connect($server,$serverUsername,$serverPassword, $dbName);
if(!$connection){
    echo 'connection failed to database '.mysqli_connect_error();
}

$sql = "SELECT * FROM users";

// FIX 2
// The first argument should be your mysqli connection
$query = mysqli_query($connection, $sql);

// Check for errors
if (!$query) {
    printf("Error: %s\n", mysqli_error($connection));
    exit();
}

while($row = mysqli_fetch_array($query)){
    print_r($row);
}

?>

参考了mysqli_connecthttps://secure.php.net/manual/en/function.mysqli-connect.php

参考了mysqli_queryhttps://secure.php.net/manual/en/mysqli.query.php

© www.soinside.com 2019 - 2024. All rights reserved.