Welcome Guest, Not a member yet? Register   Sign In
Database doesn't work on server but works locally
#1

[eluser]mojitoo[/eluser]
Hey!

I need som help. This code works perfect when I execute it locally but it doesn't work when I upload it to a server.
You're able to see that I've added some code to the database that tells me that the connection is OK!

database.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

/* echo '<pre>';
     print_r($db['default']);
  echo '</pre>';

  echo 'Trying to connect to database: ' .$db['default']['database'];
  $dbh=mysql_connect
  (
    $db['default']['hostname'],
    $db['default']['username'],
    $db['default']['password'])
    or die('Cannot connect to the database because: ' . mysql_error());
    mysql_select_db ($db['default']['database']);

    echo '<br />   Connected OK:'  ;
    die( 'file: ' .__FILE__ . '--&gt; Line: ' .__LINE__); */


/* End of file database.php */
/* Location: ./application/config/database.php */

controller, site.php
Code:
&lt;?php

class Site extends CI_Controller {

function index()
{
  $data = array();

  if($query = $this->site_model->get_records())
  {
   $data['records'] = $query;
  }

  $this->load->view('options_view', $data);
}
}

model, site_model.php
Code:
&lt;?php

class Site_model extends CI_Model {

function get_records()
{
  // data = table name in db
  $query = $this->db->get('data');

  var_dump($query);

  return $query->result();
}
}

You're able to see a var_dump in my model that returns bool(false)
I also get this error:
Fatal error: Call to a member function result() on a non-object in /home/site/public_html/test/application/models/site_model.php on line 12

Please try to help me with this, it's really frustrating since it works locally.
#2

[eluser]InsiteFX[/eluser]
I would check your hosting providers versions of MySQL and PHP
#3

[eluser]mojitoo[/eluser]
mysql: 5.0.95
php: 5.2.17

Any help?
#4

[eluser]pbflash[/eluser]
Did you update the username, password, and database name?
#5

[eluser]aquary[/eluser]
I'm guessing you are using an older version of CI? I remember the old version would return a FALSE when the query return empty result....

Either upgrade the CI version or check of the $query is FALSE should fix the problem...

hmm, read it again and it seems to be different. It was about "$query->result() return a FALSE" not "$query is FALSE". Anyway, what are your CI version? Did you also dump database data, or only structure? What happen if you run the query manually via mysql_query()? do you get any result?

Edit: just finish checking the mysql driver, the only case (as of CI 2.1) where you would get FALSE from the $this->db->get() is when you have error in the query somewhere... might be not useful, but make it as a note here Tongue
#6

[eluser]mojitoo[/eluser]
Hey again!

I found the problem with help of my host. My database user had somehow been disconnected from this table.
Thanks for all your help!




Theme © iAndrew 2016 - Forum software by © MyBB