1

So what i want is to display this > https://gyazo.com/308cad4839d01aa3712da1481de136c8 data in a bootstrap table via the username excluding the id field!

so if i have the script search for all of the rows that are "owned" by TheSocomj this one row should show up. If I add another field with TheSocomj that should show up too! So there would be multiple results!

I would be importing the username via

$_SESSION['username'] 

This is what i have so far but the output is blank in the table

<?php
                $con = new mysqli($servername, $username, $password, $dbname);
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                } 

                $username = $_SESSION['username'];
                $sql = "SELECT year, make, model, price FROM user_cars WHERE username=$username"; 
                $result = $con->query($sql);

                if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . $row['year']. "</td> <td>" . $row['make']. "</td> <td>" . $row['model']. "</td> <td>" .  $row['price']. "</td>";
                echo "</tr>";
                    }
                }                           
                ?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
TheSocomj
  • 33
  • 7
  • 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. You do have quite a few errors here. – Funk Forty Niner Feb 14 '16 at 21:42
  • Ok I did that and fixed a few errors that were there! The one error i couldn't fix was `Notice: Trying to get property of non-object in /home/thesocomj/public_html/login/mycars.php on line 47` – TheSocomj Feb 14 '16 at 21:47
  • Well, there you go. Your query failed and you need to find out why that is. – Funk Forty Niner Feb 14 '16 at 21:48
  • Thanks a lot I finally got it! – TheSocomj Feb 14 '16 at 22:14
  • Ah, great and you're welcome. I was formulating an answer during the time you were testing/debugging and it can be found below. I hope this helps, *cheers* – Funk Forty Niner Feb 14 '16 at 22:15

1 Answers1

1

Firstly, you've assigned your connection with $con as per $con = new mysqli($servername, $username, $password, $dbname);.

But then using $conn with 2x "n"'s in the following:

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

So you'll never get the real error should your connection fail. Those all need to match and if it's your real code, error reporting would have told you about an undefined variable.

It is also unknown as to where those four variables have been assigned. If you haven't, then visit the following on PHP.net to see how it is used:

Now, since you are using sessions and is unknown whether or not it has been started, session_start(); must reside all files using sessions.

This assuming that the session array has in fact been populated.

You then are not quoting what more than likely seems to be a string in the WHERE clause for the username:

WHERE username=$username";

which should read as:

WHERE username='$username'";

and error checking against your query would have thrown you something about it.

One thing you need to make sure and this is also unknown, is if you have any output before your PHP.

For instance:

<!DOCTYPE html>
...
<body>...</body>
</html>
<?php 
session_start();
...
?>

Will throw you a headers sent notice. There should not be any output before the session and should be part of the first lines of your code.

I.e.: and there should not be any spaces before your opening PHP tag.

<?php 
session_start();
...
?>
<!DOCTYPE html>
...
<body>...</body>
</html>

If you do get a notice about it, visit the following on Stack:

References:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

as well as or die(mysqli_error($con)) to mysqli_query().

  • That is the best I can offer you, in order to help you find out why your query failed.
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141