其他分享
首页 > 其他分享> > 开关功能输出默认值而不是所需值

开关功能输出默认值而不是所需值

作者:互联网

我正在尝试使用以下代码做一个简单的开关功能.

$query = "SELECT * FROM entries where Venue='Condamine' and Year='2018' and 
Event='$5,000 Novice'  and herd='2' and scratched IN('n','l')  ORDER BY Draw 
ASC";
// Execute the query
$result = mysqli_query($con ,$query);
if (!$result){
    die ("Could not query the database: <br />". mysqli_error());
}
// Change herds
function getherd($catch) { 
    switch($catch) 
    { 
        case '2': 
            return 'Herd Change'; 
        break; 
        default: 
            return 'Damn!'; 
        break; 
    } 
} 
$catch = $row["herd"];
echo "<tr>";
echo "<td bgcolor=#FFFFFF><strong> ". getherd($catch) ." </strong></td>";
echo "<td bgcolor=#FFFFFF>&nbsp;</td>";
echo "</tr>";
?>

我的结果是打印出默认值“该死!”.而不是所需的值“ Herd Change”,这是我做错了.如果该行的牧群的值= 2,我想打印出“ Herd Change”字样.

解决方法:

它很可能是类型转换问题

尝试

$catch = string($row [“ herd”]);并确保$catch是2

标签:switch-statement,mysql,php
来源: https://codeday.me/bug/20191025/1929083.html