PHP function on SQL selectee - php

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

Related

How to return MySQL records via PHP function call using PDO and Prepared Statements?

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

SQLite3::createFunction vs internal php function

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.

Take SQL input in PHP and output query results to the page

Basically I'm looking to create a page using PHP that will take SQL input, and output the results returned by the DB (MySQL). This is not for a production website (I understand the security implications). It's more for learning and practice. Kind of like the SQL console section of phpMyAdmin, or even similar to what sqlzoo.net can do (I think they are using perl, but I'd like to do it in PHP). Is there a practical way to accomplish this?
For example, how can I create a page in PHP/HTML to display a table of results when I don't know how many columns the query will return?
Also, what is the most practical way to allow a visitor to this web page to restore the DB to a default state with the original data? (e.g. create a sql dump of the original state and make a button that runs it? or is there a better way?)
Thanks!
Use * in your SQL query to fetch all columns and loop over the results from mysql_fetch_row() or mysql_fetch_assoc() with foreach.
Besides that, have you thought of using the mysql CLI ? It's useful for those requirements.
This question should be more specific than it is now.
"create a sql dump of the original state and make a button that runs it?" - Yes. But make sure you drop/delete the existing data.
You may have to run at least two queries... first return one row using LIMIT 1, and count the returning elements (using PHP count($row) if you use mysql $row = fetch_row($handle) ) to count the columns, and you can use SQL COUNT() to find out how many rows would be returned.
As for returning data to original state, I think a drop/recreation from a dump like you said may be the simplest and most reliable option.
Your best option is just running the query, checking if the amount of rows > 0, and then if it is, loop through the query resultset in a foreach and just show whatever you like.

Can a recordset be opened in PHP using one call?

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.

call database row in to a function

I am writing a function which is called into a page, but I am not sure how to call information form database into the function in order to use them. Basically I am doing some calculation in the function where I need information form database to do them.
Is there anyone who can give a clue on how ot do this? Many thanks F
From a comment...
What I am trying to do is this: I have
a page in php which retieves some info
from database and all works fine. I am
writing a function that needs to make
some calculation based on some fields
in the database. What I cannot solve
is how to get this information form
the database into my function. I have
tired this: function CalculateCost ()
{ $low_season =
$row_rsbooking['cost']; etc. etc. then
making some calculations but I am
getting nowhere. I am not sure if the
function is getting the information
form database in order to make
calculation.
This is an older question, but as it remains unanswered, I will proffer my two cents.
From your comment to Rafael, it looks like your problem is variable scope. I think you want something like this
function add_one($cost)
{
// just something irrelevant to do
return $cost + 1;
}
// query stuff here, leaving $row with the results
$new_value = add_one($row_rsbooking['cost']);
In other words, pass the column value you need to the function and process the results it returns. If you need to alter your actual row, you could pass the whole row by reference (i.e. add_one(&$row)) and modify it in your function.
Without some more code from you, this is my best guess.
I am not sure I understand the question. But will try to answer it the best way I can.
If you are looking for help on how to connect to a database and execute a query, then have a look at the following link:
http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx
It describes how you can use the SQL Command object to get data from a SQL Server database in the form of a Dataset. Once this is done, you can get a dataset or datatable to get data to any function in your code for calculations.
You could also use an odbccommand. I would post a link, but I don't have enough reputation points to do more than one! :o)
If the Dataset is too big, you could consider using a datable or a SQLDataReader. Again, please have a look at the MSDN for info on those classes.
I hope this helps!
Rafael Jovel
www.augensoftwaregroup.com

Categories