Execute mySQL UPDATE command

I have the following code, but it doesn’t work. I think it might be because the method was removed? Not sure what the new way to do it is though. I’m on wordpress.

<php?
mysql_query ("UPDATE $wpdb->users 
SET access_key = $newAccessKey
WHERE ID = $currentUserID");
?>

That don’t work.

Read More

users is the table nome.

Advice??

This script is to be run on a page on php.

Related posts

Leave a Reply

3 comments

  1. First you start with wrong syntax to start PHP script ‘

    Now you should check the type of access_key if it is integer than you wrote right and if it is varchar or text then you should write with ”. For this your query is below.

    mysql_query ("UPDATE $wpdb->users 
    SET access_key = '$newAccessKey'
    WHERE ID = $currentUserID");
    

    I hope you will get solution.

  2. use single quote for string vars and be sure for sanitize $wpdb->users use concat

    mysql_query ("UPDATE " . $wpdb->users  .
      " SET access_key = '$newAccessKey'
       WHERE ID = $currentUserID");
    
  3. The reason why it doesn’t work for you can be a number of things. You can find it out by enabling error reporting (ini_set('display_errors', true); error_level(E_ALL);) and checking for mysql errors (echo mysql_error();).

    Second, if it wasn’t a typo when you were writing the question:

    <php? doesn’t start PHP code. It should be <?php, the way you wrote it it is ignored both by the server and the browser because it is an illegal tag and the code inbetween isn’t executed at all.

    Apart from that, $wpdb brings its own update() function that you can use:

    $wpdb->update( 
        $wpdb->users, 
        array( 'access_key' => $newAccessKey ), 
        array( 'ID' => $currentUserID ),
        array( 
            '%s'    // if $newAccessKey is a string
            // '%d' // if $newAccessKey is an integer
        ), 
        array( '%d' ) 
    );
    

    That way you don’t have to worry about the database connection or deprecated functions.