user6265109 user6265109 - 4 months ago 14
MySQL Question

Getting no database selected message. PHP,mysql

I have a database at local server. I am trying to access this database. I have table of venueTypeMaster in the database. I tried to get all the venuetypes from database. But when I run the script message I get is - 'No database selected'.

I have a class in which I have created two functions in php script. For createVenue it dose not send any message and the query has been executed successfully. But for getVeunueTypes it is showing this message. Connection for both is same.

DB Script:

<?php
include_once("config.php");
include_once("exceptions.php");
class DB {
static $con;

public static function getConnection() {
try {
if ( DB::$con == null ) {
DB::$con = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD);
if ( DB::$con) {
$status = mysqli_select_db(DB::$con,DB_NAME);
if (! $status ) throw new DBSelectionExcpetion(mysqli_error());
return DB::$con;
} else {
$e = new DBConnectionException(mysqli_error());
throw $e;
}
}
return DB::$con;
} catch(DBConnectionException $e) {
throw $e;
} catch(DBSelectionExcpetion $de) {
throw $de;
}
return null;
}
?>


venueTypes script

<?php
include("exceptions.php");
include("DB.php");
include("config.php");

class VenueTypes {

public function getVenueTypes() {
try {
$con = DB::getConnection();

$query = "select * from venueTypeMaster";

$rs = mysql_query($query) or die (json_encode(array("result"=>-1, "message"=>mysql_error())));
$n = mysql_num_rows($rs);

$vt = array();

if ( $n > 0 ) {

while ( $row = mysql_fetch_assoc($rs)) {
$vt[] = $row;
}

$result = array("result"=>1, "message"=>"success", "venueTypes" => $vt);
return json_encode($result);
} else {
$result = array("result"=>-1, "message"=>"Venue types list is empty");
return json_encode($result);
}
} catch(DBConnectionException $e) {
$result = array("result"=>-1, "message"=> $e -> getMessage());
return json_encode($result);
}
return null;
}

public function createVenueType($fields) {

try {

$required = array("venuetype", "active", "entry_by", "entry_date", "entry_time", "last_modify_date", "ip_addr","venue_name");
$actual = array_keys($fields);
$intersection = array_intersect($required, $actual);

$n1 = sizeof($required);
$n2 = sizeof($intersection);

if ( $n1 != $n2 ) {
throw new MissingParameterException();
}
$con = DB::getConnection();

$keys = array_keys($fields);
$values = array_values($fields);

$columns = implode(",", $keys);

$n = sizeof($values);
for($i=0;$i<$n; $i++) {
$values[$i] = "'" . mysqli_real_escape_string($con,$values[$i]) . "'";
}

$columnValues = implode(",", $values);

$query = "insert into venuetypemaster($columns) values($columnValues)";

mysqli_query($con,$query) or die (json_encode(array("result"=>-1, "message"=>mysqli_error())));
$af = mysqli_affected_rows($con);
if ( $af == 1 ) {
$venuetypeId = mysqli_insert_id($con);
$result = array("result"=>1, "message"=>"success", "venuetypeId" => $venuetypeId);
return json_encode($result);
} else {
$result = array("result"=>-1, "message"=>"Failed to register");
return json_encode($result);
}
} catch(DBConnectionException $e) {
$result = array("result"=>-1, "message"=> $e -> getMessage());
return json_encode($result);
}
return null;
}
?>


getVenueType script

<?php

header("Content-type: application/json");

if ( $_SERVER['REQUEST_METHOD']=='GET') {

include_once ("../include/VenueTypes.php");


try {

$v = new VenueTypes();
$response = $v -> getVenueTypes();

//$response is null means something went wrong as a result we got null result from above statement

if ( $response == null ) {
$response = json_encode(array("result" => -2, "message" => "Empty result"));
echo $response;
} else {
echo $response;
}
} catch(Exception $e) {
$result = array("result" => -1, "message" => $e -> getMessage());
echo json_encode($result);
}
} else {
$result = array("result" => -3, "message" => "Unsupported method : " . $_SERVER['REQUEST_METHOD']);
echo json_encode($result);
}
?>


Can anyone help please.. Thank you..

Answer

The problem is that you are mixing between the mysqli_xxx() and mysql_xxx() functions. These two libraries are not compatible with each other; you must only use one or the other throughout your code.

Since the mysql_xxx() functions are obsolete and deprecated, that means you should only use mysqli_xxx().

  1. Change all occurrences of mysql_ in your code to mysqli_.

  2. You will also need to make other changes to these lines of code -- notably this will include adding the DB reference variable (ie the variable returned by getConnection()), but some other syntax changes may also be necessary.