尝试计算并返回表中的行数


Trying to count and return a number of rows in a table

我有以下代码来计算数据库中的行数并将结果返回给用户:

// Connect to database and select
$mysqli = mysqli_connect($config['db_hostname'], $config['db_username'], $config['db_password'], $config['db_name']);
$mysqli->set_charset('utf8');
$select = 'SELECT COUNT(*) AS wines FROM  ft_form_4 WHERE feu_id = "'.$feuid.'"';
$response = mysqli_query($mysqli, $select) or die("Error: ".mysqli_error($mysqli));
$result = mysqli_fetch_array($response, MYSQLI_ASSOC);
$wines = (int)$result[wines];
echo $wines;
// Return 0 for no entries
if ($wines = 0) {
echo 'You currently have no wines entered, your current total entry fee is <strong>£0</strong>.';
}
elseif ($wines = 1) {
echo 'You currently have 1 wine entered, your current total entry fee is <strong>£135</strong>.';
}
elseif ($wines > 1) {
$fee = $wines * 135;
echo 'You currently have '.$wines.' wines entered, your current total entry fee is <strong>'.$fee.'</strong>.';
}

当我运行代码时,结果在第一位(我刚刚放入测试)中回显为 3,这是正确的,但它总是显示第二行,说输入了一种葡萄酒,入场费为 135 英镑。它似乎没有将 3 识别为一个数字。

我已经使用mysqli_num_rows尝试了第二批代码,但我也没有运气。

也许是更聪明的方法?

$fee = $wines * 135;
echo 'You currently have '.($wines > 0 ? $wines : 'no').' wine'.($wines != 1 ? 's' : '').' entered, your current total entry fee is <strong>£'.$fee.'</strong>.';

解释:

($wines > 0 ? $wines : 'no')

在括号内,我们所做的是评估第一部分:

$wines > 0

如果为真,则"?"后面的值(if) 将被输出,如果为 false,则输出 ':"(else) 之后的值。

同样,找出是否在葡萄酒*s*中呼应s

($wines != 1 ? 's' : '')

如果 $wines == 1,我们不回显 wine*s* 中的 s

比较是错误的,你应该使用 == 而不是=

elseif ($wines = 1) {

将始终为 true,因为您将值 1 分配给$wines,请尝试:

elseif ($wines == 1) {

您需要使用"=="比较器

// Return 0 for no entries
if ($wines == 0) {
    echo 'You currently have no wines entered, your current total entry fee is <strong>£0</strong>.';
}
elseif ($wines == 1) {
    echo 'You currently have 1 wine entered, your current total entry fee is <strong>£135</strong>.';
}
elseif ($wines > 1) {
    $fee = $wines * 135;
    echo 'You currently have '.$wines[0].' wines entered, your current total entry fee is <strong>'.$fee.'</strong>.';
}

使用"=="而不是"="

 if ($wines == 1) {

因为,==是比较运算符,单个=只会将值1分配给$wines