Display data returned by function in PHP MySQL

Gags

This is the one thing that I am trying absolutely first time. I have made few websites in PHP and MySQL as DB but never used functions to get data from database.

But here is the thing that I am trying all new now as now I want to achieve reusability for a bog project. I wanted to get the order details from DB (i.e. MySQL) through PHP Function. I am bit confused that how to print values returned by a PHP function.

Function wrote by me is as below:

function _orderdetails(){
    $sql = "Select * from orders WHERE 1";
    $result = DB::instance()->prepare($sql)->execute()->fetchAll();
    return $result;
}

Please let me know how i can print these values and value returned by above function is an array.

I tried by calling function directly through print_r(_orderdetails());

Please let me know:

  1. Efficient way of iterating through this function to Print Values.
  2. Is there any other approach that can be better worked upon?
Your Common Sense

You cannot chain fetchAll() to execute() like this.

Besides, for a query that takes no parameters, there is no point in using execute(). So change your function like this

function _orderdetails(){
    $sql = "Select * from orders WHERE 1";
    return DB::instance()->query($sql)->fetchAll();
}

and so you'll be able to iterate results the most natural way

foreach (_orderdetails() as $order) {
    echo $order['id'] . '<br />';
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Display mysql table data in PHP

분류에서Dev

Using PHP, get data from MySQL and display it

분류에서Dev

PHP refuses to display mysql data in framework grid

분류에서Dev

Simple mySql-php data display

분류에서Dev

How to display dynamic mysql vertical data to horizontal using php

분류에서Dev

PHP round function returned a weird result

분류에서Dev

Use AJAX to display dictionary data returned by Django view in a table on the template

분류에서Dev

Undefined Function PHP MYSQL

분류에서Dev

How to display data for a certain period of time (day / week / month) from Mysql table in html table (php)

분류에서Dev

php current_time() function returned wrong time

분류에서Dev

Adding multiple returned MySQL rows to the same array key element in PHP

분류에서Dev

MYSQL Trim function using on PHP

분류에서Dev

Display Single PHP Data in 2 Columns

분류에서Dev

Json data to MySQL using PHP

분류에서Dev

php - mysql storing old data

분류에서Dev

php mysql select data to gridview

분류에서Dev

PHP MYSQL -> Display dates saved as 'date' type from MYSQL Database

분류에서Dev

Are elements returned by lxml xpath function returned in order?

분류에서Dev

PHP function giving me exception when trying to display on HTML

분류에서Dev

To display photos of same alias name using php and mysql

분류에서Dev

Error Appears When Trying to Display MySQL Table Data in HTML Table

분류에서Dev

Using PHP to submit data to MYSQL Database

분류에서Dev

PHP mysql select data where column array

분류에서Dev

PHP AJAX MySql grabbing data - radio button

분류에서Dev

Proper way to insert data into table with php and mysql?

분류에서Dev

How to pass a MySql Hierarchical data to a array in PHP?

분류에서Dev

PHP and MySQL query outputting data twice

분류에서Dev

Can't update data in mysql from php

분류에서Dev

Check input data by MySQL schema or PHP?

Related 관련 기사

  1. 1

    Display mysql table data in PHP

  2. 2

    Using PHP, get data from MySQL and display it

  3. 3

    PHP refuses to display mysql data in framework grid

  4. 4

    Simple mySql-php data display

  5. 5

    How to display dynamic mysql vertical data to horizontal using php

  6. 6

    PHP round function returned a weird result

  7. 7

    Use AJAX to display dictionary data returned by Django view in a table on the template

  8. 8

    Undefined Function PHP MYSQL

  9. 9

    How to display data for a certain period of time (day / week / month) from Mysql table in html table (php)

  10. 10

    php current_time() function returned wrong time

  11. 11

    Adding multiple returned MySQL rows to the same array key element in PHP

  12. 12

    MYSQL Trim function using on PHP

  13. 13

    Display Single PHP Data in 2 Columns

  14. 14

    Json data to MySQL using PHP

  15. 15

    php - mysql storing old data

  16. 16

    php mysql select data to gridview

  17. 17

    PHP MYSQL -> Display dates saved as 'date' type from MYSQL Database

  18. 18

    Are elements returned by lxml xpath function returned in order?

  19. 19

    PHP function giving me exception when trying to display on HTML

  20. 20

    To display photos of same alias name using php and mysql

  21. 21

    Error Appears When Trying to Display MySQL Table Data in HTML Table

  22. 22

    Using PHP to submit data to MYSQL Database

  23. 23

    PHP mysql select data where column array

  24. 24

    PHP AJAX MySql grabbing data - radio button

  25. 25

    Proper way to insert data into table with php and mysql?

  26. 26

    How to pass a MySql Hierarchical data to a array in PHP?

  27. 27

    PHP and MySQL query outputting data twice

  28. 28

    Can't update data in mysql from php

  29. 29

    Check input data by MySQL schema or PHP?

뜨겁다태그

보관