0

I'm trying to set up a codeigniter web app I just recently finished on a server. I wrote it locally on my own computer. However when I try to login I get these two errors.

A PHP Error was encountered

Severity: Warning

Message: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO)

Filename: mysql/mysql_driver.php

Line Number: 319



A PHP Error was encountered

Severity: Warning

Message: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established

Filename: mysql/mysql_driver.php

Line Number: 319

Anyone know what the problem is?

ninjacoder
  • 1,464
  • 2
  • 10
  • 13
  • Are you connecting to the DB after or before the call to this function? – Jan Hančič Nov 28 '12 at 09:54
  • 1
    I'm assuming if you fix your database connection string, this will go away – asprin Nov 28 '12 at 09:54
  • This function uses the last database connection handle/resource, if any, or the one declared in the second parameter of this function. Make sure it's already connected before using this function. – inhan Nov 28 '12 at 09:56
  • The answer to your question is in this post: [http://stackoverflow.com/questions/1162491/alternative-to-mysql-real-escape-string-without-connecting-to-db][1] [1]: http://stackoverflow.com/questions/1162491/alternative-to-mysql-real-escape-string-without-connecting-to-db – Bram Verstraten Nov 28 '12 at 09:57

4 Answers4

4

First make sure you are connect to your db

you can modify the settings in application/config/database.php

for codeigniter you should use

$this->db->escape() 

Details and samples can be found here http://ellislab.com/codeigniter/user-guide/database/queries.html

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Grumpy
  • 2,140
  • 1
  • 25
  • 38
  • Yes, I'm sure that I'm connected to the DB, I've even autoloaded the database library and I haven't personally used mysql_real_escape_string anywhere in my code. Can't you see that the error is coming from a native CI database driver, not from my own code. – ninjacoder Nov 28 '12 at 10:13
2

I finally found what was causing the problem. It was this line in the database config folder:

$db['mydb']['autoinit'] = FALSE;

When I changed it to :

$db['mydb']['autoinit'] = TRUE;

It worked! Thanks guys for all your advice, wouldn't have done it without you!

ninjacoder
  • 1,464
  • 2
  • 10
  • 13
1

The answer is simple: mysql_real_escape_string() (the hint is in the name) escapes a string based on the receiving character set used by the DB connection. In order to use this, you first need to be connected to a MySQL server!

Auto-load the DB class and have it auto-connect. It'll solve your problem.

Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66
  • I've autoloaded the database library and I haven't personally used mysql_real_escape_string anywhere in my code. – ninjacoder Nov 28 '12 at 10:14
  • Correction to the comment: if you haven't used mysql_real_escape_string at all and don't require DB access, unload the DB connector. It looks like it is trying to connect, fails, and continues as normal. Surely that's an old version of CodeIgniter, for it to do this (and to rely on deprecated functions)... – Sébastien Renauld Nov 28 '12 at 10:16
  • My eyes just got burned off by the CodeIgniter MySQL driver. I might provide a replacement at some point - this thing runs on ShutOp... no wonder it silently fails to connect. Anyway, if you don't need DB access, un-autoload it. It'll solve your issue. If you need DB access, autoload it and make sure it can connect. – Sébastien Renauld Nov 28 '12 at 10:24
0

check your db settings inside..

application>config>database.php
bipen
  • 36,319
  • 9
  • 49
  • 62