Forum Moderators: coopster

Message Too Old, No Replies

Parse error, unexpected T_STRING in.

$sql is causing an error on my page, using PHP 5, MySQL 5, Apache 2

         

rolANDkA

8:51 am on Nov 18, 2005 (gmt 0)

10+ Year Member



Hi all,

I'm new, and I believe I found the right place to post this, so let me apologize head of time if I am mistaken.

I am using a book that's reputable from what I've read, so here's the issue.

I installed XAMPP on my XP bo (w/ VMWARE running Fedore 4)

Here's the error:

Parse error: parse error, unexpected T_STRING in C:\Program Files\xampp\htdocs\projects\mysql_up.php on line 10

Here's the code for the culprit page, which is supposed to just give me a black/white answer of whether PHP and MySQL are working properly:

<html>
<head><title>Test MySQL</title></head>
<body>
<!-- mysql_up.php -->
<?php
$host=”hostname”;
$user=”mysqlaccount”;
$password=”mysqlpassword”;
mysql_connect($host,$user,$password);
$sql=”show status”;
$result = mysql_query($sql);
if ($result == 0)
echo “<b>Error “ . mysql_errno() . “: “
. mysql_error() . “</b>”;
else
{
?>
<!-- Table that displays the results -->
<table border=”1”>
<tr><td><b>Variable_name</b></td><td><b>Value</b>
</td></tr>

<?php
for ($i = 0; $i < mysql_num_rows($result); $i++) {
echo “<TR>”;

$row_array = mysql_fetch_row($result);

for ($j = 0; $j < mysql_num_fields($result); $j++)
{
echo “<TD>” . $row_array[$j] . “</td>”;
}
echo “</tr>”;
}

?>

</table>

<?php }?>

</body>
</html>

Thank you in advance, and I will also post when I am seasoned and give back to other beginners coming through.

Sincerely,
Mark

Anyango

9:33 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Mark, Welcome to Webmasterworld.

And yes you are at the right place to post this problem and we appreciate your words.

You might be surprised of how little the error is, but it is indeed a syntax/parse error ;)

This character


and



which you have used throughout your code are unfortunately not either single quote or double quotes. and to enclose any string you need to enclose it in either single quotes or double quotes in any programming language. that's the error

Just do a search and replace for those two characters and replace them with proper


"

And it's perfect ;)

Regards,
Kami

rolANDkA

10:04 am on Nov 18, 2005 (gmt 0)

10+ Year Member



Kami,

Thank you so much for replying. Wow, that was fast!

I had a litte bit of an emergency (which I'll tell you about in a second, because I think it may apply to others, yet it's sort of unrelated to this post - well, maybe not).

I found the following link, which I think applied to my situation, and I wanted to post it here in case others ran into the same situation I did, and could use the info:

[shiflett.org...]

Ok, it says in that page about the same thing you were saying - about the double quotes needing to be single quotes - and I did just that; replaced all of the double quotes with singles, and wala! It works!

Now, you were saying something in your post about removing the quotes all together, and replacing it with parens or brackets? I'm confused, and if you could clear that up for me so I understand, I would really appreciate it. I want to make sure I have this information down firm, and I'm still a bit hazy, even though I was lucky enough to fix the problem I posted on. Here's what I did (code pasted below):

<html>
<head><title>Test MySQL</title></head>
<body>
<!-- mysql_up.php -->
<?php
$host='localhost';
$user='root';
$password='dbpass';
mysql_connect($host,$user,$password);
$sql='show status';
$result = mysql_query($sql);
if ($result == 0)
echo '<b>Error ' . mysql_errno() . ': '. mysql_error() . '</b>';
else
{
?>
<!-- Table that displays the results -->
<table border=”1”>
<tr><td><b>Variable_name</b></td><td><b>Value</b>
</td></tr>
<?php
for ($i = 0; $i < mysql_num_rows($result); $i++) {
echo '<TR>';
$row_array = mysql_fetch_row($result);
for ($j = 0; $j < mysql_num_fields($result); $j++)
{
echo '<TD>' . $row_array[$j] . '</td>';
}
echo '</tr>';
}
?>
</table>
<?php }?>
</body>
</html>

Now, you're saying that I could have done something other than single quotes as well?

Is this a new thing in PHP5 where they are talking about a good majority of the code has changed drastically enough to where it will require some recoding if you've done much of your coding in PHP4?

I know, I need to read up more on PHP5 (and 4), but I just wanted to clarify your input and put it in my notes so that I'm clear on it.

Thanks again, I appreciate you taking the time to assist me.

I will check back later today. I'm off to bed.

Night all,
Mark

rolANDkA

10:09 am on Nov 18, 2005 (gmt 0)

10+ Year Member



One last thing here. I just realized that my post is completely vague because not only did I leave out the line numbers, but I don't believe I was specific on exactly what line the error was on. I'm sorry. I did a cut and paste right out of EditPlus, thinking that the line numbers were going to be there.

The error (parser stopped reading) came at line 10, which is this line (I'd paste a print screen, but I don't think this page will allow me to do so):

$sql=''show status'';

I changed that to:

$sql='show status';

Then all the other errors came, line by line (the ones that had the double quotes in them), and I changed all of them, careful so as to keep attention to detail throughout the code and not put an extra (or take away) 'tic' in there, and the page processed, and MySQL spit out a beautiful table of users!

