打开和关闭mysqli查询的正确方法 [英] A proper way to open and close mysqli queries

查看:84
本文介绍了打开和关闭mysqli查询的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单而又令人困惑的问题,特别适合像我这样的自学成才的人...
我已经阅读了PHP.NET和MYSQL.COM中的不同文档,它们都解释了如何打开和关闭它,但是对于这个问题(至少对我来说)并没有真正的帮助.

I have this simple yet confusing question, special for people that are self taught programmer like my self...
I have read different documentation in PHP.NET and MYSQL.COM, they all explain how to open and how closed it but for this question that doesn't really help, at least for me.

我的学习方式如下
我需要一个文件,例如dbconnection.php或db.php,名称不重要,但内容是我经常执行的操作.

The way that I've learn this is as follow
I require of a file to my conecction such as dbconnection.php or db.php the name doesn't mater but the content so here is what I always do...

<?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();
}

所以我的数据库已经连接好了,现在让我们取一些西瓜

So there is my conection to my DB, now lets fetch some watermelons

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

现在这是我的困惑...

Now here is my confusion...

        <?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几乎相同,但是它没有告诉您何时关闭它..

So as you can see, in MYSQL.com it tell you how to close it, and PHP.net is almost the same, but it doesn't tell you when to close it..

如果a = 1进行查询,然后将其关闭...
否则,如果a = 5进行另一个查询然后将其关闭,我认为这很简单,但是如果我的一个查询依赖于另一个查询的信息怎么办...

if a = 1 do a query then close it...
else if a = 5 do another query then close it, this is very simple I think, but what if one of my queries relay on the information from another query...

如果a = 1进行查询
然后做一些更多的事情和大量的IF
如果第二个查询的结果是B,则执行另一个查询...依此类推...

也许我只是想得太多,然后还有另一种打开或关闭MySQLi的方法

if a = 1 do a query
do some stuff and more stuff and a ton of IF's then
if the result from the second query was B do another query... and so on...

Maybe I'm just over-thinking it, then there is the other way to open or closed MySQLi

$con_one = conectTodb();
// queries
$con_one ->close();

如果您有3个查询con_one,con_two,con_three,则必须一个一个地关闭,例如con_one-> close(),con_two-> close(),con_three-> close()...等等在...正确吗?

if you have 3 queries, con_one, con_two, con_three, then you have to close one by one, like con_one->close(), con_two->close(), con_three->close()... and so on... correct?

我问这个是因为几个小时.以前,我有一个错误告诉我与数据库的连接太多,因此无法处理信息. 事实证明,这是服务器的错误配置,让我思考,如果那是真实情况,我该怎么办!如果用户先购买东西然后再购买战俘该怎么办! !!太多的连接,数据丢失...尽管如此,这将带来一些噩梦

I'm asking this because a few hrs. ago I had an error telling me that there was too many connections to the data base and for that it was not possible to process the information... it turnout it was a server miss-configuration, that got me thinking, what am I going to do if that was was a real situation!??? what if a user is buying something and then POW!!! too many connections, data lost... just the though of that will give some nightmares

我不知道...我的大脑现在正在发烫...可以有人吗!给我解释一下,如果问不多,请提供一些例子...

I don't know... my brain is steaming right now... can some one please! explain this to me if is not much to ask please provide some examples...

非常感谢您!

pd.在本地主机上,最大连接数是10(IIS,WAMP,XAMP)...在共享主机上,我认为是25 ...

pd. on a local host the maximum number of connections is 10(IIS, WAMP, XAMP)... on a share host I think is 25...

推荐答案

mysqli_close()关闭连接,而不关闭查询.仅当您知道当前请求不再需要该连接时,才应关闭该连接.如果未在代码中明确关闭连接,则连接将在PHP执行结束时自动关闭.

mysqli_close() closes a connection not a query. You should only close the connection if you know it will not be needed once again for the current request. The connection will automatically close at the end of the PHP execution if you don't explicitly close it in your code.

通常不需要显式关闭连接,并且显然有些特殊情况是您可能需要的,例如,如果脚本在使用连接后进行了一些巨大的处理,则在释放连接期间将是有益的.处理.

Usually there is no need to explicitly close the connection and obviously there are some special circumstances where you might want to, for example if your script does some huge processing after using the connection where it would be beneficial to free up the connection during the processing.

这篇关于打开和关闭mysqli查询的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