Guide sample script doesn't seem to work

Long story short, I’m running through a PHP/MySQL for Dummies ebook.

I’ve attempted to run a sample script from it on my server but it doesn’t work. I think it doesn’t like the kind of quotation marks used.

Is this a problem with how my server is set up? Or is the guide wrong or out of date? (I changed the database options to my own as instructed)

The error I am getting is
Parse error: syntax error, unexpected ‘>’ in /home/content/t/a/k/takshack/html/php/sqltest.php on line 6

Changing the " to ’ solves it but there are so many instances of this that I can’t be bothered to find which ones are correct and which ones aren’t.

[php]<?php
/*Program: mysql_send.php
*Desc: PHP program that sends an SQL query to the

  • MySQL server and displays the results.
    */
    echo “
SQL Query Sender ”; if(ini_get(“magic_quotes_gpc”) == “1”) { $_POST[‘query’] = stripslashes($_POST[‘query’]); } $host=”hostname”; $user=” mysqlaccountname”; $password=” mysqlpassword”; /* Section that executes query and displays the results */ if(!empty($_POST[‘form’])) { $cxn = mysqli_connect($host,$user,$password, $_POST[‘database’]); $result = mysqli_query($cxn,$_POST[‘query’]); echo “Database Selected: {$_POST[‘database’]}
Query: {$_POST[‘query’]}

Results


”; if($result == false) { echo “

Error: “.mysqli_error($cxn).”

”; } elseif(@mysqli_num_rows($result) == 0) { echo “

Query completed. No results returned.

”; } else { /* Display results */ echo “”; $finfo = mysqli_fetch_fields($result); foreach($finfo as $field) { echo “”; } echo “”; for ($i=0;$i < mysqli_num_rows($result);$i++) { echo “”; $row = mysqli_fetch_row($result); foreach($row as $value) { echo “”; } echo “”; } echo “
”.$field->name.”
”.$value.”
”; } /* Display form with only buttons after results */ $query = str_replace(“‘“,”%&%”,$_POST[‘query’]); echo “

”; exit(); } /* Displays form for query input */ if (@$_POST[‘queryButton’] != “Edit Query”) { $query = “ “; } else { $query = str_replace(“%&%”,”’”,$_POST[‘query’]); } ?> ” method=”POST”>
Type in database name >
Type in SQL query <?php echo $query ?>
[/php]

Looks like you’re using MS Word as your editor :slight_smile: you better switch to some plain text editor such as Notepad, or some php editor (if you like highlighted code). I just replaced all the single/double quotes in your code, try it:
[php]<?php
/*Program: mysql_send.php
*Desc: PHP program that sends an SQL query to the

  • MySQL server and displays the results.
    */
    echo "
SQL Query Sender "; if(ini_get("magic_quotes_gpc") == "1") { $_POST['query'] = stripslashes($_POST['query']); } $host="hostname"; $user="mysqlaccountname"; $password="mysqlpassword"; /* Section that executes query and displays the results */ if(!empty($_POST['form'])) { $cxn = mysqli_connect($host,$user,$password, $_POST['database']); $result = mysqli_query($cxn,$_POST['query']); echo "Database Selected: {$_POST['database']}
Query: {$_POST['query']}

Results


"; if($result == false) { echo "

Error: ".mysqli_error($cxn)."

"; } elseif(@mysqli_num_rows($result) == 0) { echo "

Query completed. No results returned.

"; } else { /* Display results */ echo ""; $finfo = mysqli_fetch_fields($result); foreach($finfo as $field) { echo ""; } echo ""; for ($i=0;$i < mysqli_num_rows($result);$i++) { echo ""; $row = mysqli_fetch_row($result); foreach($row as $value) { echo ""; } echo ""; } echo "
".$field->name."
".$value."
"; } /* Display form with only buttons after results */ $query = str_replace("'","%&%",$_POST['query']); echo "

"; exit(); } /* Displays form for query input */ if (@$_POST['queryButton'] != "Edit Query") { $query = " "; } else { $query = str_replace("%&%","'",$_POST['query']); } ?>
Type in database name >
Type in SQL query <?php echo $query ?>
[/php]

I copied the code from the PDF into notepad. So this would mean the guide I am reading is wrong?

And yes that did work fine thanks :slight_smile:

You probably already noticed difference - in your code there were “left” and “right” quotes and same with double quites. These are special characters (non ascii). And in PHP syntax you can only use standard single quote ’ and double quote " chars.

Aha! I totally didn’t notice this! Thanks for pointing that out :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service