简体   繁体   中英

Update the selected random row PHP

I'm trying to update the selected random row in database here is my php code

$offset_result = mysqli_query($conn, "SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `tbl_combi` WHERE `clear` = 0 ");
$offset_row = mysqli_fetch_object( $offset_result );
$offset = $offset_row->offset;
$result = mysqli_query($conn, "SELECT * FROM `tbl_combi` LIMIT $offset, 1");    
$result_fetch = mysqli_fetch_array($result);

echo $result_fetch[1];

You never perform any update. Here is something you could try (it might need some tuning to fit your need) :

$offset_result = mysqli_query($conn, "SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `tbl_combi` WHERE `clear` = 0 ");
$offset_row = mysqli_fetch_object( $offset_result );
$offset = $offset_row->offset;

$updateSql = 'UPDATE tbl_combi SET my_field="my_value" WHERE offset=' . $offset;
mysqli_query($conn, $updateSql);

$result = mysqli_query($conn, "SELECT * FROM `tbl_combi` LIMIT $offset, 1");    
$result_fetch = mysqli_fetch_array($result);

echo $result_fetch[1];

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