what is advantages of using SQLite3::createFunction() over simple php function?
You can use it in SQL-queries
SELECT col1, my_function(col2) FROM tab;
With PHP-functions you must iterate over the result set on your own. I don't know, how exactly it is implemented, but as far as I know SQLite3 works with cursors and therefore the function should only get executed, when you retrieved a row.
Related
I am trying to find the best way to get records from a MySQL table via a PHP function call using a prepared statement and PDO?
Efficiency is not the issue (I am sure there is perhaps a faster way or more memory efficient way to accomplish this task). My goal is to make the code simple, with one call to a function for to obtain records via a PHP function call and a using PDO prepared statement.
The caller so far looks like this:
$records = DB_Get_Records($pdo_connection, '"SELECT * FROM Table1 WHERE ID = ?', array($record_id);`
And the function looks like this:
function DB_Get_Records(&$pdo_connection, &$sql_statement, &$records_located, &$select_string, &$param_array) {
$sql_statement = $pdo_connection->prepare($select_string);
$sql_statement->execute($param_array);
$records_located = $sql_statement->fetch(PDO::FETCH_ASSOC);
return $records_located;
Because of the scope of the params, I am using pass by reference. But I am not sure how to best define in the main portion of the script. If I use pass by value, I believe that all results will be lost after returning from the function.
The confusion is how to define them prior to the function call so that they will receive the data. I am not sure if I have to reserve space of a some sort prior to calling the function. When using
$records_located = $sql_statement->fetch(PDO::FETCH_ASSOC);
In either the main portion of the script or the function, $records_located receives the records.
But trying to path this information from the caller to the main is not clear to me. When used in the function, the fetch statement (I believe) will create the necessary space using $records_located as the pointer. The question that pops up seems to be is how? Is the receiving variable used to create an array and then setting a point to $records_located variable? If so, then the scope would of the array would likely be limited to the function call. And if that is the case, then -- although the data pointer is retained -- the actual data returned from fetch would be gone after the function returns.
Hopefully, I am missing something here or overthinking the issue.
Again, it is not about trying to maximize speed of memory usage. It is simply about making simple calls to the database of one line instead of many lines to setup each call to get records.
Thanks for any help you can provide!
Looks like you need to forget all about allocating memory and who does what where, PHP mostly does that for you. If you change all of your parameters to NOT be passed by reference and then
return $sql_statement->fetchAll(PDO::FETCH_ASSOC);
This will just return an array of the results and no scope is involved
Is it possible to perform a specific PHP function on data that is being returned by a database query, at the very moment this query is still running?
Let's say we are in some class and we have the following pseudo SQL which should return a couple of rows:
$results = $this->db->query("SELECT PHP_function(column), column 2 FROM table")->fetchAll(PDO::FETCH_ASSOC);
PHP_function could be json_decode for example.
Or is this not possible and would it require an additional loop on the results in order to do such a thing?
A manual page for the very function you're using contains an example
No, this is not possible. SQL engine (mysql or any other) is a separate application. PHP can communicate with it, send queries and receive data. Depending on what you want to do in that custom function, you may be able to write a custom function in mysql to do the same thing.
Have a look at create function documentation for info on how to do that.
Where's the problem with this?
<?php
foreach($results as $key=>$res){
$results[$key]['column'] = PHP_function($res['column']);
}
Beside that MySQL has some useful functions included already, so maybe that one you need is usable right in the query
As i know it's not possible to achieve php manipulation while query running , you need to take result and then loop aur use predefined function from
I am using a combination of php and MySql. My case is I am using two queries (one 'select' and 'insert') for an activity, sometimes 3 queries. This will be done very frequently by different users.
I am using mysql_query function separately to execute the queries. Is this good or is it better to use a SQL function which executes all the queries. I want to know which is better regarding performance.
In my opinion it is better practice to create a function which performs a single operation well. This makes the functions easier to test and allows them to be utilized in other operations in the future. In your case I would create a stored procedure to execute the routine.
If you're using the mysqli extension you can utilize multi_query:
http://php.net/manual/en/mysqli.multi-query.php
However please note that if one of the queries relies on the success of another of the queries, they should be separated out for error checking.
It would help if you could show concrete examples - right now, it's all very hypothetical. However:
If you can combine the operations into a single query, it's the fastest option. For instance:
insert into tableA
select x, y, z
from tableB
where Foo = Bar
If you have to do some processing on the results of the select statement, you can create a stored procedure to do the processing. This avoids the round trip of sending the data back to your PHP server, which should yield a performance benefit - however, it may well be that PHP is a better/faster language to execute the processing in than the stored procedure. For instance, if you have to manipulate text, I'd do it in PHP.
Why is it necessary using 2 functions in PHP for opening a recordset? like:
$rc = mysql_query($sq, $db);
$rs = mysql_fetch_array($rc);
Can a recordset be handled also directly without mysql_fetch_array? What is the disadvantage?
mysql_query returns a resource and not data.
You must fetch each row by some way mysql_fetch_array can help you to move between the results (between pointers)
The way an SQL query is made, at least at the level of the C library that's used by PHP as an interface between PHP and the MySQL database, goes in two steps :
Execute the query
Fetch the results
The msql_* functions are more or less based on that C-library (libmysql, before PHP 5.3 and its mysqlnd), and, so, work the same way.
Basically, the mysql_* functions of PHP are nothing more than wrappers arround the functions exposed by libmysql.
For more informations about the C library in question, see :
MySQL C API
C API Function Overview
No, because the first is used for database interrogation and the second is used for iteration of the array.
This sounds like a really simple question, but I am new to PHP. If I have a statement like this:
$r =& $db->query("insert into table (col1, col2) values (10, 20)");
Do I have to still execute it, or does it get executed when I reference it? I have another case where I have a select query, which seems logically to run only when I call fetchrow, but the code I am copying from does not call execute or fetch. I would have expected it to, so I cannot tell if it is just that I don't get it, or that the missing execute statement is the problem. It also does not insert the record, but it does not throw an error I can find.
Also, I am a little confused by the =& notation. I looked it up on google, and found a few mentions of it, but I am still not clear on it.
Thanks.
It will be executed when you call query()
The =& notation is obsolete... it used to make the function return a reference to the resource object. But current versions of PHP (>5.0, I think) always pass (and return) objects by reference, so it doesn't really change anything anymore.
The query gets executed when you call the query function. when you talk about code that needs to be fixed, what is broken, and what does the code that "need[s] to be fixed" (according to who?) look like?
& is used in several contexts and it means by reference. You should start reading from here:
http://es.php.net/manual/en/language.operators.assignment.php
http://es.php.net/manual/en/language.references.return.php
In your code snippet it's most likely unnecessary (although you give little clue about what $db is) because the result set is probably an object and objects no longer need to be assigned by reference since that's the default behaviour. If you are learning PHP, be careful with outdated tutorials.
$db->query is a method contained by a class, it's not functional in php out of context. It is possible that this example of yours comes from a larger application that uses a database abstraction layer like ADODB or any of its kind.
If this is the case, then you could refer to the documentation specific to that db abstraction layer, because the query could be contained in a transaction for example and it would not be executed as soon as you call query();
To be sure the query is executed immediately try testing with a simple mysql function:
mysql_query("SHOW TABLES");