Parse error: syntax error, unexpected T_VARIABLE

Oops…

  <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 = mysqli_query($dbc, "SELECT Surname, Forename, HNN, Ad2, TC, Postcode FROM People")or die('Error: '.mysqli_error()); $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($r = mysqli_fetch_object($q)){ echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo '
$r->Surname$r->Forename$r->HNN$r->Ad2$r->TC$r->Postcode
'; 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>

Thanks for all your help, I appreciate this has been going on for some time, please don’t feel you have to keep replying but I am so grateful for your help so far. I am doing as much reading as I can as well but so far I don’t understand the different between mysql and mysqli, why there are two methods and what they are used for :slight_smile:

Ben

you don’t need the if($r) as if there’s an error the die command will show it so its not needed at all.

[php]

PHP Table integration with MySQL

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%;
}

<?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 = mysqli_query($dbc, "SELECT Surname, Forename, HNN, Ad2, TC, Postcode FROM People")or die('Error: '.mysqli_error()); // Table Header echo '<td align="left"PHP Address List '; // Fetch and print all the records: while($r = mysqli_fetch_object($q)){ echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo '
$r->Surname$r->Forename$r->HNN$r->Ad2$r->TC$r->Postcode
'; mysqli_free_result ($q); // Free up the resources mysqli_close($dbc); // Close the database connection. ?> [/php]

Yay - finally working! Thanks for all your patience and persistence!

Just a few questions about the script -

  1. while($r = mysqli_fetch_object($q)){

  2. echo “

    $r->Surname”; - i’m not quite sure how this bit works.
  3. When a database connection is established is the connection automatically assigned to “mysqli_query” and what is mysqli query (is that a function being created which is then called by the variable $q)? What is the difference between mysqli and mysql?

  4. while($r = mysqli_fetch_object($q)){ - I am not sure what this means and how to read it.

On the plus side a lot of it makes sense to me and I’m certainly starting to become familiar with the synatax!

Thanks :slight_smile:

Ben

your welcome,

when using mysql_fetch_object you return an object rather then an array as a susult you don’t use $r[‘Surname’] but $r->Surname its the -> then the column your after.

in order to call anything from a database a connection is needed first once it set you can use mysqli_query the difference is mysqli is more recent then mysql its recommended over mysql having said that I still use mysql.

does that make sense?

Thanks for that!

  1. Whats the different between an object and an array and why is an object better?

  2. With

($r = mysqli_fetch_object($q)){ 

Is this assigning mysqli_fetch_object to the variable $r and what is $q - is that the query?

  1. Why was mysqli introduced as well as/to replace mysql and what are the differences?

Thanks for taking the time to explain :slight_smile:

Ben.

an object can hold a lot of things where an array can only hold information for that array, in the context of your script there isn’t much difference I prefer to use an object so you can echo values inside double quotes without having to concatenate.

yes mysql_fetch_object is assigning all the data from the query ($q) to a var called $r

if you had a query like:

[php]$sql = mysql_query(“SELECT email FROM users”);[/php]

then the mysql_fetch_object would be:

[php]$r = mysql_fetch_object($sql);[/php]

for the difference between mysqli and mysql have a look at http://stackoverflow.com/questions/548986/mysql-vs-mysqli-in-php

Sponsor our Newsletter | Privacy Policy | Terms of Service