dousi8931 2013-01-14 10:12
浏览 17

从ajax更改数据库以进行会话

I have an XML API that makes queries to a database. I also have a site that uses this XML API functions to get data from my database. I want to change the database accordingly to what the user selects on my site. When user selectes a region code from a select item i i call a javascript action that calls an xml api call (php file) to change my db. DB changes only for the current call but i want to change it for the current session. In my XML API i have a config file that creates a class Mysql an initiates a connection to the database. That config file is require_once to every php file for the API. The class is like that:

class Mysql 
{ 
    private $host = DB_HOST; 
    private $username = DB_USER; 
    private $password = DB_PASS; 
    private $database = DB_NAME; 
    private $connection; 
    private $selection; 


    public function connect() 
    { 
       $connection = mysql_connect($this->host,$this->username,$this->password) or die('Cannot connect to db('.$this->database.'): '. mysql_error()); 
            $selection = mysql_select_db($this->database) or die('Cannot select db: '. mysql_error()); 
            return $connection;
    } 

    public function connect_close() 
    { 
       mysql_close();
    }

        public function change_DB($dbname){
            //$this->connect_close();
            $this->database = $dbname;
        }

        public function getDB(){
            return $this->database;
        }
}

To change the database , i tried to use mysql_select_db in change_DB function but it changed just for the spicific call. I then tried to change the private var database ,close the connection and reopen it with the new database,but nothing. Can someone give me an advise? I've done my homework.

  • 写回答

1条回答 默认 最新

  • duanrang2627 2013-01-14 17:26
    关注

    One of the reasons for this kind of behavior could be that your $object is getting created as a new object on every call. Perhaps your PHP MySQL is not configured to create persistent connections. I can only tell if that's the case here after looking at your php.ini and how you are managing this object across your various pages.

    You'd need to make sure that your PHP MySQL is set up with mysql.allow_persistent = 1. You can read more about this setting here:

    http://php.net/manual/en/mysql.configuration.php#ini.mysql.allow-persistent

    http://php.net/manual/en/features.persistent-connections.php

    Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题