Zorak Zorak - 4 years ago 69
PHP Question

PHP SQL result as whole "object"

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download