获取行的总和,其中username是当前用户的名称

时间:2016-08-21 20:37:11

标签: php mysql

此代码只显示一个空白网页。它有什么问题吗?它应该显示登录用户的总点数。

<?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 个答案:

答案 0 :(得分:0)

  1. 您应该像这样启用error_reporting

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

  2. transaction是mysql中的关键字。所以请使用返回tick(`)。

  3. 您可以使用以下方法来避免使用直接替换值,而不是使用直接替换值。

  4.   

    使用MySQLi(用于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
    }
    

    请参阅How can I prevent SQL-injection in PHP?