1

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

// 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 尝试了第二批代码,但我也没有任何运气。

4

5 回答 5

1

也许更聪明的方法?

$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) 之后的值。

同样,找出是否在 wine* s * 中回显 s:

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

如果 $wines == 1 我们不会在 wine* s * 中回显 s:

于 2013-10-25T14:03:27.657 回答
0
elseif ($wines = 1) {

当您将值 1 分配给 $wines 时,将始终为真,请尝试:

elseif ($wines == 1) {
于 2013-10-25T13:58:03.100 回答
0

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

// 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>.';
}
于 2013-10-25T13:58:11.130 回答
0

使用“==”代替“=”

 if ($wines == 1) {

作为,==是比较运算符,并且单个=只会将值分配1$wines.

于 2013-10-25T13:58:56.373 回答
0

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

于 2013-10-25T14:04:13.117 回答