Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从MySQL表中选择数据';一行_Php_Mysql_Row - Fatal编程技术网

PHP从MySQL表中选择数据';一行

PHP从MySQL表中选择数据';一行,php,mysql,row,Php,Mysql,Row,完整的PHP代码: <?php $servername = "localhost"; $username = "*****"; $password = "**********"; $dbname = "x1_1"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname);

完整的PHP代码:

<?php
        $servername = "localhost";
        $username = "*****";
        $password = "**********";
        $dbname = "x1_1";

        // Create connection
        $conn = mysqli_connect($servername, $username, $password, $dbname);

        // Check connection
        if (!$conn) {
                die("Connection failed: " . mysqli_connect_error());
        }
        // Print out to console success
        echo '<script>console.log("Connected successfully! ")</script>';

        // sql to create table
        $sql = "CREATE TABLE MyGuests (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
            firstname VARCHAR(30) NOT NULL,
            lastname VARCHAR(30) NOT NULL,
            email VARCHAR(50),
            reg_date TIMESTAMP
        )";

        // Check if table was created
        if ($conn->query($sql) === TRUE) {
            // Was created
                echo '<script>console.log("Table MyGuests created successfully! ")</script>';
        } else {
            // Was not created
                echo '<script>console.log("Error creating table: ' . $conn->error . '")</script>';
        }

        // Insert data into table
        $sql = "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('John', 'Doe', 'john@example.com');";

        $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('Mary', 'Moe', 'mary@example.com');";

        $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('Julie', 'Dooley', 'julie@example.com')";

        // Check if values were inserted successfully
        if ($conn->multi_query($sql) === TRUE) {
            // Inserted successfully
                echo '<script>console.log("New records created successfully! ")</script>';
        } else {
            // Was not inserted successfully
                echo '<script>console.log("Error: '. $sql .' "<br>" ' . $conn->error . '")</script>';
        }

        // Selects [ID, Firstname, and Lastname] from table
        $sql = "SELECT id, firstname, lastname FROM MyGuests";
        $result = $conn->query($sql);

        // If the number of rows is greater than zero print the data in the row. Else print zero results
        if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
                }
        } else {
                echo "0 results";
        }

        $conn->close();
    ?>
        // Selects [ID, Firstname, and Lastname] from table
        $sql = "SELECT id, firstname, lastname FROM MyGuests";
        $result = $conn->query($sql);

        // If the number of rows is greater than zero print the data in the row. Else print zero results
        if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
                }
        } else {
                echo "0 results";
        }

在if条件中的
num_rows
之后添加导致内部服务器错误的
($result->num_rows()>0)
。num_rows是属性,不是方法,所以不是()。在
if($result
…行之前添加一行,以回显$result->num_行的值。表示了解的语句。甚至不安全!@sloather echo'console.log(“$result->num_行”);在if条件下返回内部错误Add
()
num_行之后作为
if($result->num_行()>0)
导致内部服务器错误。num_rows是一个属性,不是一个方法,所以不是()。在
if($result
…行)前面添加一行,以回显$result->num_rows的值。表示了解的语句。甚至不安全!@sloatherasher echo'console.log(“$result->num rows”);返回内部错误