Thanks again,
Mark

Anyango

10:45 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Mark

There are 4 characters in question

1) This is what you had and is invalid
2) This is what you had and is invalid too

3)" Double Quotes , Thats the proper one
4)' Single Quotes , Thats a proper one too

The first two, might be quotation marks in english grammer but not in computer grammer, in syntax, computers need the last two mentioned above.

so from here, forget numer 1 and number 2, we don't need em for now. let's concentrate on number 3 and 4.

if you talk about plain strings, for example.

$string1="Testing123";
$string1='Testing123';

There is No difference between them and both should work , you might ask then why your code wasnt working, thats because it didnt have the proper exact double quotes character which is this " which is found just to the bottom-left of "Enter" button on a standard 102 key, keyboard. That button has both single quote and double quote in it.


Now, you're saying that I could have done something other than single quotes as well?

Definitely Yes, you could use " too in the places where you used '

So bottom line is that for strings you can use both
Single quote ' and Double quote " but you cannot use those titled quotes


to enclose a string.

to further clarify this concept, please run this script

<?
echo "statement1";
echo "<br>";
echo 'statement2';
echo "<br>";

echo "Statements below will use those tilted quotes and will not result into what's expected<br>";

echo ”statement3”;
echo "<br>" ;
echo “statement4“;

?>


So again, it comes to a simple conlusion that you simply cannot use those tilted quotes (which you wont even find on a keyboard) to enclose strings, thats simply invalid.

you can only use those straight single quote and double quotes that you'll find on every keyboard.

and There is No difference between using Single Quote and Double Quote to enclose a string EXCEPT the following.

If the string that you are enclosing in quotes, containts a php variable then you must use Double Quotes character, if you enclose a php variable Name in a single quote character, it wont pick up its value and will simply output that as plain text. Nothing clears concepts more then an example

<?

$test="This is Value of the Variable whose name is test";

echo "<b>$test</b>"; // will work perfect and will show value

echo '<b>$test</b>'; // will not show value of $test and will simply output that as plain string

?>

I hope it's crystal clear now. it should be, atleast ;)

and if you haven't yet been able to get that code to working, check this,

-------This is with Double Quotes Character---------


<html>
<head><title>Test MySQL</title></head>
<body>
<!-- mysql_up.php -->
<?php
$host="hostname";
$user="mysqlaccount";
$password="mysqlpassword";
mysql_connect($host,$user,$password);
$sql="show status";
$result = mysql_query($sql);
if ($result == 0)
echo "<b>Error " . mysql_errno() . ": "
. mysql_error() . "</b>";
else
{
?>
<!-- Table that displays the results -->
<table border="1">
<tr><td><b>Variable_name</b></td><td><b>Value</b>
</td></tr>

<?php
for ($i = 0; $i < mysql_num_rows($result); $i++) {
echo "<TR>";

$row_array = mysql_fetch_row($result);

for ($j = 0; $j < mysql_num_fields($result); $j++)
{
echo "<TD>" . $row_array[$j] . "</td>";
}
echo "</tr>";
}

?>

</table>

<?php }?>

</body>
</html>

----and this one is with Single quote character-----


<html>
<head><title>Test MySQL</title></head>
<body>
<!-- mysql_up.php -->
<?php
$host='hostname';
$user='mysqlaccount';
$password='mysqlpassword';
mysql_connect($host,$user,$password);
$sql='show status';
$result = mysql_query($sql);
if ($result == 0)
echo '<b>Error ' . mysql_errno() . ': '
. mysql_error() . '</b>';
else
{
?>
<!-- Table that displays the results -->
<table border='1'>
<tr><td><b>Variable_name</b></td><td><b>Value</b>
</td></tr>

<?php
for ($i = 0; $i < mysql_num_rows($result); $i++) {
echo '<TR>';

$row_array = mysql_fetch_row($result);

for ($j = 0; $j < mysql_num_fields($result); $j++)
{
echo '<TD>' . $row_array[$j] . '</td>';
}
echo '</tr>';
}

?>

</table>

<?php }?>

</body>
</html>

Both of them should work perfectly, however the variable value issue that i mentioned might apply to the code with single quotes, if there is any variable being output in single quotes.


Is this a new thing in PHP5 where they are talking about a good majority of the code has changed drastically enough to where it will require some recoding if you've done much of your coding in PHP4?

Frankly, i havent even opened that link, i know what i mentioned is right, and we prolly don't need to take a look at it, but this is not at all something new, its just simple basic and you donot need to edit any of your existing quote.

You Do NOT need to change your double quotes into single quotes to "get something to work", If they are suggesting that, thats their say and i cannot comment on it.

Concluding all this, i would again say what your code had, was not a "proper double quote" character , if that was a proper double quote as " it would work 100% as i posted above.

And the search and replace i mentioned was that you should search all those tilted quotes or "inverted commas", as in english grammer, and replace them with proper double quotes or single quotes to get the syntax correct and working.

Man that was a long message, i think i need some Cold Drink, lol.

Leme know if there is something unclear still.

Kami

P.S: I type extremely fast, please ignore any typos if you see em.

Anyango

1:42 pm on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes i just read the article on the link you provided. thats perfect what he says. But please don't confuse "Enclose a string into Quotes" with "Escape single quotes" .

His post mostly is about the later and once you are clear with the basic of quotations, you can study that topic's logic as well.