SitePoint Sponsor |
|
User Tag List
Results 1 to 16 of 16
-
Apr 26, 2006, 07:42 #1
- Join Date
- Feb 2006
- Location
- North Carolina
- Posts
- 288
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Page is displaying php instead of output
Hey,
I am really new to php and when I enter this code it actually displays the php after the print command on line 5 in the browser and not the output. Any ideas?
<?
$username="Physician";
$password="doctor";
$database="jmh";
print"<br>after variables before mysql_connect</br>";
mysql_connect("localhost",$username,$password);
mysql_select_db($database) or die("Unable to select database");
$query="SELECT * FROM dr_directory";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
print"Database Output";
$i=0;
while ($i<$num) {
$last=mysql_result($result,$i,"LastName");
$first=mysql_result($result,$i,"FirstName");
$phone=mysql_result($result,$i,"HomePhone");
print"<br>$first $last</br><br>$phone</br>";
$i++
}
?>
thanks,
Dan
-
Apr 26, 2006, 07:43 #2
- Join Date
- Dec 2005
- Posts
- 36
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Server might not be configured to use short tags, try opening with <?php instead of just <?
EDIT: ignore that I didn't fully read your post
-
Apr 26, 2006, 07:45 #3
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
That was my first thought. My second was ... the file is actually called filename.php isn't it? (Stranger things are known to have been done).
Edit:
On re-reading the question, I'm not actually sure what the problem is you are trying to describe. Does it show the WHOLE FILE including ALL PHP code, or only the code after the print() function?
-
Apr 26, 2006, 07:45 #4
Originally Posted by hurricanedan
Words are weapons of Mass Destruction.
-
Apr 26, 2006, 07:48 #5
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Good spot - so why didn't a PHP parsing error occur? (Or did it and the poster hasn't said so?)
-
Apr 26, 2006, 07:50 #6
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Every print statement should cause a parse error as there's no space between the word print and the string, nor a parentheses.
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Apr 26, 2006, 08:31 #7
- Join Date
- Feb 2006
- Location
- North Carolina
- Posts
- 288
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
no parsing errors are showing
Guys, looking over your posts and making the corrections that were listed I have revised the code and now it looks like this. I added the ()s and the semicolon. Also there have been no parsing errors.
<?
$username="Physician";
$password="doctor";
$database="jmh";
print("<br>after variables before mysql_connect</br>");
mysql_connect("localhost",$username,$password);
mysql_select_db($database) or die("Unable to select database");
$query="SELECT * FROM dr_directory";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
print("Database Output");
$i=0;
while ($i<$num) {
$last=mysql_result($result,$i,"LastName");
$first=mysql_result($result,$i,"FirstName");
$phone=mysql_result($result,$i,"HomePhone");
print("<br>$first $last</br><br>$phone</br>");
$i++;
}
?>
This is the output:
after variables before mysql_connect"); mysql_connect("localhost",$username,$password); mysql_select_db($database) or die("Unable to select database"); $query="SELECT * FROM dr_directory"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); print("Database Output"); $i=0; while ($i<$num) { $last=mysql_result($result,$i,"LastName"); $first=mysql_result($result,$i,"FirstName"); $phone=mysql_result($result,$i,"HomePhone"); print("
$first $last
$phone"); $i++; } ?>Last edited by hurricanedan; Apr 26, 2006 at 08:32. Reason: adding info
-
Apr 26, 2006, 08:33 #8
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Whilst it shouldn't cause the error you are seeing, it is <br> or <br /> ... there is no </br> tag related to the <br> tag.
print ("<br />after variables before mysql_connect<br />");
Also, where's your link identifier?
$conn = mysql_connect ("localhost",$username,$password);
I think you have something screwed up with your PHP installation - you should be getting error messages trying to run this code.
-
Apr 26, 2006, 08:38 #9
- Join Date
- Feb 2006
- Location
- North Carolina
- Posts
- 288
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just as a testing idea I removed the <br></br> tags. I do not get any errors but I also get no outputs from my print() statements either.
-
Apr 26, 2006, 08:41 #10
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
PS - you can use the print function with or without ( ) - they are not critical. (The space after the print command is critical though).
-
Apr 26, 2006, 09:00 #11
- Join Date
- Feb 2006
- Location
- North Carolina
- Posts
- 288
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks, siteguru, I was not sure but I wanted to be sure that it worked. I also tried to strip the code down and this code will actually work:
<?php
$username="Physician";
$password="doctor";
$database="jmh";
print("after variables before mysql_connect");
print($username);
print($password);
print($database);
print("$username $password $database");
?>
Note: see how the opening will work with <?php and the close will work as ?>, I thought they had to be the same. Also I just want to let everyone know that I appreciate the quick responses.
When I modify the code to read as follows the screen comes back blank, do you think it could be an error in my connect and select_db statements?:
<?php
$username="Physician";
$password="doctor";
$database="jmh";
mysql_connect("localhost",$username,$password);
mysql_select_db($database) or die("Unable to select database");
print("after variables before mysql_connect");
print("<br>$username");
print("<br>$password");
print("<br>$database");
print("<br>$username $password $database");
?>
DanLast edited by hurricanedan; Apr 26, 2006 at 09:04. Reason: adding more code
-
Apr 26, 2006, 09:25 #12
- Join Date
- Oct 2003
- Location
- Your Monitor
- Posts
- 1,146
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If you are just learning PHP why don't you first get away from the print and use echo? I guess it's more of personal choice but echo (I believe) is more widely used.
PHP Code:$username = 'Physician';
$password = 'doctor';
$database = 'jmh';
$host = 'localhost';
mysql_connect($host, $username, $password);
mysql_select_db($database) or trigger_error(mysql_error());
// the above will give you a better clue as to what is
// going wrong but you probably don't want to use it on
// a live site just for testing purposes.
echo 'after variables before connect';
echo '<br>'.$username;
echo '<br>'.$password;
echo '<br>'.$database;
echo '<br>'.$username.' '.$password.' '.$database;
-
Apr 26, 2006, 10:06 #13
- Join Date
- Mar 2006
- Location
- Netherlands
- Posts
- 1,661
- Mentioned
- 7 Post(s)
- Tagged
- 1 Thread(s)
Also, if you look at the source in your browser, does it display the whole file?
Or just from after the print statement?
Do you have access to the server's error log?
-
Apr 26, 2006, 10:15 #14
- Join Date
- Feb 2006
- Location
- North Carolina
- Posts
- 288
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Immerse,
It seems to be a server error. It has a small list of errors for innoDB so maybe after a reboot it will work correctly.
Thanks for that point.
-
Apr 26, 2006, 10:54 #15
- Join Date
- Feb 2006
- Location
- North Carolina
- Posts
- 288
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Guys,
Thanks again for all of your help. It was an error on my part. I missed uncommenting my mysql extension. I had set the extension_dir but had forgotten to uncomment the mysql ext.
Thanks again,
Dan
-
Apr 26, 2006, 11:58 #16
- Join Date
- Mar 2006
- Location
- Netherlands
- Posts
- 1,661
- Mentioned
- 7 Post(s)
- Tagged
- 1 Thread(s)
Glad you got it working!
Bookmarks