0

I am trying to output the results of an SQL query as a table on a page on my website. I have found a few solutions online but I can't get any of them to work properly. Right now I copied and pasted a bit of code to just output the first two columns but I can't figure out how to get every column in a table. I am new to PHP and web development in general so any help would be appreciated.

My PHP:

 <?php
SESSION_START() ;

$servername = "localhost";
$username = "MY USERNAME";
$password = "MY PASSSWORD";
$dbname = "MY DATABASE NAME";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

//$_session['userid'] = $userlogged;

$sql = "SELECT * FROM `climbs` WHERE `userlogged` = '" . $_SESSION['userid']     . "'";
$result = mysqli_query($conn,$sql);


if ($result->num_rows > 0) {
     echo "<table><tr><th>ID</th><th>Name</th></tr>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<tr><td>" . $row["climb-id"]. "</td><td>" .     $row["climbname"]. " " . $row["cragname"]. "</td></tr>";
     }
     echo "</table>";
} else {
     echo "0 results";
}


mysqli_close($conn);
?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • You just have to add the code for each column. Or were you wanting a way to figure out all of the columns automatically? – Jay Blanchard Dec 29 '16 at 21:03
  • You are wide open for SQL injection. Since you're using mysqli, take advantage of [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [bind_param](http://php.net/manual/en/mysqli-stmt.bind-param.php). – aynber Dec 29 '16 at 21:03
  • As for the columns, just explicitly add the code for each column. You've already started in your echo statement, just continue on with what you want to echo. – aynber Dec 29 '16 at 21:04
  • Possible duplicate of [Show values from a mySQL database table inside a html table in a page](http://stackoverflow.com/questions/17902483/show-values-from-a-mysql-database-table-inside-a-html-table-in-a-page) – Shank Dec 29 '16 at 21:04
  • @JayBlanchard When I run that there are no lines between each column, that is what I would like to happen but don't know how to do. – Jack McKechnie Dec 29 '16 at 21:07
  • You want lines? Add CSS to style the table. – Jay Blanchard Dec 29 '16 at 21:08
  • @aynber I know I am, this is for a school project and does not have any valuable information. I have a tight time schedule to get it done in so I have not looked into doing anything yet but if I have time at the end I'll sort it out. If not it gives me something to write about in the evaluation I suppose :) – Jack McKechnie Dec 29 '16 at 21:10
  • @JayBlanchard Oh my gosh how did I not think of that! Thanks very much, think I know where to go from here – Jack McKechnie Dec 29 '16 at 21:12

1 Answers1

0

check with var_dump :

some like that:

$result = mysqli_query($conn,$sql);

var_dump($result);

if ($result->num_rows > 0) {

maybe the query it's wrong.