douweng7083 2014-11-12 15:25
浏览 11
已采纳

使用函数查询数据库

I have a page and I would really like some help / advice to create a better way / function to call in information from another table.

At the moment, my code looks like: [I do know this is deprecated SQL and would really like to do some nice SQLi for this.]

<?
$menuid = "100";
$imageid = "50";

// ** Talk to 'imagedirectory' table

mysql_select_db($database_db, $BASEdb);
$query_displayimage = "SELECT * FROM imagedirectory WHERE menuid = ".$menuid." AND imageid = ".$imageid."";
$displayimage = mysql_query($query_displayimage, $BASEdb) or die(mysql_error());
$row_displayimage= mysql_fetch_assoc($displayimage);

?>

<img src="/images/assets/<?php echo $menuid; ?>-<?php echo $imageid; ?>-<?php echo $row_displayimage['urlslug']; ?>.jpg" alt="<?php echo $row_displayimage['alttext']; ?>" />

I figure There really has to be a better way because if there is 10 images on a page, this is pretty intense way of doing it.

  • 写回答

1条回答 默认 最新

  • dongsui5464 2014-11-12 16:01
    关注

    Since you seem to know that mysql_* is deprecated, I am assuming you have read up on, and are using mysqli_* instead.

    You needn't query the database every time. mysqli_query() returns a mysqli_result, which you can iterate over, and read using functions like mysqli_fetch_assoc(). Here is one way of doing it:

    <?php
    
    // store your query in a variable.
    $query_displayimage = "SELECT * FROM imagedirectory";
    
    // query the database.
    $displayimage = mysqli_query($query_displayimage, $BASEdb);
    
    // check for errors.
    $dbErrors = mysqli_error($BASEdb);
    if (count($dbErrors))
    {
        print_r($dbErrors);
        die();
    }
    
    // iterate over the returned resource.
    while ($row_displayimage = mysql_fetch_assoc($displayimage))
    {
        echo '<img src="/images/assets/' . $menuid . '-' . $imageid . '-' . $row_displayimage['urlslug'] . '.jpg" alt="' . $row_displayimage['alttext'] . '" />';
    }
    
    ?>
    

    Hope that helped.


    EDIT:

    You can use this code in a function, too. For example:

    <?php
    
    function printImage($menuid, $imageid)
    {
        $query_displayimage = "SELECT * FROM imagedirectory";
        $displayimage = mysqli_query($query_displayimage, $BASEdb);
        $dbErrors = mysqli_error($BASEdb);
        if (count($dbErrors))
        {
            print_r($dbErrors);
            die();
        }
    
        if ($row_displayimage = mysql_fetch_assoc($displayimage))
        {
            echo '<img src="/images/assets/' . $menuid . '-' . $imageid . '-' . $row_displayimage['urlslug'] . '.jpg" alt="' . $row_displayimage['alttext'] . '" />';
        }
        else    // if there is a problem getting the image
        {
            echo 'Error getting image.';
        }
    }
    
    ?>
    

    and elsewhere in your HTML, you would do something like:

    <div>
        And here is an image!
        <?php printImage(20, 50); ?>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条