Jump to content

database include question


yellowepi

Recommended Posts

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/49708-database-include-question/
Share on other sites

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

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

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));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.