Problems linking to SQL file

Hello all,

I have a PHP file which is designed to link to a test SQL table which I’ve set up. The problem is that entering data into my search box isn’t getting the results I’m after.

First, the PHP code:

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

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

$sql = "SELECT ID, FirstName, LastName FROM 'create'";
$result = mysqli_query($conn, $sql);

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

mysqli_close($conn);
?> 

And the SQL file, copied and pasted from the CSV version, in order of ID, FirstName, LastName…

1,"Ryan","Butler"
2,"Ryan","Butler"
3,"Brent","Callahan"
4,"Harry","Callahan","makemyday@dirtyharry.net"
5,"Luke","Cage","luke@mywifeisdead.com",
6,"Jessica","Jones","jessica@getkilgrave.org",

The result of entering any of the above words in my search box should give me a result of 6. Jessica. Jones. (I’m not bothering to search for the emails, I don’t know why I included them.) Instead, I get “0 results” no matter what I type.

There’s probably just one thing that I have wrong, but I’ll be damned if I know what it is. Then again, PHP is not my forté.

1 Like
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

$sql = "SELECT ID, FirstName, LastName FROM 'create'";
$result = mysqli_query($conn, $sql);

var_dump($result);

What does that show?

1 Like

“bool(false)”

I found the answer in another forum – I was using single quotes where I should’ve used backstrikes.

Thanks tho!

1 Like

Unless you use reserved words for table names you don’t need to backtick them.

1 Like

Try this snippet.

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

// Create connection using the OOP style
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if($conn->connect_errno) {

    // Don't die any errors in here. You shouldn't be displaying any errors to the user at all.
    die('Could not connect to the database. Check the database information to make sure there are no typos. If this problem persists, the database engine could be done.');

}

$sql = "SELECT ID, FirstName, LastName FROM create"; // Your SQL query string
$stmt = $conn->prepare($sql); // Prepare the SQL query string
$stmt->execute(); // Execute the prepared SQL query string
$stmt->store_result(); // Store the results for later checking

// Check to see if there are any results
if($stmt->num_rows) {

    $stmt->bind_result($id, $firstname, $lastname); // Append variables to the columns that you specified

    // output data of each row
    while($stmt->fetch()) {

        print('ID: ' . $id); // Print the ID
        print('<br />'); // Create a line break
        print('Name: ' . $firstname . ' ' . $lastname); // Print the full name
        print('<br /><br />'); // Create double line break

    }

} else {

    print('There are no results at the moment'); // There are no data at this moment.

}

Also, if you are having a search box, wouldn’t you want to use LIKE %? Or are you searching for users using the WHERE clause? If you are using the WHERE clause, you should be using prepared statements.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.