【问题标题】:while loop not working for more than one variable php?while 循环不适用于多个变量 php?
【发布时间】:2016-08-15 17:42:18
【问题描述】:

我正在尝试创建一个 while 循环,该循环从 Mysql 数据库中检索所有相关数据,但它不适用于多个变量,我认为问题在于 while 循环,因为我已经回显了 sql 语句和它检索到变量的值吧,代码是:

$wherein = implode(',', $_SESSION['cart']);
$sql = "select ID, Name, Price from lamps WHERE ID = '$wherein'";
$result = mysqli_query($conn, $sql);

echo "<table style='width:100%' border='1' >";
echo "<tr>";
echo "<th> Product Name</th>";
echo "<th>Product Price </th>" ;
echo "<th>Quantity </th>" ;
echo "</tr>";

while ( $row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    echo "<tr>";
    echo "<td>" . $row['Name'] . "</td>";
    echo "<td> $". $row['Price'] . "</td>" ;
    echo "<td> <select>
        <option value= '1'>1</option> 
        <option value= '2'>2</option> 
        <option value= '3'>3</option> 
    </select>
    </td>";

    echo "</tr>";
}

echo "</table>";

我对代码进行了很多尝试,但问题仍然存在,非常感谢您的帮助,谢谢。

【问题讨论】:

  • 你说的它与一个变量一起工作是什么意思?顺便说一句,在 $row['price'] 之前有一个 $
  • 好吧,它只工作 1 次,因为您的查询只得到 1 行,我们假设数据库有不重复的单个 id,如果您的查询是:给我一些来自 id = single_id 的字段,那么结果将是 1 行,以防它存在...
  • @PavulZavala 我回应了查询,它有多个变量
  • @bub 我的意思是,只要查询中有一个变量,它就会从数据库中检索信息,但是当查询中有多个变量时,它不会检索任何信息
  • @hossam,我假设您的意思是 select ID, Name, Price from lamps WHERE ID = '$wherein' 检索数据,例如 select ID, Name, Price from lamps WHERE ID = '$wherein' and Price = '$something' 没有?

标签: php mysql while-loop session-variables


【解决方案1】:

&lt;/table&gt; 语句位于 while 循环内。因此,一旦表格在第一次循环后关闭,其余数据可能不会显示在浏览器上(它仍然会在您的 html 源代码中)。尝试将&lt;/table&gt; 语句移到最后一行。

【讨论】:

    【解决方案2】:

    假设通过内爆变量您将拥有多个 ID,那么您应该在 sql 中使用 IN 语句。

    $wherein = implode("','", $_SESSION['cart']);
    $sql = "select ID, Name, Price from lamps WHERE ID IN ( '$wherein' )";
    

    渲染的输出可以稍微简化,循环后表格应该是关闭的!不知道$wherein 的内容很难回答,但我认为IN 语句似乎更适合查询中使用的逗号分隔值的性质,而不是直接等于=

    $wherein = implode("','", $_SESSION['cart']);
    $sql = "select ID, Name, Price from lamps WHERE ID IN ( '$wherein' )";
    $result= mysqli_query( $conn, $sql );
    
    echo "
        <table style='width:100%' border='1' >
            <tr>
                <th> Product Name</th>
                <th>Product Price </th>
                <th>Quantity </th>
            </tr>";
    
    while ( $row = mysqli_fetch_object( $result ) ){
        echo "
            <tr>
                <td>{$row->Name}</td>
                <td>${$row->Price}</td>
                <td>
                    <select>
                        <option value= '1'>1
                        <option value= '2'>2
                        <option value= '3'>3
                    </select>           
                </td>
            </tr>";
    }
    echo "
        </table>";
    
    
    /* Example of using an array of IDs and imploding to generate where conditions for IN clause */
    
    $cart=array('BGL1','BJL');
    /* session - cart */
    
    $wherein=implode( "','", $cart );
    /* correctly add quotes around each string */
    
    $sql="select ID, Name, Price from lamps WHERE ID IN ('$wherein');";
    /* use the new $wherein string within quotes */
    echo $sql;
    
    >> select ID, Name, Price from lamps WHERE ID IN ('BGL1','BJL');
    

    【讨论】:

    • 我在查询中尝试的第一件事就是这个确切的陈述,但这个问题仍然存在
    • 变量设置后最终的sql是什么样的?即:你能在执行时显示sql吗
    • 对多个变量执行时,只显示列头,当只有一个变量时,显示该变量的所有信息;产品名称和价格
    • 你能添加最终呈现的 sql 语句吗?还是$_SESSION['cart']的内容?
    猜你喜欢
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 2013-04-05
    • 2021-06-07
    • 2010-12-20
    相关资源
    最近更新 更多