mysqli_result类的对象无法转换为字符串5

时间:2014-05-01 20:26:31

标签: php

目标

  1. 用户输入在文本字段中插入一个数字并点击计算

  2. 然后将该数字减去250

  3. 根据$ result的结果,会显示特定图像。

  4. 问题

    当页面运行时,我收到以下错误消息;

    导致致命错误:无法将类mysqli_result的对象转换为第95行/home/cs12jcw/public_html/n-power/includes/calculator.php中的字符串

    第95行是;

    echo "<img src='$image' alt='' />";
    

    完整代码

    <?php
    $valuea = (isset($_POST['valuea']) && is_numeric($_POST['valuea'])) ? $_POST['valuea'] : 0;
    $valueb = 250;
    
    $answer = $valuea - $valueb;
    
    ?>
    <form method='post' action='calculator.php'>
    <table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
    <tr class="calcheading">
        <td colspan="2"><strong>Work out how much you could be     saving</strong></td>
    </tr>
    <tr class="calcrow">
        <td>How much do you spend a year?</td>
        <td align="center"><input     type='text' name='valuea' value="$valuea"/></td>
    </tr>
    <tr class="calcrow">
        <td>Minus the average price of an n-power student tarrif* Leave     Blank:</td>
        <td align="center"><input type='text' name='valueb' value="$valueb"/></td>
    </tr>
    <tr class="submit">
        <td colspan="2"><input type='submit' value='Calculate'/></td>
    </tr>
    <tr class="calcrow">
        <td><i>You could be saving:</td>
        <td align="center"><input type="text" value="<?php echo round($answer)?>"></td></i>
    </tr>
    </table>
    </form>
    
    <?php
    if($db_server){
    
    switch( $answer ){
        case $answer > 0 and $answer < 150 : $image = mysqli_query($db_server, "SELECT URL     FROM images WHERE imagename = 'image1'");
        break;
    
        case $answer < 250 : $image = mysqli_query($db_server, "SELECT URL FROM images WHERE imagename = 'image2'");
        break;
    
        case $answer < 350 : $image = mysqli_query($db_server, "SELECT URL FROM images WHERE imagename = 'image3'");
        break;
    
        case $answer < 450 : $image = mysqli_query($db_server, "SELECT URL FROM images WHERE imagename = 'image4'");
        break;
    
        case $answer < 550 : $image = mysqli_query($db_server, "SELECT URL FROM images WHERE imagename = 'image5'");
        break;
    }
    
    echo "<img src='$image' alt='' />";
    
    }
    ?>
    

    如果您还有其他需要,请告诉我。如果我错过任何事情,请道歉。

2 个答案:

答案 0 :(得分:1)

您忘了取结果:

$row = mysqli_fetch_array($image);
echo "<img src='$row[URL]' alt='' />";

答案 1 :(得分:1)

您正在将$ image变量设置为mysql结果对象。您仍然需要从结果中获取数据。试试下面的

$result = mysqli_query($db_server, "SELECT URL FROM images WHERE imagename = 'image1'");
$obj = mysqli_fetch_object($result);
$image = $obj->URL;

此外,您可以将switch语句修改为以下内容,因此您只有一个点发出查询

$imagename = '';
switch( $answer ){
    case $answer > 0 and $answer < 150 : $imagename = 'image1';
    break;

    case $answer < 250 : $imagename = 'image2';
    break;

    case $answer < 350 : $imagename = 'image3';
    break;

    case $answer < 450 : $imagename = 'image4';
    break;

    case $answer < 550 : $imagename = 'image5';
    break;
}

if ($imagename) {
    $result = mysqli_query($db_server, "SELECT URL FROM images WHERE imagename = '$imagename'");
    $obj = mysqli_fetch_object($result);
    $image = $obj->URL;

    echo "<img src='$image' alt='' />";
}