Simple SELECT statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jbriskin
Forum Newbie
Posts: 6
Joined: Mon Feb 11, 2008 3:55 pm
Location: Texas

Simple SELECT statement

Post by jbriskin »

The following code is just for testing... not worried about security etc at this point...

All I want to do is select all the records from a table and display the results.

my code is:

Code: Select all

<?php
 
$doc_root = $_SERVER['DOCUMENT_ROOT'];
include("$doc_root/constants.php");
 
mysql_connect ($db_host, $db_user, $db_pass);
mysql_select_db ($db_name);
$query = "SELECT * FROM formsubmit";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
while ($row = mysql_fetch_assoc($result)) {
    echo $row['timeStamp'];
    echo $row['NameFirst'];
    echo $row['NameLast'];
    echo $row['NameCompany'];
    echo $row['Address01'];
    echo $row['Address02'];
    echo $row['City'];
    echo $row['State'];
    echo $row['ZipCode'];
    echo $row['Email'];
    echo $row['PhoneWork'];
    echo $row['PhoneCell'];
    echo $row['PhoneFax'];
    echo $row['Comments'];
    echo $row['RemoteAddress'];
}
//mysql_free_result($result);
?>
My result is:

Code: Select all

Parse error: parse error, unexpected T_STRING in FormData.php on line 11
What am I missing?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Simple SELECT statement

Post by Stryks »

Good question actually. I only have time for a quick glance, but I cant see it.

Maybe a silly question, but you're sure that is FormData.php you posted?

Also, I believe that you need to remove line 10 to prevent discarding the first returned row of data.
jbriskin
Forum Newbie
Posts: 6
Joined: Mon Feb 11, 2008 3:55 pm
Location: Texas

Re: Simple SELECT statement

Post by jbriskin »

Yes, that is FormData.php... was suppose to be a quick test... but it is now not quick and seems to test only that I am not a php coder.

I got the syntax online from a sample. I just made it point to my specifics. I don't really understand all of what it is doing... but it looks straight forward.. but line 10 is suspicious now that you point it out...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Simple SELECT statement

Post by John Cartwright »

The error most likely exists on your included files.. there certainly is no parse error on the code you've posted.
jbriskin
Forum Newbie
Posts: 6
Joined: Mon Feb 11, 2008 3:55 pm
Location: Texas

Re: Simple SELECT statement

Post by jbriskin »

Jcart wrote:The error most likely exists on your included files.. there certainly is no parse error on the code you've posted.
There is some HTML Style tags in the constants.php... I guess that could be an issue... otherwise it is just a bunch of variables... that have been in use for about a year... but it is worth checking out.... thank you.
jbriskin
Forum Newbie
Posts: 6
Joined: Mon Feb 11, 2008 3:55 pm
Location: Texas

Re: Simple SELECT statement

Post by jbriskin »

I pulled out the includes now the code looks like this:

Code: Select all

<?php
$db_host = 'my.server.net';
$db_name = 'myDataBase';
$db_user = 'myUser';
$db_pass = 'myPass';
mysql_connect ($db_host, $db_user, $db_pass);
mysql_select_db ($db_name);
$query = "SELECT * FROM formsubmit";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
while ($row = mysql_fetch_assoc($result)) {
    echo $row['timeStamp'];
    echo $row['NameFirst'];
    echo $row['NameLast'];
    echo $row['NameCompany'];
    echo $row['Address01'];
    echo $row['Address02'];
    echo $row['City'];
    echo $row['State'];
    echo $row['ZipCode'];
    echo $row['Email'];
    echo $row['PhoneWork'];
    echo $row['PhoneCell'];
    echo $row['PhoneFax'];
    echo $row['Comments'];
    echo $row['RemoteAddress'];
}
?>
And my result is:

Code: Select all

 
Parse error: parse error, unexpected T_STRING in FormData.php on line 11
 
To state the obvious, I don't get it...
Festy
Forum Commoner
Posts: 28
Joined: Wed Jan 30, 2008 10:01 pm

Re: Simple SELECT statement

Post by Festy »

Is multiple declaration of $row on line 10 and 11 causing the problem? :D I'm not sure though.
Last edited by Festy on Mon Feb 11, 2008 10:12 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Simple SELECT statement

Post by s.dot »

jbriskin wrote:I pulled out the includes now the code looks like this:

Code: Select all

<?php
$db_host = 'my.server.net';
$db_name = 'myDataBase';
$db_user = 'myUser';
$db_pass = 'myPass';
mysql_connect ($db_host, $db_user, $db_pass);
mysql_select_db ($db_name);
$query = "SELECT * FROM formsubmit";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
while ($row = mysql_fetch_assoc($result)) {
    echo $row['timeStamp'];
    echo $row['NameFirst'];
    echo $row['NameLast'];
    echo $row['NameCompany'];
    echo $row['Address01'];
    echo $row['Address02'];
    echo $row['City'];
    echo $row['State'];
    echo $row['ZipCode'];
    echo $row['Email'];
    echo $row['PhoneWork'];
    echo $row['PhoneCell'];
    echo $row['PhoneFax'];
    echo $row['Comments'];
    echo $row['RemoteAddress'];
}
?>
And my result is:

Code: Select all

 
Parse error: parse error, unexpected T_STRING in FormData.php on line 11
 
To state the obvious, I don't get it...
In this EXACT code that you've posted, there aren't any parsing errors. I've copied it into my editor and ran it on localhost in my web browser. The results?

Code: Select all

 
Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'my.server.net' (11004) in C:\Apache2\Apache2\htdocs\crap\testing.php on line 6
 
Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Apache2\Apache2\htdocs\crap\testing.php on line 7
 
Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in C:\Apache2\Apache2\htdocs\crap\testing.php on line 7
 
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Apache2\Apache2\htdocs\crap\testing.php on line 9
 
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\Apache2\Apache2\htdocs\crap\testing.php on line 9
Access denied for user 'ODBC'@'localhost' (using password: NO)
Those errors are just because I don't have the database set up. :P It got past the parser so there's no parsing errors. Perhaps an include or something is causing the error. The file that the error is on is formData.php. Are you sure you're posting the formData.php code?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Simple SELECT statement

Post by Stryks »

Just for fun, create a new file and copy the code as last posted (with the includes removed) into it, save and give it a run.
jbriskin
Forum Newbie
Posts: 6
Joined: Mon Feb 11, 2008 3:55 pm
Location: Texas

Re: Simple SELECT statement

Post by jbriskin »

ok... very odd. Starting at line 10, none of the spaces are actually spaces. I turned on "show hidden characters" and what should be spaces are not denoted the same way as actual spaces. I guess I could throw it in a hex reader and find out but ....

So anyway, thanks to everyone showing me the actual code was right... I simply deleted the bad "spaces" and replaced them with good "spaces"... Now it works. As expected...

Again, thanks for the help on getting my first ever select statement to work...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Simple SELECT statement

Post by s.dot »

I suspect you're using a bad editor.. such as one that inserts hidden characters.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
jbriskin
Forum Newbie
Posts: 6
Joined: Mon Feb 11, 2008 3:55 pm
Location: Texas

Re: Simple SELECT statement

Post by jbriskin »

I use bbedit. The code that had the bad spaces was cut and paste from the web. The code I typed out myself had no issues... but I'll leave the option for showing hidden characters on for a bit.
Post Reply