简体   繁体   English

如何调试查询?

[英]How do i debug my query?

I am trying to insert some data to the mysql database 我正在尝试向mysql数据库中插入一些数据

$db = new DataBase($config);

// connect to the database RETURNS true if success false if fails
$conn = $db->connect();

// check whether the connection is successfull or not...
if ($db->isConnected())
     {

        //prepare the query
        $query = 'INSERT INTO scoreboard (score) VALUES(:score) WHERE username=:username';
        $bindings =  array(
                         'score'   =>  $score,
                        'username' => ($_SESSION['username'])

                         );


        // call the query function from db class and retrieve the results as an array of rows.
        $results = $db->setData($conn,$query,$bindings);
        if ($results)
            echo "Your Score is Updated!";
        else

            echo "Your Score is Not Updated!";


        }

Heres what setData() does : 这是setData()的作用:

function setData($conn,$query,$bindings)
{
    try {

            // prepare the query
            $stmt = $conn->prepare($query);

            //binde the query with the data .here the data is $bindings
            $stmt->execute($bindings);

            return ( $stmt->rowCount() > 0 )
                // return the result if the query is success else return false.
                ? $stmt
                : false;
        } 

        catch(Exception $e) {
            // return error if something goes wrong
            return false;
        }


}

Everytime I run this script I get "Your Score is Not Updated" as output. 每次运行此脚本时,都会得到“您的分数未更新”作为输出。

Where I am going wrong? 我要去哪里错了?

Is that the $_SESSION['username'] causing trouble? 是$ _SESSION ['username']引起麻烦了吗?

Any help with proper explanation will be highly appreciated! 任何有适当解释的帮助将不胜感激!

you are updating or inserting? 您要更新还是插入? try your query manually with static data on phpmyadmin, then make sure your session is set & return true if successfully data inserted 在phpmyadmin上使用静态数据手动尝试查询,然后确保设置了会话并成功插入数据,则返回true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM