YII DAO - Sending Multiple queries to database and retrieving results - php

I have scenario where I have to send different queries to the database to get the result and display it.
Using $mysqli->multi_query in PHP we can send all the queries together to the database and get the results in one shot .Here is the link for it
http://se2.php.net/manual/en/mysqli.multi-query.php
Can I do the same using Yii Command ..? I see QueryAll is returning only for the first query If I combine the queries together separated by ";"
Thanks for checking .

I've made an extension for this.
https://github.com/javijuol/yii-CDbExtendedConnection
It's in a very early stage. Give it a try.

Related

MySQL querying view alternatives

I am having an issue querying a couple of MySQL VIEWS.
The MySQL tables are generated hourly from a process that pulls data from a central Oracle database. Once the update is complete, a query automatically updates the VIEWS. (I do believe this is standard. I could be wrong).
At this point, I am using PHP to query said VIEWS to return data to the page. This is where my problem begins.
When the query begins, it can take well over 30 seconds to return 4 records. The VIEW contains no more than 30K records. But when I try to return a larger data set to the page, it now causes the server to freeze.
The query from my PHP script is very simple. It is looking like the following:
SELECT
DISCHARGE_ETA,
TERMINAL_DISCH,
IMPORT_RAMP,
ORIG_RAMP_FINAL_DEST_ETA,
POOL_LOCATION_FROM_IMP,
POOL_LOCATION_TO_IMP,
ROAD_HAULIER_IMP
// few more columns
FROM
view1
WHERE
" . $_SESSION['where'] . ";
I am using jQuery to send parameters over to the PHP script. PHP then builds the query by plugging all the parameters into the WHERE clause. The data is then returned via JSON.
The query that builds the actual VIEW in MySQL is a little more intense (currently not shown here).
My question is: are VIEWS the best route in retrieving the data I need?
I was thinking it would seem easier to query a TABLE instead of a VIEW. Perhaps the same hourly process that is updating the VIEWS could instead update a single TABLE. That way, I could query the TABLE instead of the VIEW.
Would this be the best bet? If not, are there any alternatives?
Edit
Here is the results from EXPLAIN

Optimize way to fetch data from multiple tables in mysqli PHP

Right now I am learning to write an android api in php for some CRUD operation.
I am able fetch data from the database without any issue.
Now I want to fetch 200, 500 and 600 records from three different tables.
At the end I am going to show it the UI by grouping them at their appropriate position.
My question is, should I write multiple php files for fetching the records from each table and send it back to the user separately (obviously I have call the api separately one after the another from the app after getting the response of each of the previous call).
OR
Only one php file where I will fetch all the records from the three tables and send it back to the user in one shot.
I am returning the data in json format.
Please help in figuring out which one of the above method should I use and its advantage or disadvantage if any..

Multiple queries in code igniter - semicolon breaks query

I am working on a project where I need to create a pivot table with dynamic columns. To this end I am using the tutorial as described here: http://stratosprovatopoulos.com/web-development/mysql/pivot-table-with-dynamic-columns/
Using that tutorial, I was able to make a proper query to make a pivot table.
When I execute it via phpMyAdmin SQL dialog, the query runs fine and the results come up as expected. The problem I ran into is that when I try to run the query through Code Igniter via $this->db->query($query), it fails at the first semicolons that mark the end of the first SQL statement in the overall query. The problem is that the statement is built up of several queries that I need to run at the same time.
I read about transactions on CI, but could not figure how (if possible) to obtain the result of the query.
Will greatly appreciate any advice on a proper way to run that query on CI and avoid the semicolon problem.
Thanks!
In case this is helpful to anyone in the future, I was able to get over the multi-statement issue by placing the whole query inside a stored procedure. The procedure along with its parameters is then called via CodeIgniter.

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.

Php / MySql advice

I need to have a button to fire an action to copy all records from a defined client from one database to another with php.
The template database has 12 tables (diferent rows on each) but all with the row client_id to make the WHERE clausule work properly.
The question is, how do I do this?
Thanks,
Pluda
Since PHP is a Server-side programming language, you can't copy something from the client. You can however upload Data (like XML), parse it and then insert it into your MySQL Database.
If you want to copy records from one to another database, you might want to read from the Database and save them in a format like SQL. Then, you could send those querys to the second Database.
An advise at this point: If you need to make the same Query (with different values) over and over again, you should use a PreparedStatement. It will be compiled in the Database and then just filled out with new values. This is way faster then using an Insert every time.

Categories