Page 1 of 1
Newbie: Need help with BASIC PHP , HTML and MYSQL connection
Posted: Tue Jun 15, 2010 1:44 am
by purushi1
Hi All,
I am exploring PHP and MYSQl connection.
With great difficulty i have been able to get php, mysql and apache all working.
I tried a simple code
Code: Select all
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<h2>
Testing MySQLConnect
</h2>
<?php
$link = mysql_connect('localhost', 'root', 'root');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo '<p>Connected successfully </p>';
mysql_close($link);
?>
</body>
</html>
When i try this output on IE and Firefox, i am getting an extra line of output.
"; mysql_close($link); ?>
Not able to figure out why this is happening. The output looks like this in text format
#########################
Testing MySQLConnect
Connected successfully
"; mysql_close($link); ?>
#########################
Please help me.
thanks

Re: Newbie: Need help with BASIC PHP , HTML and MYSQL connec
Posted: Tue Jun 15, 2010 4:04 am
by koolsamule
Hi, probably due to this:
echo '<p>Connected successfully </p>";
Change it to:
Code: Select all
echo "<p>Connected successfully </p>";
Note the double-quote, rather than the single-quote at the start of the output.
Re: Newbie: Need help with BASIC PHP , HTML and MYSQL connec
Posted: Tue Jun 15, 2010 4:06 am
by hypedupdawg
If you look carefully, you have ended the string you are outputting with a
double quote. So php is treating the next lines as a string also; you may notice in your post below, the last three lines are all coloured blue (string) when there should be different colours for functions etc.
All you need to do is end with a
single quote:
Code: Select all
<?php
$link = mysql_connect('localhost', 'root', 'root');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo '<p>Connected successfully </p>'; //change quote here
mysql_close($link);
?>
That should fix all your problems
EDIT: Just saw the first reply. Yes: as long as the quotes are
the same type at the start and end, it will work fine.
Re: Newbie: Need help with BASIC PHP , HTML and MYSQL connec
Posted: Tue Jun 15, 2010 4:08 am
by aravona
Double or Single, doesn't matter but make sure you
match them up
Personally I single quote php, because I double quote html ^_^
Re: Newbie: Need help with BASIC PHP , HTML and MYSQL connec
Posted: Tue Jun 15, 2010 4:22 am
by purushi1
Sorry , That was a typo in the code i pasted.
I corrected it to Single quote only or double quote only. There is no change in output.
echo "<p>Connected successfully </p>";
or
echo '<p>Connected successfully </p>';
Re: Newbie: Need help with BASIC PHP , HTML and MYSQL connec
Posted: Tue Jun 15, 2010 4:26 am
by aravona
Shouldn't be showing that as far as I can see if you're closing tags etc properly. If you're running live, try CTRL +F5... if you're on a wamp may have to shut the localhost and reopen it to see a change, it can get its knickers in a twist. (eliminate the obvious ^^)
Edit: on wamp, in IE and FF it is fine.
Re: Newbie: Need help with BASIC PHP , HTML and MYSQL connec
Posted: Tue Jun 15, 2010 4:36 am
by purushi1
I am not using WAMP, instead i have manually installed PHP and apache servers.
I just modified the code as below
Code: Select all
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<h2>
Testing MySQLConnect
</h2>
<?php
echo "<p>Connected successfully 1</p>";
echo "<p>Connected successfully 2</p>";
?>
</body>
</html>
And even here the output is not clean. Tested on Firefox only.
#######################################
Testing MySQLConnect
Connected successfully 1
"; echo "
Connected successfully 2
"; ?>
#######################################
I have restarted services on apache and still there is no change.
And one more information to add. I have saved the file as .html (not php).
thanks.
Re: Newbie: Need help with BASIC PHP , HTML and MYSQL connec
Posted: Tue Jun 15, 2010 4:47 am
by aravona
Oh dear,
All files including PHP must be .php
Even if they happen to have HTML in them, it needs to be .php if it contains php. Try changing the extention.
Re: Newbie: Need help with BASIC PHP , HTML and MYSQL connec
Posted: Tue Jun 15, 2010 4:50 am
by purushi1
Thank You So much.
And that solves all my problem
I am getting clean output now.