Jump to content

Reading and copying from the book the joy of php


Markus1954

Recommended Posts

Hi All;

I am stuck. I am getting an error in some code. First this is the error I am getting:  Parse error: syntax error, unexpected token "echo" in C:\Apache24\htdocs\createdb.php on line 19

The code below is line 18 and 19. In the past I found that sometimes the line above the error is the problem. I don't see the problem and hope that someone can shed some light on where this problem is. If you want to see more code I will provide it.

  

    if($mysqli->query("CREATE DATABASE Cars")=== TRUE{
        echo ("<p>DATABASE Cars created</p>");

Thanks for your help

Link to comment
Share on other sites

Look at the parens.  They are not matched.  Also your Brace doesn't show and ending brace and you don't need parens for an echo statement.

Does your host not offer you an administration tool such as cPanel to do this kind of work?  Much easier than writing queries to do it.

Edited by ginerjm
Link to comment
Share on other sites

The ending curly brace exists, I just didn't swipe it. In the book there are echo statements with both parentheses and no parentheses. I put the parentheses in this particular statement to see if it would work. In the book there are none.. I am learning from the book using apache, mysql and php 8 on my computer. I am not ready for hosting right now. When the time comes and I have a website ready I have an approach to do it on this computer.

Link to comment
Share on other sites

Please use the code tag button in the future.  I edited your post to add it for for your code snippet.  It's the <> button.

The condition in PHP must be inside parens.  This is what ginerjm was telling you.

Yours:

    if($mysqli->query("CREATE DATABASE Cars")=== TRUE{
        echo ("<p>DATABASE Cars created</p>");

The logical operator '===' has the 2 things you are comparing.  These must be enclosed in parens.  if (logical evaluation)  {  //true code } ...etc.

Should be:

    if ($mysqli->query("CREATE DATABASE Cars") === TRUE) {
        echo ("<p>DATABASE Cars created</p>");

Putting a space before/after logical evaluation constructs like if while etc., will help you see these types of mistakes easier.  PHP removes the whitespace, so anything you do to make your code more readable is fine.  

Link to comment
Share on other sites

Will this work better ?

$query = "INSERT NTO `Cars`.`inventory`
(`VIN`, `YEAR`, `Make`, Model`, `TRIM`, `EXT_COLOR`, `INT_COLOR`, `ASKING_PRICE`, `SALE_PRICE`, `PURCHASE_PRICE`, `MILEAGE`, `TRANSMISSION`, `PURCHASE_DATE`, `SALE_DATE`)
VALUES
(`5FNYF4H91CB054036`, `2012`, `Honda`, `Pilot`, `Touring`, `White Diamond Pearl`, `Leather`, `37807`, NULL, `34250`, `7076`, `Automatic`, `2012-11-08`, NULL)";

	if($mysqli->query($query) === TRUE
{
	echo ("<p>Honda Pilot inserted</p>");
}
else
{
	echo "<p>Error inserting Honda Pilot:</p>".$mysqli->error;
	echo"<p>*********</p>";
	echo $query;
	echo "<p>**********</p>";
}

This is the complete section of code where the problem exists.

Link to comment
Share on other sites

I pointed this out earlier and it was mentioned by another poster here.  The IF statement needs to be in parens and you are not doing that!!!

If (something == something)
{
	handle the true conditon
}
else
{
	handle the false condition
}

That is how it is written.

Edited by ginerjm
Link to comment
Share on other sites

On 12/18/2021 at 9:42 AM, Markus1954 said:

OK I put parentheses around the echo and it still did not work. Like I said above there are echo statements in the book that have () and some that are just 

echo "this is going to be printed";

 

It could not have been made any clearer than it was in my reply to you.  Take time to carefully read and study the replies, especially when people spend the time to give you code snippets and corrections.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.