1

Class function that is retrieving values from db

public function retrieveFun()
    {
        include 'inc/main.php';//connecting to db.
        $result = mysqli_query($con,"SELECT * FROM db  order by name DESC");
        while($row = mysqli_fetch_array($result))
          {
            $var = array('name' =>$row['name'] ,
                          'owner'=>$row['owner'],
                          'date'=>$row['date'] );
            // echo $var["name"]."<br/>";
            // echo $var["owner"]."<br/>";
            // echo $var["date"]."<br/><br/>";
            return array($var["name"],$var["owner"],$var["date"]);
          }
    }

and code where I want to display all the rows retrieved in desired format

$obj =  new classname();

$id= $obj->retrieveFun();

echo //here i want to display all the rows retrieved in table format.

Please help me out as soon as possible

1
  • I guess you're a beginner. Try decommentig the // echo $var, and you will have an idea of what is happening. Commented Mar 18, 2014 at 15:45

4 Answers 4

1

You've got return statement inside while loop, it has no sense. Try to put return statemanet outside the loop. To retrive result in table format i would do :

public function retrieveFun()
{
    include 'inc/main.php';//connecting to db.

    $result = mysqli_query($con,"SELECT * FROM db  order by name DESC");

    $resutlArray = array();
    while($row = mysqli_fetch_array($result))
    {
        array_push($resultArray, $row);
    }
    return $resultArray;
}

Then to display your result in table format use :

$rows = $obj->retrieveFun();
echo '<table>';
foreach( $rows as $row ) 
{
   echo '<tr>';
   echo '<td>'.$row['name'].'</td>';
   echo '<td>'.$row['owner'].'</td>';
   echo '<td>'.$row['date'].'</td>';
   echo '</tr>';
}
echo '</table>';
Sign up to request clarification or add additional context in comments.

1 Comment

so can u explain how can retrieve values then.
0

Put the return call outside your while loop. At the moment, only the first result would be stored before being returned. What you want to do is populate $var first before returning the variable.

When displaying the rows:

$rows = $obj->retrieveFun();
foreach( $rows as $key => $row ) {
    echo $row[$key]['name'];
    echo $row[$key]['owner'];
    echo $row[$key]['date'];
}

See modification for $key support.

EDIT:

Also, when fetching the results, store them in an array.

$var = array();
while ( $row = mysqli_fetch_array( $result ) ) {
    $var[] = array( ... );
}

2 Comments

i am getting following error Illegal string offset 'name' in /Applications/XAMPP/xamppfiles/htdocs/xampp/Biz/FunnelUpdateMarch18/index.php on line 32 F Warning: Illegal string offset 'owner' in /Applications/XAMPP/xamppfiles/htdocs/xampp/Biz/FunnelUpdateMarch18/index.php on line 33 F Warning: Illegal string offset 'date' in /Applications/XAMPP/xamppfiles/htdocs/xampp/Biz/FunnelUpdateMarch18/index.php on line 34 F
Do a var_dump( $row ) under foreach and see what that gives you. Or do a var_dump( $rows ) before foreach.
0

http://www.w3schools.com/Php/php_mysql_select.asp

W3C is an amazing site for beginners. That page I linked has exactly what you need. It has examples and the source code all there to see, plus explanations. Read the examples and it'll explain it and you should be able to understand it.

If not, feel free to message me and I'll do what I can.

W3C has many tutorials that can cover just about all aspects of web, browse and be amazed!

It's good to check around the web before posting questions. We're here to help, but only if you try to help yourself first.

Hope it helps. If you got your answer from this, please vote it up and mark it as the answer so others can find it easier.

Note: It's in the second section under 'Display the Result in an HTML Table'.

Comments

0

I really like the PDO approach over MySQLi it's much cleaner. Here's what I would do. A class with the DB connection and a public function inside the class.

Class + Function

class Database
{
    private $db = null;

    public function __construct()
    {
        $this->db = new PDO('mysql:host=localhost;dbname=pdo', 'root', 'password');
    }

    public function retrieveFun()
    {
        $query = $this->db->prepare("SELECT * FROM db ORDER BY name DESC");
        $query->execute();
        return $query->fetchAll();
    }
}

Then to retrieve data I use foreach

if (retrieveFun()) { //Verify if the DB returned any row
    foreach (retrieveFun() as $key => $value) {
        echo $value->name . "<br />";
        echo $value->owner . "<br />";
        echo $value->date . "<br /><br />";
    }
} else {
    echo 'No data found.';
}

If you want to set up the connection in your main.php you can do the following:

//This section goes in the main.php file
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'db_name');
define('DB_USER', 'root');
define('DB_PASS', 'password');

//You can then call the defined constants like this
$db = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME, DB_USER, DB_PASS);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.