yellowepi Posted May 2, 2007 Share Posted May 2, 2007 I am attempting to connect to a database using this script, but am getting a message that states no data base selected. Here's the code, I have tried calling it using a funtion, but so far no dice. Database name is test. <?php class dbControl { //set local variables var $dbhost = "localhost:8889"; var $dbuser = "root"; var $dbpass = "root"; var $dbname = "test"; //connect to database and select a database function dbConSel ($dbhost, $dbuser, $dbpass, $dbname) { //connect $db = mysql_pconnect($dbhost,$dbuser,$dbpass); if (!$db) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname,$db) or die(mysql_error()); } //Select Table function selTable ($mysqlquery) { $result = mysql_query($mysqlquery) or die(mysql_error()); } } $dataconnect = new dbControl(); $dataconnect -> dbConSel ('localhost:8889, root, root, test'); $dataconnect -> selTable ("SELECT * FROM store_items WHERE cat_id=3"); echo $result; ?> Quote Link to comment Share on other sites More sharing options...
yellowepi Posted May 2, 2007 Author Share Posted May 2, 2007 Sorry, its not an include question now, because I combined the files to try to figure out the problem. The include works fine. Quote Link to comment Share on other sites More sharing options...
yellowepi Posted May 2, 2007 Author Share Posted May 2, 2007 After some more experimenting I solved part of the problem. I am now getting this error though. Warning: mysql_pconnect(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /Applications/MAMP/htdocs/TacoHTMLEditTemp.php on line 15 Could not connect: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) How can you change the socket you are using. I copy and pasted the connection code from another file that works too. This is the socket that it should use:/Applications/MAMP/tmp/mysql/mysql.sock Quote Link to comment Share on other sites More sharing options...
dj-kenpo Posted May 2, 2007 Share Posted May 2, 2007 I always just use the following and it works fine, but for all I know it's full of security holes. at the top of my page I say //set database host, usually localhost $DBHost = "localhost"; //set datbase user $DBUser = "root"; //set database pass $DBPass = "root"; //set database name $DBName = "test_table"; mysql_connect("$DBHost", "$DBUser", "$DBPass"); mysql_select_db("$DBName"); then I do whatever sql queries, php I need. then I say at the bottom mysql_close() try that Quote Link to comment Share on other sites More sharing options...
cluce Posted May 2, 2007 Share Posted May 2, 2007 this is all I use... //connect to server and select database $mysqli = mysqli_connect("localhost", "username", "password", "database"); //create and issue the query $sql = "SELECT......................FROM.............. $result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli)); Quote Link to comment 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.