Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hey guys I am getting an unexpected error message fo the following coed, any ideas...
<body>
<?php
include("includes/open_db.php");
$result = mysql_query("SELECT * FROM index")
or die(mysql_error());
while($row = mysql_fetch_row($result))
{
$leftdiv1 = $row[0];
$leftdiv2 = $row[1];
$leftdiv3 = $row[2];
$contentdiv1 = $row[3];
?></body>
</html>
my include file code follows<?php
$con = mysql_connect("localhost" ,"name","pass") or die(mysql_error());<?php
mysql_select_db("dbname") or die(mysql_error());
?>
Also, I want to extract some information from the database and echo it out firther down in the code, can I close the <?php nad the nreopen it further down and still have access to the variables?
The error I am getting isParse error: syntax error, unexpected $end in /home/psctestc/public_html/sherrilyn/index.php on line 50
line 50 being the closing html tag

no closing curly bracket after while()
That error (unexpected $end) is almost always related to forgotten closing curly brackets on if-else statement, while, do, for, foreach, switch, and the likes.
can I close the <?php nad the nreopen it further down and still have access to the variables?Yes.
---
Fubar

Hi guys, I am getting real aggravated at this, I think it has something to do with the version of mysql I am running. I can connect to the DB no problem but I cannot retract an info nad display it. Here is some basic code that I used as a test. There is only 1 row within this database table (index) with four fields (leftdiv1, leftdiv2, leftdiv3, contentdiv1) here is the code and error-
<?php
$result = mysql_query("SELECT * FROM index")
or die(mysql_error());
$row = mysql_fetch_array( $result );
echo "Name: ".$row['leftdiv1'];
echo " Age: ".$row['leftdiv2'];
echo "Name: ".$row['leftdiv3'];
echo " Age: ".$row['contentdiv1'];
?>
error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index' at line 1Any ideaas?

OK, let's think a little logically? What would the version of MySQL have to do with running a basic query? If you can't query a database - then it's not of any use.
The problem is that you picked a reserved word as your table name. 'INDEX' is a database command. You *can* use it as a table name, but I would HIGHLY discourage it.
When referencing a table or column name in a query you can avoid reserved word problems (and it's good practice) by enclosing the name in back quotes.
This query will work, but you really should change the table name (I aslo added some better error handling)
$query = "SELECT * FROM `index`";
$result = mysql_query($query)
or die("The query:[br]$query[br]Produced the error:[br]".mysql_error());Michael J

Another good practice to stay away from problems like this is to prefix your table names.
It'll also help when you use the database for another application (which tends to have similar table names).
app1_index
app1_users
app2_index
app2_users--
Also, take a look inside some popular open source apps like WordPress to see how they handle it.
---
Fubar

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |