LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2013, 12:16 PM   #1
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Rep: Reputation: 127Reputation: 127
Php 5.4, error connecting to mysql db with hyphen in name


I have a small php application running on a webhost. Unfortunately my webhost insists in using '-' in db-names, which now causes problems - the php script can not connect to the database.
It works fine on their server running php 5.2, also on my old testserver with php-5.2, but not on my new test server running php 5.4. I intend to switch to 5.4 on webhost as soon as everything is tested fine on my test server.

Connecting to databases without '-' in name works fine so the hyphen has to be the issue. I have read lots about this problem, solution is always to use backticks - this works fine from cli but I just can't get it to work in php.

I have tried using backticks in $DATABASE, tried connecting directly to " `mydb-name` ", I have tried every combination of quotes, ticks, backticks - you name it, but no connection.
I also tried using PDO instead but it doesn't work either.
Code:
function anslutDB() {
//	Variabler
	$DB_HOST="172.16.1.10";
	$DB_USER="11111_aaaa";
	$DB_PASS="8blackdots";
	$DATABASE='11111-mydb';
	
	global $mysqli;

//	Funktion 
	$mysqli = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DATABASE);
My testserver:
VM-guest OpenVZ running on Proxmox host.
Ubuntu 12.04, apache 2.2.24, php-5.4.13-2~precise+1, mysql-5.5.29

Last edited by pingu; 10-24-2013 at 12:22 PM. Reason: Adding code
 
Old 10-24-2013, 04:12 PM   #2
tamtam
Member
 
Registered: May 2004
Distribution: Slackware.
Posts: 323

Rep: Reputation: 33
You could try escaping the hyphen...have not tried it myself...

Code:
function anslutDB() {
//	Variabler
	$DB_HOST="172.16.1.10";
	$DB_USER="11111_aaaa";
	$DB_PASS="8blackdots";
	$DATABASE='11111\-mydb';
	
	global $mysqli;

//	Funktion 
	$mysqli = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DATABASE);
 
Old 10-24-2013, 06:27 PM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Why the double quotes around all but the one giving you trouble?
Just saying...
single or double, the \ escape should work.
 
Old 10-25-2013, 02:42 AM   #4
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Original Poster
Rep: Reputation: 127Reputation: 127
I have tried escaping too but nope.
Why single quotes around the one giving me trouble:
I have tried single quotes, double quotes, ticks, backticks, combinations of these - nothing works.
Also tried a few escape variants like '\', '[]' also in varius combinations with quotes & ticks.

It's crazy, backticks should work as it works on cli? But it doesn't work even if I write in the db-name directly!

Last edited by pingu; 10-25-2013 at 02:43 AM.
 
Old 10-25-2013, 03:29 AM   #5
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Original Poster
Rep: Reputation: 127Reputation: 127
I am very sorry to have to admit I'm an idiot. I thought I had checked everything several times but I misread the db-name - it is actually "mydb-11111"...

I was fooled by the fact that I sometimes got error like "error connecting to database 11111\-mydb" - that is, when adding characters I get access denied as there is no database with that name.
For some weird reason I got no error message at all when trying to connect to 11111-mydb - which also then did not exist - this made me quite certain I had the correct name.

Anyway, problem solved.
Thanks guys!
 
Old 10-25-2013, 03:42 AM   #6
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Original Poster
Rep: Reputation: 127Reputation: 127
I have thourougly gone through everything I did concerning the database to find out why I got no error.
Adding in here just in case someone else is as confused as I was:
I imported the database into mysql but with wrong name ("mydb-11111").
However when granting access I used the correct db-name, so the user "11111_aaaa" did have access to database "11111-mydb" hence no error message connecting.

Last edited by pingu; 10-25-2013 at 03:42 AM. Reason: Spelling
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Fulltext search hyphen workaround using charsets MicahCarrick Linux - Software 0 10-11-2006 01:25 PM
Error connecting to MySQL from PHP anindyanuri Linux - Software 18 04-24-2005 09:30 AM
Connecting to Mysql using PHP mahugl Linux - Software 1 01-31-2004 11:20 AM
I need help connecting to mysql with php Pete Dogg Programming 4 06-08-2003 12:52 AM
connecting mysql by php norbs Linux - Software 1 07-25-2002 01:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration