Parse error: syntax error, unexpected T_VARIABLE

Hello, i’m not sure what is up here:

  <head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >

       <title>PHP Table integration with MySQL</title>
body { font-family:arial; color: navy; } h1, h2 { text-align: center; font-weight: strong; } div.text {color: navy; } div.right { text-align: right; } div.table { text-align: center; margin-left: 20%; }
       </head>


   <body>

<?php

echo "Time: “;
echo date(’ H:i:s’);
echo " Date:”;
echo date(‘j F Y’);
?>

PHP Test Area


The following is an attempt at creating a table to recall data from an SQL Database:

The following is a table which will be populated automatically from a Database using a PHP script so that the entries do not need to manually be typed out.

<? include ('../../testphpincludes/header.php') $q = "SELECT CONCAT(Surname, ', ', Forename) AS name, $r = @mysqli_query ($dbc, $q); // Run the query if ($r) {// If it ran OK, display the records. // Table Header echo '<td align="center"Date Registered '; // Fetch and print all the records: while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo ' '; } echo '
'. $ row['name'] . ' . $row['dr'] . '
'; mysqli_free_result ($r); // Free up the resources } else { // If it did not run OK. //Public Message: echo 'The address table could not be retrieved because you have cocked up your code.'; } // Debugging Message: echo '

' . mysqli_error($dbc) . '

Query: ' . $q . ''; } // End of if ($r) IF. mysqli_close($dbc); // Close the database connection. ?>

   </html>

the full includes address is: www.garethgroup.com/testphpincludes/header.php

the full address where the script is stored is: www.garethgroup.com/test/php/index.php

Thanks lots!

Ben :slight_smile:

that error is usually doe to a command not being terminated such as the following line is missing an ending ;

include ('../../testphpincludes/header.php')

add ; to the end to terminate the line

include ('../../testphpincludes/header.php'); 

Hi, Thanks for that, as soon as I corrected that one I had another problem with a different line. Adding a ; did not work.

The full code is:

  <head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >

       <title>PHP Table integration with MySQL</title>
body { font-family:arial; color: navy; } h1, h2 { text-align: center; font-weight: strong; } div.text {color: navy; } div.right { text-align: right; } div.table { text-align: center; margin-left: 20%; }
       </head>


   <body>

<?php

echo "Time: “;
echo date(’ H:i:s’);
echo " Date:”;
echo date(‘j F Y’);
?>

PHP Test Area


The following is an attempt at creating a table to recall data from an SQL Database:

The following is a table which will be populated automatically from a Database using a PHP script so that the entries do not need to manually be typed out.

<? include ('../../testphpincludes/header.php'); $q = "SELECT CONCAT(Surname, ', ', Forename) AS name, $r = @mysqli_query ($dbc, $q); // Run the query if ($r) {// If it ran OK, display the records. // Table Header echo '<td align="left"PHP Address List '; // Fetch and print all the records: while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo ' '; } echo '
'. $row['name'] . ' . $row['dr'] . '
'; mysqli_free_result ($r); // Free up the resources } else { // If it did not run OK. //Public Message: echo 'The address table could not be retrieved because you have cocked up your code.'; } // Debugging Message: echo '

' . mysqli_error($dbc) . '

Query: ' . $q . ''; } // End of if ($r) IF. mysqli_close($dbc); // Close the database connection. ?>

   </html>

The issue is Parse error: syntax error, unexpected T_STRING in /home/garethgr/public_html/test/php/index.php on line 81

According to my code editor (please let me know if i’m right) Line 81 is echo ’

Thanks for your help :slight_smile:

Ben.

ok give this a try:

[code]

  <head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >

       <title>PHP Table integration with MySQL</title>
body { font-family:arial; color: navy; } h1, h2 { text-align: center; font-weight: strong; } div.text {color: navy; } div.right { text-align: right; } div.table { text-align: center; margin-left: 20%; }
       </head>


   <body>

<?php

echo "Time: “;
echo date(’ H:i:s’);
echo " Date:”;
echo date(‘j F Y’);
?>

PHP Test Area


The following is an attempt at creating a table to recall data from an SQL Database:

The following is a table which will be populated automatically from a Database using a PHP script so that the entries do not need to manually be typed out.

<? include ('../../testphpincludes/header.php'); $q = "SELECT CONCAT(Surname, ', ',Forename) AS name"; $r = @mysqli_query ($dbc, $q); // Run the query if ($r) {// If it ran OK, display the records. // Table Header echo '<td align="left"PHP Address List '; // Fetch and print all the records: while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo ''; } echo '
'.$row['name'] ." ".$row['dr'].'
'; mysqli_free_result ($r); // Free up the resources } else { // If it did not run OK. //Public Message: echo 'The address table could not be retrieved because you have cocked up your code.'; } // Debugging Message: echo '

' . mysqli_error($dbc) . '

Query: ' . $q . ''; mysqli_close($dbc); // Close the database connection. ?>

   </html>[/code]

Thanks for that! That’s got the error message displaying from the code! What did you change and what was the reason (So I learn from it?)

New error:

Unknown column ‘Surname’ in ‘field list’

Query: SELECT CONCAT(Surname, ', ',Forename) AS name

