0

I have two rows contaning data of same id. It has different adresses in different rows for the same id.

I want to fetch both the adress in different variable in php.

How can i do this? Please help!

Below is the code:

foreach($row_address as $address)
    {
        echo "<br> Address :" .$address;
        $info=Array(); 
        $data=explode(' ', $address ); 
       $info[1]=$data[0];
       $info[2]=$data[1];
       $info[3]=$data[2];
       echo "City :".$info[1];
       echo "Country :".$info[2];
       echo "Pin Code :".$info[3];

    }

function hoteladdresstable($id)
{
    global $conn;
    /*$sql2 = "select AddressLine from hoteladdress where Hotel_Id= " .$id;
    $result2 = mysql_query($sql2,$conn);
    $row2 = mysql_fetch_assoc($result2,MYSQL_NUM);
    return $row2;*/

    $query = "select AddressLine from hoteladdress where Hotel_Id = " .$id;
    $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)) 
    {
        $d[] = $row['AddressLine'];
    }
    return $d;
}

It gives me both the address of the same id in one variable only. I want them in two different variables.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 21 '15 at 12:32
  • You're returning an array, `$d`, that you will have to parse into variables for each address. – Jay Blanchard Jul 21 '15 at 12:33

3 Answers3

0

You are already getting an array of addresses in $d.

What you can do is:

$d = array();
while ($row = mysql_fetch_assoc($result)) {
  $d[$row['Address_ID']] = $row['AddressLine'];
}
extract($d, EXTR_PREFIX_ALL, 'ad');

If you have address ids 2 and 4, you will get two variables

$ad_2 and $ad_4

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

I suggest you to use mysqli instead.

$data = array();
while($row = mysqli_fetch_assoc($result)){
    $data[] = $row;
}
return $data;

and then

foreach($data as $oneRow){
    echo $oneRow['AddressLine']; //and all other stuff that you want.
}

you can verify it:

print_r($data);
Mubin
  • 4,325
  • 5
  • 33
  • 55
0

You should use parameterized queries. Use PHP's PDO:

$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

$STH = $DBH->prepare('select AddressLine from hoteladdress where Hotel_Id = :id');
$STH->bindParam(':name', $id);
$STH->execute();

$d = array();
while($row = $STH->fetch()) {
   $d[] = $row['AddressLine'];
}

http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

Don't want your queries getting injected with attacks.

Josh LaMar
  • 271
  • 3
  • 7