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
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.
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.
Related
I discovered something very strange with my PHP code and mysqli functions. When I have my code in the format below:
function mainline(){
$q=mysqli_query($this->conn,"select * from table",MYSQLI_USE_RESULT);
$dataset=parse($q);
}
function parse($q){
if (!$q){return NULL;}
while($res=mysqli_fetch_array($q)){$r[]=$res;}
mysqli_free_result($q);$q=NULL;$res=NULL;return $r;
}
I'm able to retrieve data and process it. In the above example, data is returned to $dataset and each element is retrieved in the form of $dataset[row number][field name].
Now when I change my code so its like this:
function mainline(){
$q=mysqli_query($this->conn,"select * from table",MYSQLI_USE_RESULT);
$dataset=parse($q);
}
function parse($q){
if (!$q){return NULL;}
while($r[]=mysqli_fetch_array($q)); // I made change here
mysqli_free_result($q);$q=NULL;return $r;
}
The data returned is always nothing even though the select statement is exactly the same and always returns rows. During both tests, nothing has modified the data in the database.
My question then is why does while($res=mysqli_fetch_array($q)){$r[]=$res;} retrieve correct results and while($r[]=mysqli_fetch_array($q)); does not?
With the second while loop, I won't have to allocate an extra variable and I'm trying to cut down on the use of system memory so that I can run more apache processes on my system instead of waste memory unnecessarily on PHP.
Any ideas why while($r[]=mysqli_fetch_array($q)); wont work? or any ideas how I can make it work without using an extra variable? or am I stuck?
if you want to store all result in array than why not use
mysqli_fetch_all($q)
and store result in whatever you want. Though if you want to have quick access I
think caching sounds more appropriate.
mysqli_fetch_all — Fetches all result rows as an associative array, a numeric array, or both
I need to take value form variable, hode one part and send the other part in email.
This variable is sql anywhere query result.
This is what i have so far:
$res=sqlanywhere_query(...)
$resID=explode('#',$res);
$email.=$email_footer.$resID[1].$email_footer2;
When I had in email $res, in email I get something liike Resource #163.
When I put $resID[1], in place where should be 163, space was empty.
That is because your $res is a resource, you have to get the results.
You should have for that library something like
$sql = sqlanywhere_query(...)
$res = sqlanywhere_fetch($sql);
and $res will be an array with your query result;
It's a resource, which means that it's a special variable that holds a reference to an external source.
See the PHP manual on Resources.
please, using database with php. when you query, you must pass the result to a fetch function before you can access the values.
$res=sqlanywhere_query(...)
//fetch one
$data = sqlanywhere_fetch_row($res)
// or u loop through
while($row = sqlanywhere_fetch_row($res))
{
echo $row["id"];
}
all these functiosn are deprecated. you can use mysql_query and mysql_fetch_row (or other fetch functions).
you can also use mysqli_ functions. read PHP manual.
hope it helps
I am creating a page that will read, write, and modify data in a mysql database. I am using php with codeigniter to perform the queries however the page that the user will see I am powering with javascript, dynamically filling and changing the data based on the user selections. To do this I am passing lots of data back and forth from php to javascript functions and vice versa, but I am wondering if there is a better way of formatting the data. For example here is a javascript function that uses XMLHttpRequest to call a php function and get the result:
var myarray = new Array(2);
myarray[0] = document.getElementById("my_input").value;
myarray[1] = document.getElementById("my_select").value;
xmlhttp.open("POST", "my_url/my_function/"+myarray.join('|'), false);
xmlhttp.send();
document.getElementById("my_element").innerHTML=xmlhttp.responseText;
I simply do an explode() on the string passed to the php function and execute the necessary queries. This seems like a very awkward interaction between php and javascript. Is there a better, or at least different, way to do this?
JSON (JavaScript Object Notation) seems to be what you are looking for. You can turn pretty much all kind of Javascript variables into a string using JSON.stringify and later recover the original structure in php using json_decode, and viceversa using json_encode and JSON.parse.
See:
PHP documentation on JSON
MDN Javascript documentation on JSON
Dear gods of Stackoverflow
Let's say I have a MySQL query that selects a large dataset:
$query = "SELECT col_1, col_2, ..., col_99 FROM big_table";
And I get a MySQLi result like so:
$result = $db->query($query);
But then instead of dealing with $result in this scope, I pass it to a function:
my_function($result);
And once inside my_function(), I iterate through each row in the result and do stuff:
function my_function($result) {
while($row = $result->fetch_object()) {
...
}
}
Please help me understand the memory implications of this approach.
In other words, what does $result contain, and are there any pitfalls with passing it to a function? Should I consider passing $result by reference instead? For what it's worth, I won't be needing $result once my_function() is done with it.
Cheers from South Africa!
You will not have any memory implications at all. $result holds a resource. It does not hold the whole result. It's just a resource Id of the result. MySQL uses this I'd to collect the result.
There are virtually no memory implications to this approach. PHP passes objects by reference, so very little memory is used when passing an object to a function.
The same is not quite true of arrays, they use a technique called Copy On Write, meaning that if you change an array inside a function then the array will be cloned.
You can always see for yourself what the impact is.
Objects in PHP 5+ are automatically passed by reference, so passing the result object into a function won't duplicate it.
You can then unset the variable after the function has been called.
I have a PHP page with multiple mysql_query instances. Almost every one uses a while loop to retrieve multiple rows of data.
As I keep a snippet of this often used code, and the example I was taught with, used the variable $row, I have multiple instances of the following on my page:
while ($row = mysql_fetch_assoc($foo_data)) {
$barArray[] = $row['barValue'];
}
I even have an instance of $row = mysql_fetch_assoc($foo_data) without a while loop.
I'm wondering if the multiple uses of $row as a variable on a single page, is all right?
The PHP is functioning fine, but I always want to be sure that my code is proper and conforms to standard rules.
Thank you.
yes, it's all right.
it's all right to use the same spoon when you're eating soup.
Yes that is fine. but if you try:
var_dump($row);
at the end of your code -- it will only return that last value of $row
Yup, not a problem at all.
Just like always using $i as your count in a for loop, you can repeatedly use $row to no ill effect.