【问题标题】:mysql_result($result, $i, 'id') equivalent mysqli [duplicate]mysql_result($result, $i, 'id') 等效 mysqli [重复]
【发布时间】:2016-07-07 11:28:22
【问题描述】:

我想将我的代码从mysql 转换为mysqli。 但是,我这里有个问题。

mysql 代码是

 for ($i = $start; $i < $end; $i++) {
                    // make sure that PHP doesn't try to show results that don't exist
                    if ($i == $total_results) {
                        break;
                    }





                    // echo out the contents of each row into a table

                      $bookid = mysql_result($result, $i, 'id');
                    $cover = mysql_result($result, $i, 'cover');

                    echo "
                        <div></div>
                <li>
                    <div id='divborder'>
                        <div id='header'>
                            <a class='caption'  href='page.php?id=$bookid '><img src='$cover' alt='' class='headerBg' /></a>
                        </div>
                    </div>
        </li> ";

                }       
echo "</ul></div>";

我想把它转换成类似的东西

for ($i = $start; $i < $end; $i++) {
    // make sure that PHP doesn't try to show results that don't exist
    if ($i == $total_results) {
        break;
    }
    $i;
                              $book_row = mysqli_fetch_assoc($result);
                              $book_id = $book_row["id"];
                              $book_cover = $book_row["cover"];
                              $book_title = $book_row["title"];
                              $book_auther1 = $book_row["auther1"];                                  
                              $book_auther2 = $book_row["auther2"];




                    echo "
                        <div></div>
                <li>
                    <div id='divborder'>
                        <div id='header'>
                            <a class='caption caption1'data-title='$data_title'  href='book/$book_id'><img src='$book_cover' alt='' class='headerBg' /></a>
                        </div>
                    </div>
        </li> ";

                }       
echo "</ul></div>";

但是mysql代码中变量$i的问题我不知道我会把它放在mysqli的哪个地方

【问题讨论】:

    标签: php mysql mysqli


    【解决方案1】:
    $query = "select * from books";
    
    // run the query
    $result = mysqli_query($query);
    
    // fetch all the rows
    $arrayofrows = mysqli_fetch_all($result, MYSQLI_ASSOC);
    
    // then you can use $i from your for loop
    // var_dump($arrayofrows[$i]);
    

    【讨论】:

    • 感谢您的回答,但我想要程序样式的代码和 $i 变量而不是 id 我用它来分页一些结果,我将计数一个结果
    • 也许你正在寻找这样的东西:stackoverflow.com/a/21921936/5756331我根据上面的链接更新了我的答案。
    【解决方案2】:

    或者像这样(来自您的原始代码):

    <?php
    $conn = mysqli_connect('host','username','password','database');
    for ($i = $start; $i < $end; $i++) {
            // make sure that PHP doesn't try to show results that don't exist
            if ($i == $total_results) {
            break;
            }
    
    
            $query = "select * from tablename where id ='$i'";
            $result = $conn->query($query);
            $data = mysqli_fetch_all($result,MYSQLI_ASSOC);
            $book_id = $data["id"];
            $book_cover = $data["cover"];
            $book_title = $data["title"];
            $book_auther1 = $data["auther1"];                                  
            $book_auther2 = $data["auther2"];   
    
            echo "
            <div></div>
            <li>
            <div id='divborder'>
            <div id='header'>
            <a class='caption caption1'data-title='$data_title'  href='book/$book_id'><img src='$book_cover' alt='' class='headerBg' /></a>
            </div>
            </div>
            </li> ";
    }   
    ?>
    

    更新:

    <?php
    $con = mysqli_connect('host','username','password','database');
    $per_page=5;
    
    if (isset($_GET['page'])) {
    $page = $_GET['page'];
    } else {
    $page=1;
    }
    
    // Page will start from 0 and Multiple by Per Page
    $start_from = ($page-1) * $per_page;
    
    //Selecting the data from table but with limit
    $query = 'SELECT * FROM books LIMIT $start_from, $per_page';
    $result = mysqli_query ($con, $query);
    $total_records = mysqli_num_rows($result);
    $total_pages = ceil($total_records / $per_page);
    
    while ($row = mysqli_fetch_assoc($result)) {
    $book_id = $row["id"];
    $book_cover = $row["cover"];
    $book_title = $row["title"];
    $book_auther1 = $row["auther1"];                                  
    $book_auther2 = $row["auther2"];    
    
            echo "
            <div></div>
            <li>
            <div id='divborder'>
            <div id='header'>
            <a class='caption caption1'data-title='$data_title'  href='book/$book_id'><img src='$book_cover' alt='' class='headerBg' /></a>
            </div>
            </div>
            </li> ";
    
    }
    
    //pagination goes here:
    echo "<center><a href='".htmlspecialchars($_SERVER["PHP_SELF"])."?page=1'>First Page</a>";
    for ($i=1; $i<=$total_pages; $i++) {
    echo "<a href='".htmlspecialchars($_SERVER["PHP_SELF"])."?page=".$i."'>".$i."</a>";
    }
    echo "<center><a href='".htmlspecialchars($_SERVER["PHP_SELF"])."?page=$total_pages'>Last Page</a>";
    ?>
    

    【讨论】:

    • 感谢您的回答,但我想要程序样式的代码和 $i 变量而不是 id 我用它来分页一些结果,我将计数一个结果
    • @AhmedAbuelmagd :更新了处理分页的答案。
    猜你喜欢
    • 2014-03-23
    • 2016-08-12
    • 2017-03-27
    • 2014-04-03
    • 2012-08-22
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多