0

I am new in php and reading book "PHP Bible". I wanted to code an example. The example is following. I have a database with 2 tables. The first table is country and the second is city. The country table hase the following columns:

  id, continent and name

The city table has the following columns:

 id, countryId and name.

Now I would like to run the following query against the database:

select country.continent, country.name, city.name from country, city where country.id=city.countryId

The code I wrote so far is:

<?php
      $conn=mysqli_connect("127.0.0.1", "root", "123","example1");

- this establishes the connection with the database it works fine

 $query="select country.continent, country.name, city.name from    country, city where country.id=city.countryId ";
        $result=mysqli_query( $conn, $query);
        if(!$result)
            die("could not fetch the data");

Ideally the following table must be printed:

 print("<table border=1>\n");
        while($row=mysqli_fetch_row($result))
        {
            print("<tr>");
            for($col=0; $col<3;$col++)
                print("<td>$row[$col]</td>\n");
            print("</tr>");
        }
        print("</table>\n");

However,

 Could not fetch data 

message is printed.

When I use only one table, say country or city everything works fine. The trouble occurs when I try to use both tables. The error.log file gives the following error:

 HP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php5/20121212/msql.so' - /usr/lib/php5/20121212/msql.so: cannot open shared object file: No such file or directory in Unknown on line 0

What should I do to make code work? Why 1 table code works but does not work when I use 2 tables? Thanks in advance

Nigel Ren
  • 53,991
  • 11
  • 38
  • 52
olzha bala
  • 185
  • 1
  • 5
  • 14
  • 1
    It would help to have some more information on the error - https://stackoverflow.com/questions/17053466/how-to-display-errors-for-my-mysqli-query – Nigel Ren Aug 14 '19 at 15:08

1 Answers1

-1

Try to use the explicit Inner Join on Mysql Query:

SELECT cou.continent, cou.name, cit.name FROM country cou INNER JOIN city cit ON cou.id=cit.countryId;