ratr
ratr

Reputation: 616

Unable to get key value from a mysql multi-dimensional array

I am trying to get the key value from the multidimensinal array which I have created using .The Array snapshot is given after the Code.

Below is my PHP code-

 $selectTicket =  "select ticketID from ticketusermapping where userID=$userID and distanceofticket <=$miles;";
$rsTicket = mysqli_query($link,$selectTicket);
$numOfTicket = mysqli_num_rows($rsTicket);  
if($numOfTicket > 0){
    $allRowData = array();  
while($row = mysqli_fetch_assoc($rsTicket)){
    $allRowData[] = $row;
}
 $key = 'array(1)[ticketID]';
    $QueryStr = "SELECT * FROM ticket WHERE ticketID IN (".implode(',', array_keys($key)).")";

Array Snapshot- enter image description here

I need the tickedID value from this array . Like the first one is 49 . Please help.

Upvotes: 0

Views: 30

Answers (3)

Mihai Matei
Mihai Matei

Reputation: 24276

You should do a single query using JOIN for this:

$query = "
  SELECT t.* 
  FROM ticket t 
  JOIN ticketusermapping tum 
    ON t.ticketID = tum.ticketID
   AND tum.userID = '$userID'
   AND tum.distanceofticket <= '$miles'
";

$stmt = mysqli_query($link, $query);

$numOfTickets = mysqli_num_rows($stmt);

while($row = mysqli_fetch_assoc($stmt)){
    var_dump($row); // here will be the ticket data
}

Upvotes: 0

Rakesh Sojitra
Rakesh Sojitra

Reputation: 3658

change your code like

$selectTicket = "select ticketID from ticketusermapping where userID=$userID and distanceofticket <=$miles;";
    $rsTicket = mysqli_query($link, $selectTicket);
    $numOfTicket = mysqli_num_rows($rsTicket);
    if ($numOfTicket > 0) {
        $allRowData = array();
        while ($row = mysqli_fetch_assoc($rsTicket)) {
            $allRowData[] = $row['ticketID'];
        }
        $QueryStr = "SELECT * FROM ticket WHERE ticketID IN (" . implode(',', $allRowData) . ")";

Upvotes: 1

nospor
nospor

Reputation: 4220

$ids = array_column( $allRowData, 'ticketID'); //this will take all ids as new array
 $QueryStr = "SELECT * FROM ticket WHERE ticketID IN (".implode(',', $ids).")";

Upvotes: 1

Related Questions