English 中文(简体)
我是否必须每当我与PDO提出质询时,把我的 d子连接器存档?
原标题:Do I have to include my db connector file each time I run a query with PDO?
  • 时间:2011-06-03 02:49:29
  •  标签:
  • php
  • mysql
  • pdo

我先把以下东西节省到我的台。 网址

$host =  localhost ;
$username =  root ;
$pass =  mypass ;

try {
    $dbh = new PDO("mysql:host=$host;dbname=horh_new", $username, $pass);
} catch (PDOException $e) {
    echo $e->getMessage();
}

是什么最佳方法把这一点纳入我的文字? 现在,我开始在数据库中添加一个用户的功能,而数据库中也有这样的内容:

function insertUser($username) {
include("dbConnect.php"); // i d like to not have to include this each time
$query = $dbh->prepare("INSERT INTO users (username) values (?)");

$query->bindParam(1, $username);
$query->execute();
}

显然,我的职能越多,我就越需要包括这一条线。 php档案。 相反,我是否应该把它列入一个有私人变量的类别,即用户名称、影子和客套(因此在使用相同变数时没有冲突)。

Then include the class once in the header of my script instead of in every single function? I m quite new to PDO, so I d like to figure out the most efficient method of doing this. Thanks.

最佳回答

你每次都提到这一点,你都会在这一职能范围内打开新的联系。 简言之,在您的首创网页上刊登这一文件,并在全球范围内把你与每一项职能联系起来。 不要不止一次地列入这一档案,否则,你会打开更多的联系,造成不必要的减缓。

指数。 php例:

<?php
include("dbConnect.php");
include("somefunctions.php");
// more stuff, front-facing web stuff, using stuff in somefunctions.php

somefunctions.php:

<?php
function doSomething()
{
    global $dbh;
    $query = $dbh->prepare("INSERT INTO users (username) values (?)");
   // etc.
}
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签