简体   繁体   中英

Update multiple records mysql

Hi Guys my code is below.

What I'm trying to do is to update all of the value in mysql that satisfy a specific a specific condition. Now when I just type in the mysql query:

UPDATE `$DBNAME`.`video` SET `Secret_key` = '0'

The query works fine but I can't seem to write php code that will do this. Any help is welcomed

    <?php
    // Part 1 (works fine)
    include("/home3/kintest2/public_html/include/config.local.php");
    $connect= mysql_connect ($DBHOST,$DBUSER,$DBPASSWORD);
    $select= mysql_select_db($DBNAME, $connect);
    // End of Part 1

    // Part 2 (works fine)
    $test2= "SELECT * FROM `video`";
    $results= mysql_query($test2, $connect);
    $num_rows = mysql_num_rows($results);
    // End of part 2

    // Part 3 ( Not too sure about this part)
    for ($num_rows= $count; $count >= 0; $count--)
    {
    mysql_query("UPDATE `$DBNAME`.`video` SET `Secret_key` = '0'",$connect);
    }
    // End of part 3 ( Not too sure about this part)


    ?>

For starters I'd take the query out of the loop (and just delete the loop). If it still doesn't work, try hard coding the database name. If that doesn't work, make sure $connect is what you think it is. Look up mysql_query() and make sure you're using it right.

If you try everything above, you'll either find the problem or at least figure out what the problem is not.

<?php
        // Part 1 (works fine)
        include("/home3/kintest2/public_html/include/config.local.php");
        $connect= mysql_connect ($DBHOST,$DBUSER,$DBPASSWORD);
        $select= mysql_select_db($DBNAME, $connect);
        // End of Part 1

        //no need of part 2 to update records in table
        //part 3 loop removed
        mysql_query("UPDATE `$DBNAME`.`video` SET `Secret_key` = '0' ",$connect);


?>

Try this. May it will solve ur problem.

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