-1

I am trying to solve something out. this time I am using mysqli and not PDO.

The problem: I am getting an error saying: "Notice: Trying to get property of non-object in ..." I only get that problem when I put the LIKE or " = " statement in the sql query, when I remove it, it is good.

What I am trying to do: Basically, I am saying if the option in the dropdown is selected, then get some info about the agencies in that specific city location only.

Here is my code:

if(isset($_POST["submit"])){

    $elpost = $_POST["cityName"];
    $sql = "SELECT office_name, office_location, office_phone FROM `easyt_findagents_agents WHERE office_city='$elpost";
    $result = $db->query($sql);

    //Display states list
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo $row['office_name'];
        }
    } else {

        echo "0 results";
    }
}

any thoughts on how to fix that? I've searched a lot but didn't find anything why this is happening.

Thanks in advance for everybody.

RiggsFolly
  • 89,708
  • 20
  • 100
  • 143
Jamal Jubran
  • 97
  • 1
  • 1
  • 8
  • Consult these following links http://php.net/manual/en/mysqli.error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code. – Funk Forty Niner Mar 31 '16 at 14:42
  • *"I only get that problem when I put the LIKE or " = " statement in the sql query, when I remove it, it is good."* - We can't say until we know exactly "how" you implemented that. Plus, not enough code/information to support the question. – Funk Forty Niner Mar 31 '16 at 14:42
  • If you're using the code you posted above, you have mismatched / missing quotes in your query. – devlin carnate Mar 31 '16 at 14:44
  • *Groan*.... you missed a tick here `easyt_findagents_agents` <<< at the end there, and is a typo; off-topic. Either add the missing one, or delete the first one. – Funk Forty Niner Mar 31 '16 at 14:46
  • You get this error when your query fails and `$db->query($sql);` returns `false` rather than an `mysqli_result object`. Your query has at least 2 errors in it as you posted it – RiggsFolly Mar 31 '16 at 14:47
  • *It's a typo Smokey* @RiggsFolly one too many ticks or one missing. – Funk Forty Niner Mar 31 '16 at 14:48

1 Answers1

1

Your query was missing a matching backtick and also a matching single quote.

It is always a good idea to check that an executed query actually worked, and its easy to do.

Its always a good idea to check the PHP Error log if you get problems, and if you are working on a LIVE server and error reporting is turned off, add the first 2 lines to your code while you are debugging it, so you can actually see if an error has occured.

error_reporting(E_ALL); 
ini_set('display_errors', 1);

if(isset($_POST["submit"])){

    $elpost = $_POST["cityName"];
    $sql = "SELECT `office_name`, `office_location`, `office_phone` 
            FROM `easyt_findagents_agents` 
            WHERE `office_city` = '$elpost'";

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

    // check query worked and report error if it did not
    if ( $result === FALSE ){
        echo $db->error;
        exit;
    }


    //Display states list
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo $row['office_name'];
        }
    } else {

        echo "0 results";
    }
}

Oh, you are also open to SQL Injection. You would be well advised to read up on that starting with this

Community
  • 1
  • 1
RiggsFolly
  • 89,708
  • 20
  • 100
  • 143