我有一个简单但令人困惑的问题,特别是对于像我这样自学程序员的人......
我已经阅读了 PHP.NET 和 MYSQL.COM 中的不同文档,它们都解释了如何打开和如何关闭它,但对于这个问题并没有真正的帮助,至少对我来说。
我学习的方式如下
我需要一个文件来连接我的连接,例如 dbconnection.php 或 db.php,名称并不重要,但内容重要,所以这就是我总是做的...
<?php
// con.php
$host = 'localhost';
$us = 'root'; // or whatever name for the user
$ps = 'abcd'; // your password
$db = 'abc'; // The name of your DB
$con = mysqli_connect ($host, $us, $ps, $db);
if (mysqli_connect_errno()) {
echo "DB server offline: ". $mysqli->connect_error;
exit();
}
现在我已经连接到数据库了,现在让我们去取一些西瓜
<?php
include ('con.php');
// Lets get our varibales $_GET or $_POST
// Lets clean our variables
// Lets make sure that the data is what we expect, numbers and letters
// because thats how I roll!
$query_one = "SELECT * FROM table 1 WHERE id = 1";
$r_one = mysqli_query($con, $query_one);
$rows = mysqli_fetch_assoc($r_one);
// now lets echo the results, or print_r() or json()...
mysqli_close($con); // this is pretty straight forward i think...
现在这是我的困惑......
<?php
include ('con.php');
// Lets get our varibales $_GET or $_POST
// Lets clean our variables
// Lets make sure that the data is what we expect, numbers and letters
// because thats how I roll!
$query_one = "SELECT * FROM table_1 WHERE id = '$ids'";
$r_one = mysqli_query($con, $query_one);
$rows = mysqli_fetch_assoc($r_one);
// If I closed here the second query doesn't get executed...
if ($rows['publish'] == 1) {
$query_two = "SELECT * FROM table_2 WHERE id_user = '$ids' AND items = '$items'";
// lets do some foreach or while
// If I closed here the first query still open...
}
// If I close here
mysqli_close($con); // Am I closing both queries?
// Let get some more data
// Should I close my third query
mysqli_close($con);
// hasn't this been close already? which mean that the third query never got
the change to execute... correct?
所以正如你所看到的,在 MYSQL.com 中它告诉你如何关闭它,而 PHP.net 几乎是一样的,但它没有告诉你何时关闭它。
如果 a = 1 执行查询然后关闭它...
否则,如果 a = 5 执行另一个查询,然后关闭它,我认为这非常简单,但是如果我的一个查询继承另一个查询的信息怎么办...
如果 a = 1 进行查询
然后做一些事情和更多的事情以及大量的 IF
如果第二个查询的结果是 B 则执行另一个查询...依此类推...
也许我只是想太多了,那么还有其他方法来打开或关闭MySQLi
$con_one = conectTodb();
// queries
$con_one ->close();
如果你有3个查询,con_one,con_two,con_third,那么你必须一一关闭,比如con_one->close(),con_two->close(),con_third->close()...等等。 。 正确的?
我问这个是因为几个小时。之前我收到一个错误,告诉我与数据库的连接太多,因此无法处理信息......
事实证明这是服务器配置错误,这让我开始思考,如果这是真实情况我该怎么办!???如果用户正在购买东西然后 POW 该怎么办!!!连接太多,数据丢失……光是想到这一点就会带来一些噩梦
我不知道...我的大脑现在正在沸腾...有人可以吗?向我解释一下,如果没有什么要问的,请提供一些例子......
非常感谢!
PD。在本地主机上,最大连接数是 10(IIS、WAMP、XAMP)...在共享主机上,我认为是 25...