-2

I am working on project, which will have limited PHP processes and possibly, many people will use the page. So I am trying to optimize it so the most (but secure) things will be handled clientside.

    $result = mysqli_query($db, "SELECT * FROM userdata");


    /*return $result; */

    while ($row = mysql_fetch_array($result)) {
      //do sth, e.g. get row to array
    } 
   /*return $array;*/ 

My question is simple in this case. Looping through each line takes time and will block the php process for quite a time. So, is here some solution, taht you will simply send SQL requests, from DB server you will get response in shape of "some bunch of data", that I will be able to pass directly to jquery, where I can handle/sort/edit/make viewable the result using client resoruces and not server resources?

(so far, making sql request and passing $result to jquery variable seems not returning anything, the jquery variable is empty)

Thanks in advance

Zorak
  • 709
  • 7
  • 24

2 Answers2

0

First of all, it looks like you are mixing mysql with mysqli.

mysqli method:

mysqli_query($db, "SELECT * FROM userdata");

mysql method:

mysql_fetch_array($result)

You SHOULD NOT do this. mysql method is deprecated, and both method are different api entirely, they are not meant to work together, eventhought its work, it is plain wrong to do so.

As for you question, in order to pass the fetched db data result to jQuery or Javascript for client side processing, you can convert the result array to JSON strings.

Do like this:

$row = mysqli_fetch_array($result);
echo json_encode($row);

Then using jquery, make ajax call to the php script. and parse the json data.

Afif Zafri
  • 640
  • 1
  • 5
  • 11
0

I think you're asking for this function :

http://www.w3schools.com/php/func_mysqli_fetch_all.asp

which will retrieve ALL the rows into a single nested associative array. You can then encode this array using JSON_encode() to process using client-side javascript.

Loveen Dyall
  • 824
  • 2
  • 8
  • 20