简体   繁体   English

多个mysql_connect语句互相取消

[英]Multiple mysql_connect statements canceling each other out

I am trying to code an ajax chat application using php and my sql. 我正在尝试使用php和我的sql编写一个ajax聊天应用程序。 I have a file called connect.php that looks like this: 我有一个名为connect.php的文件,如下所示:

mysql_connect("host", "username", "password");
mysql_select_db("database name");

I have another file called Core.php that needs this same connection to the database in order to get the chat messages from my database. 我有另一个名为Core.php的文件,它需要与数据库相同的连接才能从我的数据库中获取聊天消息。 Core.php looks like this: Core.php看起来像这样:

class Core {
protected $db, $result;
private $rows;

public function __construct() {
    $this->db = new mysqli('host', 'username', 'password','database name') or die('Connection Failure');
}
}

How do I set $this->db to the same connection I already have open? 如何将$ this-> db设置为我已打开的同一连接?

As it is right now connect.php is called in my init.php file before Core.php and results in the chat messages not showing up in the box. 就像现在一样,在Core.php之前的init.php文件中调用了connect.php,导致聊天消息没有显示在框中。 If I comment out the call for connect.php then the chat messages display but all of the users login data can't be accessed and the user gets logged out. 如果我注释掉connect.php的调用,则会显示聊天消息,但无法访问所有用户登录数据,并且用户将被注销。 Can anyone help me with this? 谁能帮我这个? It's driving me crazy 这让我疯狂

EDIT: This is my init.php file where core.php and connect.php are called 编辑:这是我的init.php文件,其中调用core.php和connect.php

ob_start();
session_start();
error_reporting(0);

$current_file = explode('/',$_SERVER['SCRIPT_NAME']);
$current_file = end($current_file);

require 'core/database/connect.php';
require 'functions/general.php';
require 'functions/users.php';
require 'classes/Core.php';

I'm not 100% sure what you're doing so this is best guess answer... For one, how are you assigning the already open connection? 我不是100%肯定你正在做什么所以这是最好的猜测答案......首先,你如何分配已经打开的连接?

//connect.php
<?php
    $conn = mysql_connect("$host", "$u", "$p");
    mysql_select_db("$db");
?>

<?php
include 'connect.php';

class Core
{
    private $conn;

    function __construct()
    {
        global $conn;
        $this->conn =& $conn;
    }
}
?>
public function __construct() {
      $this->db = new mysqli('$host', '$u', '$p','$db') or die('Connection Failure');
 }

this code will not run, be sure u have used "$host"/"$u"..etc instead of '$host' or '$u' 这段代码不会运行,请确保你使用“$ host”/“$ u”...等而不是'$ host'或'$ u'

php parser will parse it as a string not a variable inside the quotation. php解析器会将其解析为字符串而不是引号内的变量。

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

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