-1

This code just displays a blank webpage. Is there anything wrong with it? It is supposed to show the total points the logged in user has.

<?php

session_start();

$servername = "localhost";
$username = "root";
$password = "randompassword";
$dbname = "transactions";

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

$query = "SELECT sum(points) AS points FROM transaction WHERE username =    '".mysqli_real_escape_string($conn,$_SESSION['username'])."'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);

print($row)
?>

1 Answers1

0
  1. You should enable the error_reporting like this

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

  2. transaction is a keyword in mysql. So use back tick ( ` ).

  3. Instead of using direct substitution values, you could use below methods to avoid sql injection.

Using MySQLi (for MySQL):

$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?');
$stmt->bind_param('s', $name);

$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // do something with $row
}

Please refer How can I prevent SQL-injection in PHP?

Community
  • 1
  • 1
Tamil
  • 1,193
  • 9
  • 24