0

I'm doing a project and I want to send data from function which is on the another php file, then I will show that data in a list which should have button view.

This is my data base management file:

<?php
class Database{

    private function connect() {
        $link = new mysqli ("localhost", "xx", "xxx", "table");
        /* check connection */
        if (mysqli_connect_errno ()) {
            print ("Connect failed: %s\n" . mysqli_connect_error ()) ;
            exit ();
        }
        if (! $link->set_charset ( "utf8" )) {
            printf ( "Error loading character set utf8: %s\n", $mysqli->error );
        }
        return $link;
    }

    private function disconnect($link)
    {
        $link->close();
    }

       public function getAllRestaurants($id)
{
    $db = $this->connect();
    $results = array();
    if($res = $db->query("select * from restaurants where id='$id'"))
        {
            while($rows = $res->fetch_assoc())
                {
                    $results[] = $rows;
                }
        }
    else {
        die($db->error);
    }
    $this->disconnect($db);
    return $results;

}

and this is my other php file. I want to show data on list in that file:

    <?php

    $ID = isset($_GET['id']) ? intval($_GET['id']) : null;

    if (empty($ID))
        {
            echo("ERR");
        }

    // Use $ID to fetch whatever data you need

include 'php_includes/db_conf.php';
$db = new Database();
$db->getAllRestaurants();
 $restaurants = $db->getAllRestaurants($id);
foreach ($restaurants as $rest) {
    echo 'Name is ' . $rest['name'] . ' and address is ' . $rest['address'] . '<br>';
}


    mysql_close($db_conx);
    ?>'
2
  • This doesn't seem ok: if($res = $db->query("select * from table where='".$id."'")).. Where id? Maybe where id = .$id. ? Dec 24, 2013 at 22:51
  • yes you are right this is also wrong. but still couldnt get information to show as buttons? Dec 24, 2013 at 22:53

1 Answer 1

0

You need to collect the result rows into an array:

    public function getAllRestaurants($id)
    {
        $db = $this->connect();
        $results = array();
        if($res = $db->query("select * from restaurants where id='".$id."'"))
            {
                while($rows = $res->fetch_assoc())
                    {
                        $results[] = $rows;
                    }
            }
        else {
            die($db->error);
        }
        $this->disconnect($db);
        return $results;
    }

The calling code might look like:

$restaurants = $db->getAllRestaurants($id);
foreach ($restaurants as $rest) {
    echo 'Name is ' . $rest['name'] . ' and address is ' . $rest['address'] . '<br>';
}
13
  • thank you. im adding that part but im a litte bit confused about showing as button. how can i use that array on my other file. Dec 24, 2013 at 22:59
  • When you call getAllRestaurants, loop over the results and display them however you want.
    – Barmar
    Dec 24, 2013 at 23:00
  • i got the point but. for example i have city_id name and adress under that table. how can i get name and adress? Dec 24, 2013 at 23:03
  • it's just basic array accessing. I've added a simple example to the answer.
    – Barmar
    Dec 24, 2013 at 23:07
  • now im facing with You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table where id=''' at line problem. and updated my code Dec 24, 2013 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.