mysqli - No database selected

Using mysqli on my localhost and it works as intended.

I upload it to the server with changed database name, username and password and it comes back “No database selected”

It is querying data fine from the database as I can populate tables with already existing data from the database, but I cannot Insert, Delete, Update. I always get “No database selected”

[php]<?php
class MySQLHandler {

// Offline Database connect
var $DATABASE = ‘fear’;
var $USERNAME = ‘root’;
var $PASSWORD = ‘’;
var $SERVER = ‘localhost’;

var $SHOW_ERRORS = true;

// Do not change the variables below
var $CONNECTION;
var $FILE_HANDLER;
var $ERROR_MSG = ‘’;

function init() {
if ($this->OpenConnection()) {
return true;
} else {
return false;
}
}

function OpenConnection()    {
  $conn = new mysqli($this->SERVER,$this->USERNAME,$this->PASSWORD);

    if ((!$conn) || (!mysqli_select_db($conn,$this->DATABASE))) {
  $this->ERROR_MSG = "\r\n" . "Unable to connect to database ";
  $this->debug();
  return false;
} else {
      $this->CONNECTION = $conn;
      return true;
}

}


function CloseConnection() {
  if (mysqli_close($this->CONNECTION)) {
  return true;
} else {
  $this->ERROR_MSG = "\r\n" . "Unable to close database connection";
  $this->debug();
  return false;
}
}

function debug() {
if ($this->SHOW_ERRORS) {
echo $this->ERROR_MSG;
}
}

function Insert($sql) {
    if ((empty($sql)) || (!explode("^insert",$sql)) || (empty($this->CONNECTION))) {
  $this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not an INSERT";
  $this->debug();
  return false;
} else {
      $conn = $this->CONNECTION;
      $results = mysqli_query($conn,$sql);
      if (!$results) {
    $this->ERROR_MSG = "\r\n" . mysqli_error();
    $this->debug();
    return false;
  } else {
        $result = mysqli_insert_id();
        return $result;
  }
}
}


function Select($sql)    {
    if ((empty($sql)) || (!explode("^select",$sql)) || (empty($this->CONNECTION))) {
  $this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not a SELECT";
  $this->debug();
  return false;
} else {
      $conn = $this->CONNECTION;
      $results = mysqli_query($conn,$sql);
      if ((!$results) || (empty($results))) {
    $this->ERROR_MSG = "\r\n" . mysql_error();
    $this->debug();
    return false;
  } else {
    $i = 0;
    $data = array();
    while ($row = mysqli_fetch_array($results)) {
        $data[$i] = $row;
        $i++;
    }
    mysqli_free_result($results);
    return $data;
  }
}
}


function Update($sql)    {
    if ((empty($sql)) || (!explode("^update",$sql)) || (empty($this->CONNECTION))) {
  $this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not an UPDATE";
  $this->debug();
  return false;
} else {
      $conn = $this->CONNECTION;
      $results = mysqli_query($conn,$sql);
      if (!$results) {
    $this->ERROR_MSG = "\r\n" . mysql_error()." - " . date('H:i:s');
    $this->debug();
    return false;
  } else {
        return mysqli_affected_rows($conn);
  }
}
}

function Replace($sql) {
    if ((empty($sql)) || (!explode("^replace",$sql)) || (empty($this->CONNECTION))) {
  $this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not a REPLACE";
  $this->debug();
  return false;
} else {
      $conn = $this->CONNECTION;
      $results = mysqli_query($conn,$sql);
      if (!$results) {
    $this->ERROR_MSG = "\r\n" . "Error in SQL Statement : ($sql)";
    $this->debug();
    return false;
  } else {
        return true;
  }
}
} 

function Delete($sql)    {
    if ((empty($sql)) || (!explode("^delete",$sql)) || (empty($this->CONNECTION))) {
  $this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not a DELETE";
  $this->debug();
  return false;
} else {
      $conn = $this->CONNECTION;
      $results = mysqli_query($conn,$sql);
      if (!$results) {
    $this->ERROR_MSG = "\r\n" . mysql_error();
    $this->debug();
    return false;
  } else {
        return true;
  }
}
}

function Query($sql)    {
    if ((empty($sql)) || (empty($this->CONNECTION))) {
  $this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code>";
  $this->debug();
  return false;
} else {
      $conn = $this->CONNECTION;
      $results = mysqli_query($conn,$sql);
      if (!$results) {
    $this->ERROR_MSG = "\r\n" . mysql_error();
    $this->debug();
    return false;
  } else {
        return true;
  }
}
}

}
?>[/php]

With $sql being
[php]$this->sql->update(“UPDATE battle_attendance SET status = '”.$status."’ WHERE battleid = ‘".$id."’ AND user_id = ‘".$user_id."’ ");[/php]
Any ideas?

[php]$conn->select_db($this->DATABASE);[/php]

Although i don’t know why you wouldn’t do:
[php]$conn = new mysqli($this->SERVER,$this->USERNAME,$this->PASSWORD,$this->DATABASE);[/php]
Unless of course you are changing databases.

Hope that helps,
Red :wink:

Yeah, I’ve tried it all ways.

It’s weird because I can query data out of the database, so connecting, selecting the database and pulling data out is fine, but when it comes to altering the database it errors.
Makes me think it is a server side issue? I have checked software versions and user privileges and they are all fine.
Also it works on localhost fine. I have done some cleanup of the code whilst I have had this error and various different solutions.

when “SHOW grants FOR ozsofcom_lothop@localhost” I get #1044 - Access denied for user ‘ozsofcom’@‘localhost’ to database ‘mysql’. Although all my privileges are set correctly in cpanel. Can I grant privileges in sql instead of using cpanel?

Try this with your connection parameters.

[php]<?php
$con = mysqli_connect(“YOURhost”,“YOUR_user”,“YOUR_password”,“YOUR_db”);

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else{
echo ‘CONNECTED’;
}
?>[/php]

That works fine and I can update from that, so it must be somewhere else in my code. Frustrating. I’ll have to keep hunting. Thanks

I once was having a similar problem and after I took a break a light bulb turn on. I can’t remember the exact table name, but what happen was I had a table name called Users. It worked fine on the local server using users, but when I uploaded it to the remote server it wouldn’t So just for the heck of it I modified the table from Users to users (I like using lower case) on the remote server. Low and behold it work. This might be a case sensitivity problem?

I have a love, hate relation with issues like this, I hate the frustration, I love the epiphany moments. I have worked in collaboration with another coder so perhaps there is something I missed in his code that is having an effect. I will post the solution when I come across it

I have this problem on my live server right now using filenames.
If i call something Abc.php or image.JPG on my local server, i have issues on the live server not finding the files so nowdays i always use lowercase.

Very good shout [member=57087]Strider64[/member] 8)

Red :wink:

Exactly why you should always use lower case. Windows is not case sensitive. Linux / UNIX is.

Windows makes no distinction between upper and lower case. $!/^$& Microsoft!

Sponsor our Newsletter | Privacy Policy | Terms of Service