mysqli类的对象无法转换为字符串
问题描述:
I am getting this error from the following code:
function updateRank($master_list, $event_id)
{
$cnt_ml = count($master_list);
echo "count master list = $cnt_ml<br>";
$b=1;
for ($k=0; $k<$cnt_ml; $k++)
{
echo "master list element 1 - ".$master_list[1]."<br>";
$foo = $master_list[$k];
// Update each team in event_team table
$update = "UPDATE event_team
SET pool_rank = $b
WHERE event_id = $event_id
AND team_id = $foo";
mysqli_query($conn, $update);// or die ('Could not run insert in event_team table');
echo "|".$update."|<br>"; // Leave this line for debugging the sql query.
$b++;
}
}
Specifically "$foo" is throwing the error. The "master_list" array is being passed correctly. When running the code, $cnt_ml returns "5" (which is absolutely correct). The line with:
echo "master list element 1 - ".$master_list[1]."<br>";
Returns "54" (which is correct).
Since the script is successfully able to read the array elements and post them, why is the script throwing the error when I include it in the UPDATE? It is able to see the array data but choking when trying to use the data in the UPDATE.
Thanks in advance!
答
Variable $conn is not declared. Or it's declared globally and you forgot to add
global $conn;
in updateRank function