Mikecul Mikecul - 1 year ago 71
MySQL Question

Blank MySQL error on shared hosting

I am doing a little bit of learning on mysql, php and the like. I'm using a shared hosting plan so am quite limited from a settings changes point of view.

I am attempting to run a simple mysql select command through PHP, but all i get back is a blank error

$typeID = $_GET['tid'];
//variables for the database server
$server = "localhost";
$user = "codingma_rbstock";
$pwd = "M@nL%V{%RI+h";
$db = "codingma_rbstock";

//variables for the database fields

//database connection
//create connection
$conn = new mysqli($server, $user, $pwd, $db);

//if the connection fails throw an error.
if ($conn->connect_error){
die("Connection Failed: " . $conn->connect_error);

echo "Welcome to " . $typeID . "<br>";

$sql = "select ITEM_NAME from stock where ITEM_NO='00001'";

if ($conn->query($sql) === TRUE){
$res = $conn->query($sql);
if ($res->num_rows > 0){
echo "success";

echo "Error: " .$sql . "<br>" . $conn->error;

echo $res;


I have checked and it seems to be connecting to the database fine (I changed a few account details to see if that threw a different error and it did).

I am sure I am missing something completely obvious here! The below is the text output from the error;

Error: select ITEM_NAME from stock where ITEM_NO='00001'

Thanks for any help.

Answer Source

your problem is in this line

if ($conn->query($sql) === TRUE){

you are doing a variable type check ( === ), the result of that comparision will always fail because, for as long as you have data in your table and your query doesn't fail $conn->query($sql) will not return a boolean value

mysqli::query documentation says:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

You are using here a SELECT, therefore a successfull result won't be boolean

Try switching to

if ($conn->query($sql) == TRUE){

Or even better remove that if completely


The better approach for that part of the code is:

$res = $conn->query($sql);
        if ($res->num_rows > 0){
            echo "success";

if ($res === false) {
    echo "Error: " .$sql . "<br>" . $conn->error;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download