简体   繁体   中英

How do i debug my query?

I am trying to insert some data to the mysql database

$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 :

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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