简体   繁体   English

在不同的行中显示数据库中的一列 SQL PHP

[英]Display a column from database in different row SQL PHP

picture explanation I am trying to display the option_value(3rd colum) value into my code.图片说明我试图在我的代码中显示 option_value(3rd colum) 值。 I just tried testing mysqli_fetch_array method but it showed error undefined offset 1 and 2. Can you suggest any method to display the value separately into the page?我刚刚尝试测试 mysqli_fetch_array 方法,但它显示错误未定义偏移量 1 和 2。您能建议任何方法将值单独显示到页面中吗?

  <?php
$connection = mysqli_connect("localhost", "root","","adminpanel");

$query = "SELECT option_value FROM settings";
$query_run = mysqli_query($connection,$query);
if(mysqli_num_rows($query_run)>0)
{
    $row = mysqli_fetch_array($query_run)
    ?>
    <form action="code.php" method="POST">
     <div class="form-group">
        <label>Fine rate :</label>
        <input type="text" name="fine_rate" value="<?php  echo $row[0]; ?>" class="form-control">
    </div> 

    <div class="form-group">
        <label>Email template for issued books:</label>
        <textarea name="issue_template" class="form-control"><?php echo $row[1]; ?></textarea>
    </div>
    <div class="form-group">
        <label>Email template for return books:</label>
        <textarea name="return_template" class="form-control"><?php  echo $row[2]; ?></textarea>
    </div>  
    </form>
<?php 
} 
?>

When you do当你做

$row = mysqli_fetch_array($query_run)

It creates an array for your result.它为您的结果创建一个数组。 Considering you are only selecting the option_value that array has only one element.考虑到您只选择数组只有一个元素的 option_value。 Thus you can only access row[0].因此,您只能访问行 [0]。 As the wording implies, the $row variable only contains one row so you will have to fetch the next row using fetch array again.顾名思义,$row 变量只包含一行,因此您必须再次使用 fetch 数组来获取下一行。 Something like this像这样的东西

 <?php
$connection = mysqli_connect("localhost", "root","","adminpanel");

$query = "SELECT option_value FROM settings";
$query_run = mysqli_query($connection,$query);
if(mysqli_num_rows($query_run)>0)
{
    $row = mysqli_fetch_array($query_run)
    ?>
    <form action="code.php" method="POST">
     <div class="form-group">
        <label>Fine rate :</label>
        <input type="text" name="fine_rate" value="<?php  echo $row[0]; ?>" class="form-control">
    </div> 
<?php
   $row = mysqli_fetch_array($query_run)
    ?>
    <div class="form-group">
        <label>Email template for issued books:</label>
        <textarea name="issue_template" class="form-control"><?php echo $row[0]; ?></textarea>
    </div>
<?php
   $row = mysqli_fetch_array($query_run)
    ?>
    <div class="form-group">
        <label>Email template for return books:</label>
        <textarea name="return_template" class="form-control"><?php  echo $row[0]; ?></textarea>
    </div>  
    </form>
<?php 
} 
?>

This is not a good aproach as you can't be shure that the rows are tetched in that order every time.这不是一个好的方法,因为您不能确定每次都按该顺序显示行。 You can get all columns from that table and then sort the data for your needs:您可以从该表中获取所有列,然后根据需要对数据进行排序:

$rows;
$finerate;
$email_temp_issue;
$email_temp_return;

$query = "SELECT * FROM settings";
$query_run = mysqli_query($connection,$query);
if(mysqli_num_rows($query_run)>0){
    $rows = mysqli_fetch_array($query_run);
}
foreach ($rows as $row){
    switch ($row['option_name']) {
    case "finerate":
       $finerate = $row['option_value'];
       break;
    case "email_temp_issue":
       $email_temp_issue= $row['option_value'];        
       break;
    case "email_temp_return":
        $email_temp_return= $row['option_value'];
        break;   
   }
}
    ?>
    <form action="code.php" method="POST">
     <div class="form-group">
        <label>Fine rate :</label>
        <input type="text" name="fine_rate" value="<?php  echo $finerate ; ?>" class="form-control">
    </div> 
<?php
   $row = mysqli_fetch_array($query_run)
    ?>
    <div class="form-group">
        <label>Email template for issued books:</label>
        <textarea name="issue_template" class="form-control"><?php echo email_temp_issue; ?></textarea>
    </div>
<?php
   $row = mysqli_fetch_array($query_run)
    ?>
    <div class="form-group">
        <label>Email template for return books:</label>
        <textarea name="return_template" class="form-control"><?php  echo $email_temp_return; ?></textarea>
    </div>  
    </form>
<?php 

This way it dosen't matter the id in the table or even if you have more records in that table这样,表中的 id 或即使该表中有更多记录也无关紧要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM