PHP 7中的数据库连接

时间:2018-05-25 12:48:18

标签: php mysql php-7

但是我不明白我做错了什么以及为什么它不起作用?

似乎它与DB连接,但它不会更新DB表。

我的PHP代码

<?php
        $host = 'localhost';
        $db_name = 'db_name';
        $db_user = 'user'; 
        $db_password = 'password';

        $con = mysqli_connect($host, $db_user, $db_password, $db_name);
            if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}
        function _VoteReward($custom)
             {
                $sql = "UPDATE `users` SET `gold` = `gold` + 50000 WHERE `id` = '".$custom."' ";

                mysqli_query($con, $sql);
            }


            $custom = $_POST["custom"];
            $key = $_POST["key"];
            $result = false;

                if (($custom > 0) && ($key == 'key'))
            { 
                $result = true;
                _VoteReward($custom);

                }
    mysqli_close($con);

 ?> 

1 个答案:

答案 0 :(得分:1)

上面的代码确实产生了与数据库的连接。但是,需要检查生成的连接是否有错误。通常通过以下方式:

if(!$con)
{ // creation of the connection object failed
    die("connection object not created: ".mysqli_error($con));
}

if (mysqli_connect_errno()) 
{ // creation of the connection object has some other error
    die("Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error());
}