Simple query with mysqli

Hi, I am learning php and mysqli using W3schools as my resource. I have no issues with php itself, I can retrieve data from an html form. But when I try running a mysqli select statement, I get a 404 error.

I am able to log into mysql and query the database table from my terminal. Not sure what to look for. Below is my code:

<!DOCTYPE html>
<html>
<body>

<?php
$servername = "localhost";
$username = "richie-admin";
$password = "**OMITTED**";
$dbname = "test_user";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT firstname FROM Persons";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<br> id: ". " - Name: ". $row["Firstname"]. " ". "<br>";
}
} else {
echo "0 results";
}

$conn->close();
?>

</body>
</html>

Try mysqli_connect for connection
And mysqli_num_rows instead num_rows

I still get the same error. Here is my updated code below:

<!DOCTYPE html>
<html>
<body>

<?php
$servername = "localhost";
$username = "richie-admin";
$password = "**OMITTED**";
$dbname = "test_user";

// Create connection
$conn = new mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT firstname FROM Persons";
$result = $conn->query($sql);

if ($result->mysqli_num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> id: ". " - Name: ". $row["Firstname"]. " ". "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

</body>
</html>

Just put $conn=mysqli_connect

if($conn==false)
{ echo ‘error ocured’; }

There must be something simple I’m missing. Here is my updated code.

<!DOCTYPE html>
<html>
<body>

<?php
$servername = "localhost";
$username = "richie-admin";
$password = "**OMITTED**";
$dbname = "test_user";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn==false)
{ echo "Error Occurred"; }
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT firstname FROM Persons";
$result = $conn->query($sql);

if ($result->mysqli_num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> id: ". " - Name: ". $row["Firstname"]. " ". "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

</body>
</html>

A http 404 error (page not found) is because the URL you are entering in your browser’s address bar is wrong. It is not due to the php code and just randomly changing the php code is a waste of time.

You must to see if your url adress is true,if is true,then go to code checker online,you can find that on google

I don’t mean to waste time. The page was working fine when I grabbed values from a html form. This code seems to work fine::

<html>
    <head>
    <title>php example</title>
</head>
<body>
<strong>
        <?php
            $myVar = "Hello, ";
            $name = $_POST["name"];
            echo $myVar.$name; ?>
</strong>

    </body>
</html>

Thank you for the resource.

Ok my friend,thats what i can do for you :grinning::grinning:

I was able to get the w3schools example working. I had to enable the mysqli extension.

No…

Sponsor our Newsletter | Privacy Policy | Terms of Service