I’ve checked the database I set up myself and there is definitely a Surname and a Forename column in the database. Thanks every so much for all your help!

Ben

i changed

$q = "SELECT CONCAT(Surname, ', ', Forename) AS name,

to

$q = "SELECT CONCAT(Surname, ', ',Forename) AS name";

the query is still missing the table name

Also changed

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo '<tr><td align="left">'. $row['name'] . </td><td align="left">' . $row['dr'] . '</td></tr> '; }

to

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo '<tr><td align="left">'.$row['name'] ."</td><td align=\"left\">".$row['dr'].'</td></tr>'; }

I think that was it, I made sure any echo was terminated with a ; any double quotes inside a quote is escaped

for your error the dataase name is missing from

$q = "SELECT CONCAT(Surname, ', ',Forename) AS name";

something like

$q = "SELECT CONCAT(Surname, ', ',Forename) AS name FROM tablename";

Thanks for that,

I used the suggestion you gave but my header.php file has a connection to the database included:

<? # mysql connection to address database DEFINE ('DB_USER', 'garethgr_test'); DEFINE ('DB_PASSWORD', 'DELETED'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'garethgr_test'); $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL because you have not typed the db login script properly: ' . mysqli_connect_error() ); ?>

Does your suggested line specifying the database need to be there?

Error given: Table ‘garethgr_test.garethgr_test’ doesn’t exist

Query: SELECT CONCAT(Surname, ', ',Forename) AS name FROM garethgr_test

Thanks again :slight_smile: Sorry about there being a million problems!

no problem don’t worry, your database is called garethgr_test inside your database you have at least one table to use data from a table you need to use the table name in your query does that make sense?

Hey :smiley: Thanks man! Certainly getting there: http://testarea.garethgroup.com/php/ :smiley:

How do I add more columns to the table: Is it as simple as altering the following code line:

[php]echo ‘

’.$row[‘name’] ."<td align=“left”>".$row[‘dr’].’’;[/php]

I’m not quite sure of the syntax that should be used to include other columns and are the columns auto generated in the same way as the rows are or would alterations need to be made to this aspect of the coding to?

Thanks so much for taking the time to answer my questions! Once i’m confidence in this i’ll be sure to show as many other people as possible!

Ben

first you need to include the columns you want from the database table in your query then add them to your echo.

Here’s a quick example say you wanted a persons firstname,lastename and email address:

[code]

<?php //query database $q = mysql_query("SELECT firstname,lastname,email FROM users");

//loop through results and echo them out
while($r = mysql_fetch_object($q)){

echo “

”;
echo “”;
echo “”;
echo "";
echo “”;

}
?>

First Name Last Name Email
$r->firstname$r->lastname$r->email
[/code]

I prefer to use mysql_fetch_object over mysql_fetch_array as it brings the data as an object then you call the field by using -> instead if [] which means you can echo the value inside double quotes without having to concatenate them.

Also remember if you want to run a query to being results based an a criteria then you can use a WHERE for instance:

$q = mysql_query("SELECT firstname,lastname,email FROM users WHERE level='1' ");

Thanks! Ooops…

The following is a table which will be populated automatically from a Database using a PHP script so that the entries do not need to manually be typed out.

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/garethgr/public_html/test/php/index.php on line 87
PHP Address List

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in /home/garethgr/public_html/test/php/index.php on line 99

Query: SELECT CONCAT(Surname, Forename, HNN, Ad2, TC, Postcode) FROM People

Ben :slight_smile:

did you terminate the string?

I wouldn’t use CONCAT: I’ve added or die('Error: '.mysql_error()); to the end of the query so if there is an error it will show you why

[php]$q = mysql_query(“SELECT Surname, Forename, HNN, Ad2, TC, Postcode FROM People”)or die('Error: '.mysql_error());[/php]

I’ve edited that but now it will not connect to the database. The header which is included has these details in and is called though :slight_smile: Grrr confusion!!

ah, just noticed your using mysqli not mysql to you’ll need to update your query to use mysqli

Done :slight_smile: Haha:

Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/garethgr/public_html/test/php/index.php on line 72

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/garethgr/public_html/test/php/index.php on line 72
Error:

looking at the manual mysqli expects the resource id (your database connection var) then the query

http://php.net/manual/en/mysqli.query.php

[php]$q = mysqli_query($dbc, “SELECT Surname, Forename, HNN, Ad2, TC, Postcode FROM People”);[/php]

Hello,

Thanks so much for your help and patience over the recent days. I understand if you have had enough of helping me! Sorry, I could have checked that myself on the PHP Reference Guide I guess!

Another error however has come up which I don’t understand -

Catchable fatal error: [php]Object of class mysqli_result could not be converted to string in /home/garethgr/public_html/test/php/index.php on line 109[/php]

Thanks :slight_smile:

Ben

Ok can you post the code that’s causing the error it will around your query

Sorry :slight_smile: - [php]echo ‘

’ . mysqli_error($dbc) . '

Query: ’ . $q . ‘’;[/php]

sorry I can’t tell whats happening by that line an you post the code about it that may be related to it

Sponsor our Newsletter | Privacy Policy | Terms of Service