简体   繁体   English

在变量中设置mysql表

[英]Setting a mysql table in a variable

Okay so I'm using this code to store tables in a variable: 好的,所以我正在使用以下代码将表存储在变量中:

<?php
$host = "localhost";
$user  = "x";
$password =  "x";

try {
    $account = new PDO("mysql:host=$host;dbname=account", $user, $password);
} catch(PDOException $e) {
    die('connection cant be made');
}

try {
    $player = new PDO("mysql:host=$host;dbname=player", $user, $password);
} catch(PDOException $e) {
    die('connection cant be made');
}

and everything I do I get "connection can't be made". 我得到的所有信息都是“无法建立连接”。 So I found another code and I switched to it and everything works. 因此,我找到了另一个代码,然后切换到该代码,一切正常。

<?php
$host = "localhost";                    // Ip-ul de la server 
$user= "xx";                            // User-ul de conectare la database
$password = "xx";                       // Parola de conectare la database
mysql_connect($server, $user, $password) or die(mysql_error());
mysql_select_db('account');
mysql_set_charset('utf8');

?> but now I have no db assigned to $account and $player and I can't use my site properly. ?>但现在我没有为$ account和$ player分配任何数据库,因此我无法正常使用我的网站。 Ideas? 有想法吗?

In $e you have reason why you can't connect to database. $e您有理由无法连接数据库。 Catch exception and do nothing with it... 捕获异常,不执行任何操作...

<?php
define('__DEBUG', true);


try {
    $account = new PDO("mysql:host=$host;dbname=account", $user, $password);
} catch(PDOException $e) {
    if(__DEBUG) {
        print $e->getMessage();
    }
    die('connection cant be made');
}

for use 2 connections in (not so^) old way style: 使用2种连接方式(不是so ^)的旧样式:

$con1 = mysqli_connect();
$con2 = mysqli_connect();
mysqli_query('query', $con1);
mysqli_query('query', $con2);

^ use mysqli_ instead mysql_ ^使用mysqli_代替mysql_

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM