so, i started at php and i'm doing some exercises from the book, but when i type this code into my "view.php", the output is all wrong...

<?php include("dbconnect.php") ?>
  
  <h2>View my GuestBook!!</h2>

  <?php

  $result=mysql_query("select * from guestbook")
     or die(mysql_error());
  while ($row=mysql_fetch_array($result))
  {
     echo "<b>Name:</b>";
     echo $row["name"];
     echo "<br>\n";
     echo "<b>Location:</b>";
     echo $row["location"];
     echo "<br>\n";
     echo "<b>Email:</b>";
     echo $row["email"];
     echo "<br>\n";
     echo "<b>URL:</b>";
     echo $row["url"];
     echo "<br>\n";
     echo "<b>Comments:</b>";
     echo $row["comments"];
     echo "<br>\n";
     echo "<br>\n";
     echo "<br>\n";
  }
  mysql_free_result($reslut);
  ?>

the output is like this:

View my GuestBook!!

Name:"; echo $row["name"]; echo "
\n"; echo "Location:"; echo $row["location"]; echo "
\n"; echo "Email:"; echo $row["email"]; echo "
\n"; echo "URL:"; echo $row["url"]; echo "
\n"; echo "Comments:"; echo $row["comments"]; echo "
\n"; echo "
\n"; echo "
\n"; } mysql_free_result($reslut); ?>

Sign my GuestBook

Recommended Answers

All 29 Replies

I think your server doesn't recognize php file, so
it doesn't interpret it. Do you install and configure
correctly PHP ?


- Mitko Kostov

i installed XAMPP... and i already made another php file that worked correctly, which was the one that writes the information this one is supposed to read...

here are all my codes:

dbconnect.php

<?php
mysql_connect("localhost","nobody","ydobon")
   or die("<h3>could not connect to MySQL</h3>\n");
mysql_select_db("guestbook")
   or die("<h3>could not select database 'guestbook'</h3>\n");
?>

sign.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  <h2>Sign my guestbook!</h2>
  <form method="post" action="create_entry.php">
     
     <b>Name:</b>
     <input type="text" size="40" name="name">
     <br>
     <b>Location:</b>
     <input type="text" size="40" name="location">
     <br>
     <b>Email:</b>
     <input type="text" size="40" name="email">
     <br>
     <b>Home Page URL:</b>
     <input type="text" size="40" name="url">
     <br>
     <b>Comments:</b>
     <textarea name="comments" cols="40" rows="4"
     wrap="virtualv"></textarea>
     <br>

     <input type="submit" name="submit" value="Sign!">
     <input type="reset" name="reset" value="Start Over">

     </form>
 </BODY>
</HTML>

create_entry.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  <?php
  include("dbconnect.php");

  if ($_REQUEST["submit"]=="Sign!")
  {
     $query="insert into guestbook(name,location,email,url,comments) values ('"
            .$_REQUEST["name"]
            ."', '"
            .$_REQUEST["location"]
            ."', '"
            .$_REQUEST["email"]
            ."', '"
            .$_REQUEST["url"]
            ."', '"
            .$_REQUEST["comments"]
            ."') "
     ;
     mysql_query($query);
 ?>
 <h2>Thanks!!</h2>
 <h2><a href="view.php">View my GuestBook!!</a></h2>
 <?php
 }
 else
 {
    include("sign.php");
 }
 ?>
 </BODY>
</HTML>

view.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  <?php include("dbconnect.php") ?>
  
  <h2>View my GuestBook!!</h2>

  <?php

  $result = mysql_query("select * from guestbook")
     or die(mysql_error());
  while ($row = mysql_fetch_array($result))
  {
     echo "Name:";
     echo $row["name"];
     echo "<br>\n";
     echo "<b>Location:</b>";
     echo $row["location"];
     echo "<br>\n";
     echo "<b>Email:</b>";
     echo $row["email"];
     echo "<br>\n";
     echo "<b>URL:</b>";
     echo $row["url"];
     echo "<br>\n";
     echo "<b>Comments:</b>";
     echo $row["comments"];
     echo "<br>\n";
     echo "<br>\n";
     echo "<br>\n";
  }
  mysql_free_result($result);
  ?>

  <h2><a href="sign.php">Sign my GuestBook</a></h2>

 </BODY>
</HTML>

everything works just fine until when it gets to view.php... dunno what the problem is...

I would suspect this lack of a semi-colon might be causing your problem

<?php include("dbconnect.php") ?>

though I'm not sure why you aren't getting an immediate script error from it.

