Riley Mclaughlin Riley Mclaughlin - 5 months ago 7
SQL Question

How To Get a Single Number From a Database in PHP

I need to echo the number

1
or
0
whatever the database has in that cell. I cant seem to get it to work. What am I doing wrong people of the internet?

Picture of the database

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testfp";
$conn = new mysqli($servername, $username, $password, $dbname);


$sql = "SELECT hookup FROM testfp WHERE name = 100100" ;
$bob = mysql_query($sql);
echo $bob;
?>

Answer

You can not use mysql and mysqli together. So you can try the following code (using mysqli only, since mysql is deprecated) :

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

$sql = "SELECT hookup FROM testfp WHERE name = 100100" ;
$result = $conn->query($sql);
if($result->num_rows>0)
{
    while($row = $result->fetch_assoc()) {
        echo $row["hookup"];
    }
}
else
{
    echo "No results found!";
}
?>
Comments