PHP MySQL从Require()文件获取连接

PHP MySQL从Require()文件获取连接

问题描述:

I usually am immersed in the Microsoft Stack but dabble in PHP from time to time. A long standing question I've had with PHP that I've never seem to be able to find the answer to is how do you apply your already declared require("dbConnect.php") database connection to your mysql_query()? For clarification please see my code example below:

        require("dbConnect.php"); 

        $db_host = 'localhost';
        $db_user = 'UserName';
        $db_pwd = 'Password';

        $database = 'DbName';
        $table = 'tblQuote';

        if (!mysql_connect($db_host, $db_user, $db_pwd))
            die("Can't connect to database");

        if (!mysql_select_db($database))
            die("Can't select database");

        // sending query
        $result = mysql_query("SELECT QuoteID, FirstName, LastName, PhoneNumber, Email, QuoteDate FROM tblQuote ORDER BY QuoteDate DESC");
        if (!$result) {
            die("Query to show fields from table failed");
        }

        $fields_num = mysql_num_fields($result);

So in looking at this you can see the standard require() declaration at the top... which already holds my connection info. But every single MySQL Query example I've ever found always creates it's own connection... which I get for demonstration purposes... but I've never been able to figure out how I can use my already existing connection thereby bypassing rewriting the exact same connection info over and over again when it comes to writing queries. I know for you PHP developers this question is like 101 but I've not been able to find an answer to this seemingly basic question... admittedly I may be asking the question wrong so any help would be appreciated!

我通常沉浸在Microsoft Stack中,但不时涉猎PHP。 我用PHP得到的一个长期存在的问题是,我似乎无法找到答案,是如何应用已经声明的 require(“dbConnect.php”) code>数据库连接到 你的mysql_query()? 有关说明,请参阅下面的代码示例: p>

  require(“dbConnect.php”);  
 
 $ db_host ='localhost'; 
 $ db_user ='UserName'; 
 $ db_pwd ='密码'; 
 
 $ database ='DbName'; 
 $ table ='tblQuote'; \  n 
 if(!mysql_connect($ db_host,$ db_user,$ db_pwd))
 die(“无法连接到数据库”); 
 
 if if(!mysql_select_db($ database))
 die(“ 无法选择数据库“); 
 
 //发送查询
 $ result = mysql_query(”SELECT QuoteID,FirstName,LastName,PhoneNumber,Email,QuoteDate FROM tblQuote ORDER BY QuoteDate DESC“); 
 if(!)  $ result({
 die(“查询以显示表中的字段失败”); 
} 
 
 $ fields_num = mysql_num_fields($ result); 
  code>  pre> 
 
 因此,在查看此内容时,您可以在顶部看到标准的require()声明...它已经保存了我的连接信息。 但是我发现的每一个MySQL Query例子总是创建它自己的连接...我出于演示目的...但我从来没有弄清楚如何使用我已经存在的连接从而绕过重写 在编写查询时,一遍又一遍地完全相同的连接信息。 我知道你的PHP开发人员这个问题就像是101但是我无法找到这个看似基本的问题的答案...诚然,我可能会问这个问题是错的,所以任何帮助都会受到赞赏! p> \  n  div>

From the PHP documentation: http://php.net/manual/en/function.mysql-query.php

mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )

link_identifier The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments.

So since you've already created one in your dbConnect.php, the one you just made will be used (It won't create a new one for every query). To pass it explicitly into your mysql_query function call, you can return the MySQL resource that was returned from your mysql_connect call like so:

dbConnect.php

return mysql_connect(....);

Then in the code you pasted above:

$mysql_conn = require('dbConnect.php');
...
$result = mysql_query('...', $mysql_conn);

Then you will explicitly have the connection and pass it to your query - there will be no mistaking it, regardless of how large your codebase becomes. When you require the file, you'll have access to the connection variable, but in the above example, how you get the connection is more semantically clear.


Also, notice that this function has been deprecated in PHP>=5.5, so you'll want to use PDOs or MySQLi which have future support.

Hope this helps!