Jump to content

There Is A Problem With Your Queryno Database Selected


50r

Recommended Posts

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

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"]);
?>

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.

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.

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

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.