50r Posted November 7, 2012 Share Posted November 7, 2012 Now i think its one of those days where you get tired and starting seeing thing the other way round. this is simple i also know it is but believe me it has taken me quite some time trying it on my own and i tought maybe some can take a quick look for me in this code. <?php //this is the database class require_once("config.php"); class DatabaseConnection{ private $connection; function __construct(){ $this -> dbconnect(); } private function dbconnect(){ $this -> connection = mysql_connect(DB_HOST,DB_USER,DB_PASS); if(!$this -> connection){ die("There is a problem with the database connection" . mysql_error()); } } private function db_select(){ $db = mysql_select_db(DB_NAME, $this->connection); if(!$db){ die("There is a problem with the database". mysql_error()); } } public function sql($sql){ $results = mysql_query($sql); $this -> confirm_query($results); return $results; } private function confirm_query($results){ if(!$results){ die("There is a problem with your query" . mysql_error()); } } } $database = new DatabaseConnection(); ?> <?php require_once("../includes/database.php"); if(isset($database)){echo "true";} else{echo "false ";} //echo "<br>". $database->mysql_prep("jeff's website's rank is number one now in google insn't it? haha a>"); $sql = ("INSERT into tbl_user (user_name, user_password) VALUES('jtest', '*****') "); $database -> sql($sql); ?> this is the error Quote Link to comment https://forums.phpfreaks.com/topic/270382-there-is-a-problem-with-your-queryno-database-selected/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 7, 2012 Share Posted November 7, 2012 Where exactly are you calling your ->db_select() method? Quote Link to comment https://forums.phpfreaks.com/topic/270382-there-is-a-problem-with-your-queryno-database-selected/#findComment-1390723 Share on other sites More sharing options...
50r Posted November 7, 2012 Author Share Posted November 7, 2012 private function db_select(){ $db = mysql_select_db(DB_NAME, $this->connection); if(!$db){ die("There is a problem with the database". mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/270382-there-is-a-problem-with-your-queryno-database-selected/#findComment-1390741 Share on other sites More sharing options...
trq Posted November 7, 2012 Share Posted November 7, 2012 That is its definition. Where are you calling it? The error suggests that you are not. Quote Link to comment https://forums.phpfreaks.com/topic/270382-there-is-a-problem-with-your-queryno-database-selected/#findComment-1390745 Share on other sites More sharing options...
50r Posted November 7, 2012 Author Share Posted November 7, 2012 from the config file <?php $config = array(); $config["db_host"] = "localhost"; $config["db_user"] = "root"; $config["db_pass"] = ""; $config["db_name"] = "bookshop"; define(DB_HOST,$config["db_host"]); define(DB_USER,$config["db_user"]); define(DB_PASS,$config["db_pass"]); define(DB_NAME,$config["db_name"]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/270382-there-is-a-problem-with-your-queryno-database-selected/#findComment-1390840 Share on other sites More sharing options...
Muddy_Funster Posted November 7, 2012 Share Posted November 7, 2012 I really don't see why you would even consider building a variable $config array to just go and directly define the values as constants anyway. It's totaly pointless. just define the string literals from the beginning. That said, my first guess at the problem would be that you are doing mysql_select_db() inside a private function, without ever returning anything back out from it, so the rest of the class my not have any knowledge of the database being selected. Quote Link to comment https://forums.phpfreaks.com/topic/270382-there-is-a-problem-with-your-queryno-database-selected/#findComment-1390846 Share on other sites More sharing options...
PFMaBiSmAd Posted November 7, 2012 Share Posted November 7, 2012 Since you are not calling your ->db_select() method any where in your code, you haven't selected a database for the queries to operate on, ergo the error message you are getting. If you were calling it, the database selection would be specific to the database connection you are supplying to the mysql_select_db() statement. Quote Link to comment https://forums.phpfreaks.com/topic/270382-there-is-a-problem-with-your-queryno-database-selected/#findComment-1390870 Share on other sites More sharing options...
50r Posted November 9, 2012 Author Share Posted November 9, 2012 I really don't see why you would even consider building a variable $config array to just go and directly define the values as constants anyway. It's totaly pointless. just define the string literals from the beginning. That said, my first guess at the problem would be that you are doing mysql_select_db() inside a private function, without ever returning anything back out from it, so the rest of the class my not have any knowledge of the database being selected. yes its a private function would that be the problem Quote Link to comment https://forums.phpfreaks.com/topic/270382-there-is-a-problem-with-your-queryno-database-selected/#findComment-1391334 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2012 Share Posted November 9, 2012 You are not calling your class method. You have been told this a few times now. You have defined your class method, but have not called it. Your config file has nothing to do with the problem. Quote Link to comment https://forums.phpfreaks.com/topic/270382-there-is-a-problem-with-your-queryno-database-selected/#findComment-1391354 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.