Durgesh Suthar
Durgesh Suthar

Reputation: 3274

Can't fetch the column data even after successful execution of query

Here is snippet of my code

   $query="SELECT cid,city_name FROM city_trade WHERE city_name LIKE '$num' OR pin='$num'";
   $res=pg_query($query);
   if (!$res) {
     echo "Data Not found for this City"; exit;
   }else{
     $id=pg_fetch_assoc($res)['cid'];
     $name=pg_fetch_assoc($res)['city_name'];
     var_dump($id);
     var_dump($name);
   }

And I am getting $name as NULL and $id is correct.
Why city_name is not coming after successful execution of query ?
And how we can fetch it without using an extra query ?

Upvotes: 1

Views: 228

Answers (1)

John Conde
John Conde

Reputation: 219804

You're only getting one result but you're trying to fetch two rows from your result set. You only need to call pg_fetch_assoc() once to get your result set and then you can access the values you want from there:

// Get results of query in an associative array
$row  = pg_fetch_assoc($res);
// Get each desired value
$id   = $row['cid'];
$name = $row['city_name'];

Upvotes: 3

Related Questions