trying to get property of non-object

i have a problme in my php code .. trying to get property  of non object ..this is the code .
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "job_database";

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

if(isset($_POST['Get']))
{
    $jtitle = $_POST['jobtitle'];
    $location = $_POST['location'];
    $category = $_POST['categories'];
    //Query specified database for value
    $sql = "SELECT jtitle , location, category FROM addjob where jtitle =".$jtitle." AND location =".$location." AND category =".$category." " ;
 $result = $conn->query($sql);
}

if ($result->num_rows > 0) {

  // output data of each row
 while($row = $result->fetch_assoc()) {
 echo "jtitle: " . $row["jtitle"]. "location:" . $row["location"]. "category" . $row["category"]."<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

Open in new window

ahmed dinarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mohan singhWeb developerCommented:
Your query syntax is wrong

Your write  like this
 $sql = "SELECT jtitle , location, category FROM addjob where jtitle =".$jtitle." AND location =".$location." AND category =".$category." " ;

Open in new window


Do like this
 $sql = "SELECT jtitle , location, category FROM addjob where jtitle ='$jtitle' AND location ='$location'  AND category ='$category' " ;

Open in new window

0
Olaf DoschkeSoftware DeveloperCommented:
If you don't make a POST request with $_POST['Get'] set, $result is never set, then any code addressing it will not find $result, so $result->fetch() won't work and $row also will not be generated.

Bye, Olaf.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
Also, last note - your code is susceptible to SQL injection. Make sure you either run mysqli_real_escape_string on your variables BEFORE you use them in your query:
http://php.net/manual/en/mysqli.real-escape-string.php

...or use prepared statements:
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

Either approach will fix this vulnerability.
0
gr8gonzoConsultantCommented:
If this question is going to be closed, the answer to the original problem is addressed by Olaf in answer
#42520465. He should get full points.
0
gr8gonzoConsultantCommented:
Also, @mohan, the query syntax is not wrong. It works either way (and your suggested way actually performs a tiny bit worse). If you are going to suggest the OP's code is wrong, please test it first to make sure you are right before making that suggestion.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.