使用 php 代码调用 MYSQL CREATE VIEW?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7689715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:11:29  来源:igfitidea点击:

Calling MYSQL CREATE VIEW using php code?

phpmysqlview

提问by Bayu

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

我想使用 php 调用并使用我之前创建的 mysql 视图......这可能吗?或者换句话说,我想知道......我们可以或者我们如何调用我们已经使用php创建的mysql视图?减少长查询编码

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 :

现在是 MYSQL 视图:

$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 :

纯MYSQL命令:

query: SELECT * FROM shipsummary

IN PHP :

在 PHP 中:

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

How do we access it using php.

我们如何使用php访问它。

thanks before

之前谢谢

Addition 1:

补充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

$sql2 看不到 shipsummary VIEW,因为它已经在不同的变量中

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

如何利用然后执行 $sql1 ?& $sql2?

回答by Michael Berkowski

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

该过程在 PHP 中是相同的 - 客户端 (PHP) 将 MySQL 视图视为常规表,因此将其查询为

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).

应该可以正常工作。如果它不能正常工作,则您用来访问视图的 MySQL 用户可能具有损坏的权限。(虽然似乎不太可能)。

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.

我们不知道您使用的是什么数据库类或框架,但如果有QueryArray()与之类似的方法,它不返回结果集,而只是执行一条语句,请改用它来创建视图。

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 shipsummaryat 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.

除非每次执行此代码时都必须更改视图的定义,并且除非您有理由DROP VIEW shipsummary在每次执行此脚本时都这样做,否则它会变得很远,很远,很远,很远……更有意义的是简单地创建数据库中的视图,它将永远留在那里,而不是用 PHP 不断地重新创建它。视图一旦创建,就一直保持创建状态。

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

不要将它们视为临时查询时间/代码保护程序。在您的数据库中创建您需要一次的视图(使用 PHPMyAdmin 或 mysql CLI,或者您创建的表),并使用 PHP 访问它们。

回答by Mathieu Dumoulin

Why not just send that

为什么不直接发送

SELECT * FROM shipsummary

To mysql query, it should work, unless i'm not understanding your question...

对于 mysql 查询,它应该可以工作,除非我不明白您的问题...