使用php代码调用MYSQL CREATE VIEW?

发布于 2024-12-08 11:32:42 字数 1189 浏览 0 评论 0原文

这里需要一些启示,这是我在这里的第一篇文章。

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

标准通用代码如下:

$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"; 
}

现在是 MYSQL 视图:

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

纯 MYSQL 命令:

query: SELECT * FROM shipsummary

在 PHP 中:

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

我们如何使用 php 访问它。

谢谢之前

添加1:

好的...让我重写这个例子:

$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看不到shipsummary VIEW,因为它已经在不同的var中

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疏忽 2024-12-15 11:32:42

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

mysql_query("SELECT * FROM shipsummary");

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

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

更新

在您编辑问题后,我可以很清楚地看到问题。

$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);

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

现在,话虽如此......

除非每次执行此代码时视图的定义都必须更改,并且除非您有理由在每次执行此脚本时DROP VIEW Shipsummary ,它使得远、远、远、远......更有意义的是简单地在数据库中创建视图,它将永远保留在数据库中,而不是继续使用 PHP 重新创建它。视图一旦创建,就保持创建状态。

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

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.

婴鹅 2024-12-15 11:32:42

为什么不直接发送

SELECT * FROM shipsummary

到 mysql 查询,它应该可以工作,除非我不理解你的问题......

Why not just send that

SELECT * FROM shipsummary

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文