-2

help me with this little issues, i have two tables, taxi and hotel which have same column name "pangkalan1", and i wanna create some filter that , when i ran this query "SELECT * FROM taxi,hotel WHERE taxi.pangkalan1 = hotel.pangkalan1 AND taxi.pangkalan1 IN ('A','A');" and the results is list of taxi or hotel that have pangkalan1=A,and works , until i confuse with the fetch object in my sql

can anybody help me?

this my code

    <?php
function datarelasi(){
$ambil = mysql_query("SELECT * 
FROM taxi,hotel 
WHERE taxi.pangkalan1 = hotel.pangkalan1
AND taxi.pangkalan1 IN ('A','A');");
While ($row=mysql_fetch_object($ambil)) {
$nama    = stripslashes($row->taxi->nama);
Echo $nama;
}
}
echo datarelasi();
?>

the results is

Notice: Undefined property: stdClass::$taxi in C:\xampp\htdocs\dss\inc\relasi.php on line 21

Notice: Trying to get property of non-object in C:\xampp\htdocs\dss\inc\relasi.php on line 21

, i just want to fetch individual object from table, taxi->name, or hotel->name,

thanks

jchristsake
  • 53
  • 1
  • 4
  • When sending a request you get a stream. This stream is built with rows that all have the same structure. $row->taxi doesn't mean anything (unless you have a column named 'taxi'). You should be able to fetch the info you want with $row->nama. You could also print your row to see what you have in it before doing blind operations – Unex Apr 07 '15 at 15:51

1 Answers1

0

You should not be using the mysql_ extension. Please look at MYSQLI_ or PDO as the modern alternative. If you continue to use the MYSQL_* extension sometime soon when you want to upgrade your version of PHP, that extension will no longer work

If you are not sure what is being returned from a mysql_fetch_object() call then just dump it using print_r() But normally you shoudl get a class property for each column you select in the query.

You also dont need the IN('A','A') as what you actually need to code is taxi.pangkalan1 ='A'

<?php
   function datarelasi() {
      $ambil = mysql_query("SELECT * 
                            FROM taxi,hotel 
                            WHERE taxi.pangkalan1 = hotel.pangkalan1
                              AND taxi.pangkalan1 ='A'");

      While ($row=mysql_fetch_object($ambil)) {
         // dump whats returned when you are not sure
         // print_r($row);

          $nama    = stripslashes($row->nama);
          Echo $nama;
      }

   }
   // call the function
   datarelasi();
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • i try print_r($row); it just only show table from hotel, but i try query "SELECT * FROM taxi,hotel WHERE taxi.pangkalan1 = hotel.pangkalan1 AND taxi.pangkalan1 IN ('A','A')" phpmyadmin and the results is both table, (taxi and hotel), any ideas? – jchristsake Apr 07 '15 at 16:05
  • Do you have more than one column with the same name in these 2 tables – RiggsFolly Apr 07 '15 at 16:08