according to what i know, it shouldn't affect... since it is a single command...

still... tried it, and no response... the same exact results...

according to what i know, it shouldn't affect... since it is a single command...

still... tried it, and no response... the same exact results...

Yes, I wondered about it and a couple quick tests here came up the same. The semi-colon did not matter. That was the only thing that stood out that might have caused it to switch parsing modes. I don't really see anything else yet that would be problematic.

did you try it? i mean, the whole 'combo'?

did you try it? i mean, the whole 'combo'?

No, as that would entail creating db tables as well :)

true... hehe... i don't think there's a problem with my php setup, since two out of three pages were working just fine...

maybe it's just something unnoticeable... or the example is not well written... i'll try some more and see if it persists...

It wouldn't hurt to move the include down into the other block before the query and add the semi-colon. Something is definitely kicking it into HTML parsing mode there.

still nothing... though the problem is @ line 22... after the first echo, since it prints "Name:" correctly, and then the rest of the code kicks in...

Try it like this,

<h2>View my GuestBook!!</h2>
<?php
require_once("dbconnect.php");
$exe=mysql_query("select * from guestbook")
  or die("SQL Error: ".mysql_error());
while ($r=mysql_fetch_array($exe))
{
   echo "<b>Name: {$r['name']}<br>\nLocation: {$r['location']}<br>\nEmail: {$r['email']}<br>\nURL: {$r['url']}<br>\nComments: {$r['comments']}<br>\n</b>";
}
?>

If that doesn't work, open up a MySQL console and do

use guestbook;
select * from guestbook;

If there is messed up HTML, then theres your problem.

output:

View my GuestBook!!

Name: {$r}
\nLocation: {$r}
\nEmail: {$r}
\nURL: {$r}
\nComments: {$r}
\n"; } ?>
Sign my GuestBook

Are you sure you don't have a closing php tag( ?> ) somewhere ?
Type phpinfo(); at the line where it crashes ,to see if the interpreter works at that point( although I'm pretty sure it won't)

it stops working right

echo "<b>Name:|<--here-->|</b>|<--or here-->|"

it stops working right

echo "<b>Name:|<--here-->|</b>|<--or here-->|"

Yes, that part is clear. Have you checked what is coming from your query for anything like tags that could be interfering as a few people above have mentioned? The code itself looks okay.

