1

In my PHP code, i'm easily writing records to my database but for some reason i can't read anythign out. My PHP code is:

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

$sql = "SELECT * FROM companies";

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

    while($row = $result->fetch_assoc())
    {
        echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
    }
}
else
{
    echo "query failure";
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$sql = "INSERT INTO companies (name) 
        VALUES ('mycompany')";

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

    echo "insert success";
}
else
{
    echo "insert failure";
    echo "Error: " . $sql . "<br>" . $conn->error;
}

The output I get from the browser when i run it is: query failureError: SELECT * FROM companies insert success

I've tried variations of apostrophes, carets, quotes in that $sql string. I've tried running this query in HeidiSQL and it works fine. Any ideas where I'm going wrong? Any suggestions of more basic stuff I can try to narrow down the source of the problem?

thanks!

2
  • 1
    You don't actually bind the results from your query, if ($result = $conn->query($sql)). But that's not the source of your issue, take a look at mysqli_error and error_reporting(-1);. Commented Dec 11, 2015 at 16:26
  • You should consider using a condition so that either the select is run, or the insert is run, or the insert is run after the select, or the select is run after the insert. Right now, your code is re-using the same variable without any controls on when that variable is assigned a new value. Commented Dec 11, 2015 at 16:53

4 Answers 4

6

Using mysqli->query() with a SELECT statement returns an instance of mysqli_result. It is not identical to true (=== true), but nor does it represent an error.

Moreover, $result is undefined.

Use this instead:

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

$sql = "SELECT * FROM companies";

if (($result = $conn->query($sql)) !== FALSE)
{
    echo "query success";

    while($row = $result->fetch_assoc())
    {
        echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
    }
}
else
{
    echo "query failure";
    echo "Error: " . $sql . "<br>" . $conn->error;
}
...

This simply changes your === TRUE check to !== FALSE. MySQLi::query() returns boolean FALSE on failure, boolean TRUE on a successful query without result sets or a mysqli_result upon success with a result set. This also assigns the result of query() into $result.

Sign up to request clarification or add additional context in comments.

6 Comments

Looks like the OP took off without saying goodbye and not leaving a nice fat tip ;-)
Good comments from everyone. I would say that all the information I received helped to solve this. Actually I was thinking perhaps I should be doing this to test the query results $result = $conn->query($sql); if ($result->num_rows > 0) Anyone agree with that?
@mitch Yes, that would make more sense Mitch, however it could fail (at some point) and you would need to use a WHERE clause (it could work better) >>> depending on what you're really wanting to achieve here. You could use mysqli_error($conn) on query error though, or a mix of.
@mitch that could result in a Trying to get property of non-object, since $result === false if query failed.
Thanks. I'm now checking if($result !== FALSE) followed by my check on $result->num_rows. Does that seem good?
|
1

You have not assign the query result to $result variable.

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

    $sql = "SELECT * FROM companies";
    $result = $conn->query($sql);
    if ($result === TRUE)
    {
        echo "query success";

        while($row = $result->fetch_assoc())
        {
            echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
        }
    }
    else
    {
        echo "query failure";
        echo "Error: " . $sql . "<br>" . $conn->error;
    }


    $sql = "INSERT INTO companies (name) 
            VALUES ('mycompany')";

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

        echo "insert success";
    }
    else
    {
        echo "insert failure";
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

Comments

1

The problem is that a SELECT query will return a mysqli_result object on SUCCESS not a boolean TRUE. Only if the query fails, will it return a boolean FALSE;

Therefore you should use it like this :

$result = $conn->query($sql);

if ($result !== FALSE){
    while($row = $result->fetch_assoc())
    {
        echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
    }
}

Comments

1

just change your if condition :-

if ($result = $conn->query($sql))
{
    echo "query success";

    while($row = $result->fetch_assoc())
    {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.