Please Advise - MySQL connection error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Please Advise - MySQL connection error

Post by danielwalters6 »

Here is the contents of frank.php

Code: Select all

<?php
$link = mysql_connect('xxx.xxx.xxx.xxx', 'uuuuuuuu', 'ppppppp');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
Unfortunately, this is the output - what am I doing wrong? :(
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/fhlinux197/g/gadgetsolutions.co.uk/user/htdocs/frank.php on line 2
Last edited by Benjamin on Thu Apr 30, 2009 3:39 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Please Advise - MySQL connection error

Post by requinix »

Since the error is on line two, and you censored line two...

Look at it. The error should be obvious.
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Re: Please Advise - MySQL connection error

Post by danielwalters6 »

tasairis wrote:Since the error is on line two, and you censored line two...

Look at it. The error should be obvious.
Ppologies the censorship was performed by the administrator, I did put all of my details in the post by mistake.

I took the above (seemingly standard php script) substituted my SQL details and got the problem mentioned.
If the problem was obvious to me, I wouldn't post here and waste your time

Thankyou in advance for any support you can provide.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Please Advise - MySQL connection error

Post by requinix »

You aren't wasting anyone's time.

The code you have there, the censored version, is okay. There aren't any problems with it.
Take your real code, put it in tags, then preview your post. It will add highlighting to your code and you should be able to see something weird with it.
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Re: Please Advise - MySQL connection error

Post by danielwalters6 »

Hi tasairis,

Unfortunately nothing jumped out and grabbed me, claiming to be the problem.

Would it be possible for someone to actually try my code, with their own database values in, just to confirm it's correct.

Should the Database values be in the 'single quotes'

Code: Select all

mysql_connect('xxx.xxx.xxx.xxx', 'uuuuuuuu', 'ppppppp');
Thank you for your patience

dan
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Please Advise - MySQL connection error

Post by Benjamin »

Does your password have a ' in it?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Please Advise - MySQL connection error

Post by requinix »

danielwalters6 wrote:Unfortunately nothing jumped out and grabbed me, claiming to be the problem.

Would it be possible for someone to actually try my code, with their own database values in, just to confirm it's correct.

Should the Database values be in the 'single quotes'

Code: Select all

mysql_connect('xxx.xxx.xxx.xxx', 'uuuuuuuu', 'ppppppp');
It could be that you don't know what to look for.

I ran your code through PHP's syntax checker before and it confirmed what I thought: no problems. So it must be in the censored part - the only (?) difference between your not-working code and the posted is-working code.

Single quotes are fine.

astions... huh, when did he become a mod... anyways, astions mentioned the only thing I can think of that's causing the problem: quotes in your username or password. If you had one in the password then the highlighting would look like

Code: Select all

mysql_connect('xxx.xxx.xxx.xxx', 'uuuuuuuu', 'ppp'pppp');
See how the coloring changes? That's what you would see. Except if I do that I get a generic "parse error" and not the T_BLAH message you have so it must be something else. Perhaps your code is more complicated and uses more than just a couple literal strings?

Finally, are you absolutely sure you're looking at the right line number in the right file?
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Re: Please Advise - MySQL connection error

Post by danielwalters6 »

Thank you for your continue support.

The output of frank.php can be seen here;
http://gadgetsolutions.co.uk/CMS/inc/frank.php

I have uploaded frank.php (I have had to zip it due to restrictions of this PHPBB installation)
frank.php.zip
Frank.php but with txt extension
(636 Bytes) Downloaded 12 times
Hopefully this might shed some light.

Looking at the frank.php you might see that I have not got MY personal details in. After putting my host's DB server IP, Username and Password, I'm getting the same result.
(Plus the output of an incorrect password etc, should be "error connecting to database" and not a parse error)

Thank you
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Please Advise - MySQL connection error

Post by requinix »

You've got gibberish in your file.

Code: Select all

<?php
$link = mysql_connect('myhostipaddress' , 'username' , 'password' );
if (! $link) {
    die('Could not connect');
}
echo 'Connected successfully';
mysql_close($link);
?>
I believe those Âs mark spaces. Non-breaking spaces. Thing is, PHP doesn't like them.
Or if not the spaces it's the file encoding.

Either way, look in your editor for options to change that. Where? Don't know, depends on what you use.
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Re: Please Advise - MySQL connection error

Post by danielwalters6 »

To edit my files, I was/am using TextWrangler, a free script-writing software package for Mac.

I have also re-written my files in Mac's own TextEdit (using Plain Text format) - re uploaded, with no significant difference.

Is it my FTP program that's uploading with the wrong encoding, I'm using Filezilla at the moment?

Can anyone suggest some Mac-friendly software which I could use to edit my php files?

Best regards.
dan.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Please Advise - MySQL connection error

Post by Christopher »

(#10850)
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Re: Please Advise - MySQL connection error

Post by danielwalters6 »

I dropped a question to my hosting provider, they pointed me to this script in their "knowledge base"

Code: Select all

<?php
$dblink = mysql_connect("databaseserver", "ursername", "password") or  die("Could not connect to MySQL server"); 
 
$db = mysql_select_db("gadgetsolu1") or die("Could not connect to Database"); 
 
$res = mysql_query("SHOW TABLES ") or die("Could not run query"); 
 
$row = mysql_fetch_array($res); 
 
if($row == "")
{ echo "No tables found, connection works.";}
else
{ echo "Tables found and connections work.<br />Tables found:<br />"; 
 foreach($row as $key => $value){ if(is_numeric($key)){ echo $value."<br />"; } } }?>
This seems to work. Although I can't seem to work out where it's Closing the database connection?
Post Reply