-1

Attempting to try and join my table called 'UserCaseMessages' with my other table called 'Comments' using this exact sql command in php:

$sql = "
        SELECT UserCaseMessages.DefendantID, Comments.CommentID 
          FROM UserCaseMessages 
    INNER JOIN Comments 
            ON UserCaseMessages.DefendantID = Comments.SenderID
        ";

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

if ($result->num_rows > 0) // ERROR GETS THROWN ON THIS LINE {

}

Here is the connection part to my database (which occurs prior to the query):

$servername = "localhost";
$username = "username";
$password = "password";

// Get the username from the post
$userUsername = $_POST['string'];


// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

I get the following error:

PHP Notice: Trying to get property of non-object

I cannot figure out what I am doing wrong here!! Everything is spelled correctly and a checked and checked for any syntax errors and I can't find any, however I am new to this particular method. Please help!

Garret Kaye
  • 2,412
  • 4
  • 21
  • 45
  • 2
    I don't think the PHP error is coming from that particular line. You need to show the code that is generating the error. – Gordon Linoff Mar 14 '16 at 13:49
  • PHP error, not an SQL error. – jarlh Mar 14 '16 at 13:49
  • Please edit question by adding: your table structure; the code section of your db connection / query; exact code line that cause error. – fusion3k Mar 14 '16 at 13:52
  • @jimmy: The error means that the database query is failing and thus `$result` is not a result object. (It's probably just `false`.) There's probably a method on `$conn` which can be used to check for potential errors after executing a query. Something in the query is failing, and you need to get *that* error message to determine what. – David Mar 14 '16 at 13:56
  • I have updated it to show $conn details and more – Garret Kaye Mar 14 '16 at 14:00
  • @jimmy: This shows how to determine the error from the database query: http://php.net/manual/en/mysqli.error.php Structurally it's very similar to how you currently check for errors in the connection. – David Mar 14 '16 at 14:02
  • `$userUsername = $_POST['string'];` that probably failed you and you should be posting your HTML form with the question. – Funk Forty Niner Mar 14 '16 at 14:04
  • Btw, you never chose a database and is most likely the reason why your code is failing, if not the major contributor. http://php.net/manual/en/function.mysqli-connect.php You only used 3 parameters when you need 4, the 4th being the database. – Funk Forty Niner Mar 14 '16 at 14:04

1 Answers1

2

Since you haven't responded to my comments (some 20+ minutes prior to my submitting this), I am posting the following answer.

You didn't choose a database here:

$servername = "localhost";
$username = "username";
$password = "password";

$conn = new mysqli($servername, $username, $password);

so you need to add the database parameter:

$servername = "localhost";
$username = "username";
$password = "password";
$database = "your_DB";

$conn = new mysqli($servername, $username, $password, $database);

Read the manual on connecting with the MySQLi API:

Check for errors on the query also:

Plus, make sure the POST array does contain a value. Something in regards to the HTML form you didn't post.

The form's element must contain a matching name attribute:

name="string" and there must be a POST method for <form>.

  • method="post".

FYI: <form> is the equivalent to <form method="get"> as it defaults to a GET method if POST isn't implied.

Check for errors with error reporting also:

Although, if your form does not have a POST method as I already mentioned, you won't get errors for it, strangely enough.


Sidenote about $userUsername.

That is ambiguous. If you are trying to run a query against that variable, you would need to use a WHERE clause.

I.e.: WHERE column = '$userUsername'

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141