简体   繁体   中英

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

In $e you have reason why you can't connect to database. 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:

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

^ use mysqli_ instead mysql_

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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