Display a column from database in different row SQL PHP

Viewed 85

picture explanation I am trying to display the option_value(3rd colum) value into my code. 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?

  <?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 
} 
?>
2 Answers

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. Thus you can only access row[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. 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

Related