play_ Posted March 1, 2007 Share Posted March 1, 2007 I realize this should be in the mysql forum but i need an answer quick because of the contest due tomorrow. This works on my localhost but not web server. On my machine: php5, apache2.0 Web server: php4.3, not sure the apache version. It's not 2.0 though. Problem: I have an SQL class. here it is (well, the beginnnig of it):\ class mysql { function __construct() { $this->host = 'secret'; $this->user = 'secret'; $this->password = 'secret'; $this->db = 'secret'; $this->querycount = 0; $this->connect(); // call connect function below } // Connect and select methods function connect() { $this->linkid = mysql_connect($this->host, $this->user, $this->password); if(!$this->linkid) { echo 'Could not connect to the database.'; } echo $this->linkid; if(! @mysql_select_db($this->db, $this->linkid) ) { echo 'Could not select database'; } } now, from test.php, i do: include('./includes/classes.php'); $sql = new mysql(); if(! $sql->connect() ) { echo 'Not Coneected-----------'; } else { echo 'connected!++++++++++++'; } When i run the script, i get: Warning: mysql_connect(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in path/to/includes/classes.php on line 140 Could not connect to the database. Could not select databaseNot Coneected----------- However, this works (Not a class): define ('DB_USER', 'secret'); define ('DB_PASSWORD', 'secret'); define ('DB_HOST', 'secret'); define ('DB_NAME', 'secret'); $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('ooops.. Problem:<br> '.mysql_error()); mysql_select_db (DB_NAME) OR die (mysql_error()); Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/40652-solved-mysql-wont-connect/ Share on other sites More sharing options...
MadTechie Posted March 1, 2007 Share Posted March 1, 2007 change <?php function __construct() { $this->host = 'secret'; $this->user = 'secret'; $this->password = 'secret'; $this->db = 'secret'; $this->querycount = 0; $this->connect(); // call connect function below ?>} to <?php function __construct() { var $host = 'secret'; var $user = 'secret'; var $password = 'secret'; var $db = 'secret'; var $querycount = 0; $this->connect(); // call connect function below } ?> Quote Link to comment https://forums.phpfreaks.com/topic/40652-solved-mysql-wont-connect/#findComment-196660 Share on other sites More sharing options...
play_ Posted March 1, 2007 Author Share Posted March 1, 2007 Says change <?php function __construct() { $this->host = 'secret'; $this->user = 'secret'; $this->password = 'secret'; $this->db = 'secret'; $this->querycount = 0; $this->connect(); // call connect function below ?>} to <?php function __construct() { var $host = 'secret'; var $user = 'secret'; var $password = 'secret'; var $db = 'secret'; var $querycount = 0; $this->connect(); // call connect function below } ?> e error: parse error, unexpected T_VAR in /path/to/includes/classes.php on line 118 Line 18: var $host = 'secret'; server's on php 4.3. Just tested this. even if i do this: class mysql { // Connect and select methods function connect() { $this->linkid = mysql_connect('secret', 'secret', 'secret'); if(!$this->linkid) { echo 'Could not connect to the database.'; } echo $this->linkid; if(! @mysql_select_db('secret', $this->linkid) ) { echo 'Could not select database'; } } It won't work. Error message: Resource id #3 Not Coneected----------- Quote Link to comment https://forums.phpfreaks.com/topic/40652-solved-mysql-wont-connect/#findComment-196661 Share on other sites More sharing options...
play_ Posted March 1, 2007 Author Share Posted March 1, 2007 Found the problem. I have to return $this->linkid in the connect() method. Thanks for the help MadTechie. Quote Link to comment https://forums.phpfreaks.com/topic/40652-solved-mysql-wont-connect/#findComment-196667 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.