Home > front end >  Echo row with specific value (php, sql)
Echo row with specific value (php, sql)

Time:10-22

i have this table

enter image description here

i want echo only the row with red rectangle by condition color1. Basically what i want is if color1=yellow then print row.

My code ist like this, i dont know what i doing wrong, can you help me?

 $id = $_GET['color1'];
 $stmt = $connect->prepare('SELECT * FROM quicknews WHERE id = :color1');
 $stmt->execute(array(':color1' => $id));  
 $row = $stmt->fetch();    
 $id = $row['color1'];
 $news1 = $row['news1'];
 $url1 = $row['url1'];

    
if ($row['color1'] == yellow)

   {
    echo "<div id='contentA' style='text-align:center; vertical-align:top; margin-bottom:0px;display:none'><a href='$url1' style='text-decoration:none; text-align:center'>$news1</a></div>";
   echo "<div id='displayArea' style='text-align:center; vertical-align:top; margin-bottom:0px'></div>";
   }

CodePudding user response:

Text strings need to be inside double or single quotes to be considered a string, since yours is not wrapped inside quotes php it trying to find an undefined constant called yellow.

$id = $_GET['color1'];
$stmt = $connect->prepare('SELECT * FROM quicknews WHERE id = :color1');
$stmt->execute(array(':color1' => $id));  
$row = $stmt->fetch();    
$color = $row['color1'];
$news1 = $row['news1'];
$url1 = $row['url1'];

if ($color == "yellow") {
    echo "<div id='contentA' style='text-align:center; vertical-align:top; margin bottom:0px;display:none'><a href='$url1' style='text-decoration:none; text-align:center'>$news1</a></div>";
    echo "<div id='displayArea' style='text-align:center; vertical-align:top; margin-bottom:0px'></div>";
} elseif ($color == "red") {
  // red row stuff
} elseif ($color == "blue") {
  // blue row stuff
} elseif ($color == "green") {
  // green row stuff
}

CodePudding user response:

Please try this it should solve your problem:

<?php
$color = $_GET['color1'];
$query = "SELECT * FROM quicknews WHERE color1= ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $color);
$stmt->execute();
$row = $stmt->get_result();
while($data= $row->fetch_assoc()){
if($data['color1'] == "yellow"){
    //write your code
  }// write else if here if you need to add another condition
}
?>

best regards

  • Related