Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling MYSQL CREATE VIEW using php code?

Tags:

php

mysql

view

need some enlightment here, and this is my first post here.

I would like to call and use my previously created mysql view using php... is it possible? or in another words, i'm wondering....can we OR how do we call mysql view, that we already created using php? to reduce long query coding

standard generic codes as follow :

$sql= " SELECT shipping.tarrif1, city.city_name
         FROM shipping JOIN city
   ON shipping.id_city = city.id_city";
$db->QueryArray($sql);   

while ($row = $db->Row()) {
echo $row->city_name. " - " . $row->tarrif1 . "<br />\n"; 
}

now for the MYSQL VIEWS :

$sql= " CREATE VIEW shipsumarry AS SELECT shipping.tarrif1, city.city_name
         FROM shipping JOIN city
   ON shipping.id_city = city.id_city";

Pure MYSQL command :

query: SELECT * FROM shipsummary

IN PHP :

 $sql = i'm badly stuck here...please help

How do we access it using php.

thanks before

Addition 1:

ok... let me rewrite the example :

$sql1= " CREATE VIEW shipsumarry AS SELECT shipping.tarrif1, city.city_name
         FROM shipping JOIN city
   ON shipping.id_city = city.id_city";
$sql2= "SELECT * FROM shipsummary";
$db->QueryArray($sql2);

$sql2 can not see shipsummary VIEW, coz it's already in a different var

how to utilise and then execute $sql1 ? & $sql2?

like image 973
Bayu Avatar asked Oct 07 '11 15:10

Bayu


1 Answers

The process is the same in PHP - a MySQL view is seen by the client (PHP) as a regular table, so querying it as

mysql_query("SELECT * FROM shipsummary");

// Or for whatever framework you're using:
$db->QueryArray("SELECT * FROM shipsummary"); 

should work correctly. If it does not work correctly, the MySQL user with which you are accessing the view may have broken permissions. (Seems unlikely though).

UPDATE

After you edited your question, I can see the problem quite clearly.

$sql1= " CREATE VIEW shipsumarry AS SELECT shipping.tarrif1, city.city_name
         FROM shipping JOIN city
   ON shipping.id_city = city.id_city";
$sql2= "SELECT * FROM shipsummary";

// Here, you need to execute $sql1 before $sql2 is useful.
$db->QueryArray($sql1);
// Now execute $sql2
$db->QueryArray($sql2);

We don't know what database class or framework you are using, but if there is a comparable method to QueryArray() that doesn't return a result set, but just executes a statement, use it to create the view instead.

Now, all that being said...

Unless the definition of the view must change every time this code executes, and unless you have a reason to then DROP VIEW shipsummary at the end of this script's execution each time, it makes far, far, far, far .... more sense to simply create the view in the database, where it will stay forever, rather than to keep re-creating it with PHP. Views, once created, stay created.

Don't think of them as a temporary query time/code saver. Create the views you will need ONCE in your database (using PHPMyAdmin or mysql CLI, or however you created your tables), and access them with PHP.

like image 134
Michael Berkowski Avatar answered Sep 30 '22 02:09

Michael Berkowski