I know your problem.
That is because in your mysql database, you only have a db which name is guestbook.
But you don't have the table which name (according your php code) should also be guest book.
So i think if you create table guestbook("email" varchar(150),"......);
Then you will be o.k.

there is a table called 'guestbook', though, you're right... it doesn't seem to be called in view.php since there is just select * from guestbook unless this 'guestbook' refers to the table... since the db and the table are called the same...

there is a table called 'guestbook', though, you're right... it doesn't seem to be called in view.php since there is just select * from guestbook unless this 'guestbook' refers to the table... since the db and the table are called the same...

The 'guestbook' in that query is the table name. If you do have a table called guestbook then all is fine. Either way, that would not produce the behavior you are getting.

Well, I tested your php code in my computer. After i
mysql> create table guestbook
-> (name varchar(150),
-> location varchar(150),
-> email varchar(150),
-> url varchar(150),
-> comments text);
Query OK, 0 rows affected (0.01 sec)

It works. like:
View my GuestBook!!
Name:a
Location:a
Email:a
URL:a
Comments:b

So your code no problem, but i don't know whether your db is prepared or not.
You said sign.php and create_entry.php worked, so you can check in your db.
Use : mysql> use guestbook;
mysql> select * from guestbook;
To see whether there are records.
Mine has recoreds:
mysql> select * from guestbook;
+------+----------+-------+------+----------+
| name | location | email | url | comments |
+------+----------+-------+------+----------+
| a | a | a | a | b |
| w | w | w | e | s |
| h | h | h | h | h |
+------+----------+-------+------+----------+
3 rows in set (0.01 sec)

here are all my codes:

dbconnect.php

<?php
mysql_connect("localhost","nobody","ydobon")
   or die("<h3>could not connect to MySQL</h3>\n");
mysql_select_db("guestbook")
   or die("<h3>could not select database 'guestbook'</h3>\n");
?>

sign.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  <h2>Sign my guestbook!</h2>
  <form method="post" action="create_entry.php">
     
     <b>Name:</b>
     <input type="text" size="40" name="name">
     <br>
     <b>Location:</b>
     <input type="text" size="40" name="location">
     <br>
     <b>Email:</b>
     <input type="text" size="40" name="email">
     <br>
     <b>Home Page URL:</b>
     <input type="text" size="40" name="url">
     <br>
     <b>Comments:</b>
     <textarea name="comments" cols="40" rows="4"
     wrap="virtualv"></textarea>
     <br>

     <input type="submit" name="submit" value="Sign!">
     <input type="reset" name="reset" value="Start Over">

     </form>
 </BODY>
</HTML>

create_entry.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  <?php
  include("dbconnect.php");

  if ($_REQUEST["submit"]=="Sign!")
  {
     $query="insert into guestbook(name,location,email,url,comments) values ('"
            .$_REQUEST["name"]
            ."', '"
            .$_REQUEST["location"]
            ."', '"
            .$_REQUEST["email"]
            ."', '"
            .$_REQUEST["url"]
            ."', '"
            .$_REQUEST["comments"]
            ."') "
     ;
     mysql_query($query);
 ?>
 <h2>Thanks!!</h2>
 <h2><a href="view.php">View my GuestBook!!</a></h2>
 <?php
 }
 else
 {
    include("sign.php");
 }
 ?>
 </BODY>
</HTML>

view.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  <?php include("dbconnect.php") ?>
  
  <h2>View my GuestBook!!</h2>

  <?php

  $result = mysql_query("select * from guestbook")
     or die(mysql_error());
  while ($row = mysql_fetch_array($result))
  {
     echo "Name:";
     echo $row["name"];
     echo "<br>\n";
     echo "<b>Location:</b>";
     echo $row["location"];
     echo "<br>\n";
     echo "<b>Email:</b>";
     echo $row["email"];
     echo "<br>\n";
     echo "<b>URL:</b>";
     echo $row["url"];
     echo "<br>\n";
     echo "<b>Comments:</b>";
     echo $row["comments"];
     echo "<br>\n";
     echo "<br>\n";
     echo "<br>\n";
  }
  mysql_free_result($result);
  ?>

  <h2><a href="sign.php">Sign my GuestBook</a></h2>

 </BODY>
</HTML>

everything works just fine until when it gets to view.php... dunno what the problem is...

Could you post the HTML Source of the output from view.php.

Also, place the following above the page:

<?php 

error_reporting(E_ALL);
display_errors(true);

?>

Or the equivalent in your PHP.ini.

That way any warnings, notices, etc. will be printed.

so, i started at php and i'm doing some exercises from the book, but when i type this code into my "view.php", the output is all wrong...

<?php include("dbconnect.php") ?>
  
  <h2>View my GuestBook!!</h2>

  <?php

  $result=mysql_query("select * from guestbook")
     or die(mysql_error());
  while ($row=mysql_fetch_array($result))
  {
     echo "<b>Name:</b>";
     echo $row["name"];
     echo "<br>\n";
     echo "<b>Location:</b>";
     echo $row["location"];
     echo "<br>\n";
     echo "<b>Email:</b>";
     echo $row["email"];
     echo "<br>\n";
     echo "<b>URL:</b>";
     echo $row["url"];
     echo "<br>\n";
     echo "<b>Comments:</b>";
     echo $row["comments"];
     echo "<br>\n";
     echo "<br>\n";
     echo "<br>\n";
  }
  mysql_free_result($reslut);
  ?>

the output is like this:

Everything looks like its good to me except what Mitko said. I belive he may be right if else. The codes look good, I would check or uninstall/reinstall xampp. digital-ether is right also.

i think ther might be an error in XAMPP installation, since it doesn't seem to recognize the echo command... i'll try uninstalling and installing it again... (the other two worked well, since there were no "echo-es" in them...

i think ther might be an error in XAMPP installation, since it doesn't seem to recognize the echo command... i'll try uninstalling and installing it again... (the other two worked well, since there were no "echo-es" in them...

I'd recommend testing any theories individually before going through the whole uninstall/install process.

Just try a PHP file with:

<?php echo 'hello'; ?>

see if echo behaves funny.. etc.

BTW: Nichito, your avatar is scary! lol.

try escaping the '' like so:
\"
and in your record set use single quotes like so:
$row

i tried the <?php echo "hello" ?> command, and the output was:

so i guess that confirms my supposition...

though, i reinstalled in succesfully and it still returns the same output... i even tried other codes consisting in echoes and nothing happened...

you forgot the ;
<?php echo "Hello"; ?>

didn't really matter, since the output was exactly the same... though, it would have given my an error...

the only thing that could be wrong is u have something u are not seing
so u could rewrite the code again
maybe..........
;P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.