user977101
user977101

Reputation: 161

php/SQL returns NULL

I'll be the first to admit I'm an idiot, but is there any reason for why this query returns NULL? The connection to the DB is working just fine and I'm able to POST to it.

<?php
$servername = "server";
$username = "user";
$password = "pass";
$dbname = "db-name";

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

$sql = "SELECT * FROM mytablename";
$results = mysqli_query($sql,$conn);

if ($results !== false) {
    var_dump($results);
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

Upvotes: 2

Views: 610

Answers (1)

Sougata Bose
Sougata Bose

Reputation: 31749

mysqli_query needs the first parameter to be the link identifier and the second be the query. Try with -

$results = mysqli_query($conn, $sql);

DOCS

Update

$results be the resource, not the table data. If you need the data then you need to use

$fetchData = mysqli_fetch_object($result)

Upvotes: 3

Related Questions