0

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.

1
  • Where’s the } in this PHP code? Commented Jul 21, 2015 at 9:42

1 Answer 1

1
$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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.