dongyong5912 2014-05-01 20:26
浏览 100
已采纳

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

The aim

  1. The user enters inserts a number into a text field and hits calculate

  2. That number is then subtracted by 250

  3. Depending on what the $result is, a particular image is shown.

The problem

When the page is run I get the following error message;

Catchable fatal error: Object of class mysqli_result could not be converted to string in /home/cs12jcw/public_html/n-power/includes/calculator.php on line 95

Line 95 is;

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

The full code

<?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='' />";

}
?>

Please let me know if you need anything else. Apologies if I've missed anything.

  • 写回答

2条回答 默认 最新

  • dos3018 2014-05-01 20:36
    关注

    You are setting your $image variable to a mysql result object. You still need to fetch the data from the result. Try the below

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

    Also, you could modify your switch statement to the below, so you only have one spot issuing the query

    $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='' />";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据