简体   繁体   中英

Fetch data from database with same column entries?

Have got a database with column as

Username      Password     Chain Name
12345         12345        ASDFG
12345         12345        QWERT
12345         12345        ZXCVB

I am taking Username and Password as input from the user and want to display all the Chain Name associated with this username and password.

Uploading the PHP for same.

<?php  
  include('includes/config.php');

  if(isset($_POST['userid'],$_POST['pid'])){    
    $userid = trim($_POST["userid"]);
    $pid = trim($_POST["pid"]);

    $sql = "SELECT * FROM tbl_store WHERE username = '$userid' ";

    $result = mysqli_query($conn,$sql);
    $rowcount=mysqli_num_rows($result);
    echo "$rowcount";

    $row = mysqli_fetch_array($result);

    echo "Store code".'<br/>';
    echo $row['Chain_Name'].'<br/>'.'<br/>';
  }
?>

Kindly suggest what needs to be done to get all the Chain Name .

$sql = "SELECT * FROM tbl_store WHERE username = '$userid' AND password='$pid'";

    $result = mysqli_query($conn,$sql);
    $rowcount=mysqli_num_rows($result);
    echo "$rowcount";
    $chainName = array();
    //if($rowcount>0){
        while($row = mysqli_fetch_array($result)){

           echo "Store code".'<br/>';
         echo $row['Chain_Name'].'<br/>'.'<br/>'; 
         $chainName[] = $row['Chain_Name'];
      }
      print_r($chainName); this will have all chain names 

ALternate solution

 $sql = "SELECT GROUP_CONCAT("Chain_Name") as allChainName FROM tbl_store WHERE username = '$userid' AND password='$pid'";

$result = mysqli_query($conn,$sql);
        $rowcount=mysqli_num_rows($result);
        echo "$rowcount";
        $chainName = array();
$row = mysqli_fetch_array($result);
$chainName = explode(",",$row['allChainName']);
 print_r($chainName); // this will have all chain names

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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