tags:

views:

202

answers:

3

Hello, I am new to PHP and trying to get the following code to work:

<?php
include 'config.php';
include 'opendb.php';

$query = "SELECT name, subject, message FROM contact";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "Name :{$row['name']} <br>" .
         "Subject : {$row['subject']} <br>" . 
         "Message : {$row['message']} <br><br>";
"ARTICLE_NO :{$row['ARTICLE_NO']} <br>" .
"ARTICLE_NAME :{$row['ARTICLE_NAME']} <br>" .
"SUBTITLE :{$row['SUBTITLE']} <br>" .
"CURRENT_BID :{$row['CURRENT_BID']} <br>" .
"START_PRICE :{$row['START_PRICE']} <br>" .
"BID_COUNT :{$row['BID_COUNT']} <br>" .
"QUANT_TOTAL :{$row['QUANT_TOTAL']} <br>" .
"QUANT_SOLD :{$row['QUANT_SOLD']} <br>" .
"STARTS :{$row['STARTS']} <br>" .
"ENDS :{$row['ENDS']} <br>" .
"ORIGIN_END :{$row['ORIGIN_END']} <br>" .
"SELLER_ID :{$row['SELLER_ID']} <br>" .
"BEST_BIDDER_ID :{$row['BEST_BIDDER_ID']} <br>" .
"FINISHED :{$row['FINISHED']} <br>" .
"WATCH :{$row['WATCH']} <br>" .
"BUYITNOW_PRICE :{$row['BUYITNOW_PRICE']} <br>" .
"PIC_URL :{$row['PIC_URL']} <br>" .
"PRIVATE_AUCTION :{$row['PRIVATE_AUCTION']} <br>" .
"AUCTION_TYPE :{$row['AUCTION_TYPE']} <br>" .
"INSERT_DATE :{$row['INSERT_DATE']} <br>" .
"UPDATE_DATE :{$row['UPDATE_DATE']} <br>" .
"CAT_1_ID :{$row['CAT_1_ID']} <br>" .
"CAT_2_ID :{$row['CAT_2_ID']} <br>" .
"ARTICLE_DESC :{$row['ARTICLE_DESC']} <br>" .
"DESC_TEXTONLY :{$row['DESC_TEXTONLY']} <br>" .
"COUNTRYCODE :{$row['COUNTRYCODE']} <br>" .
"LOCATION :{$row['LOCATION']} <br>" .
"CONDITIONS :{$row['CONDITIONS']} <br>" .
"REVISED :{$row['REVISED']} <br>" .
"PAYPAL_ACCEPT :{$row['PAYPAL_ACCEPT']} <br>" .
"PRE_TERMINATED :{$row['PRE_TERMINATED']} <br>" .
"SHIPPING_TO :{$row['SHIPPING_TO']} <br>" .
"FEE_INSERTION :{$row['FEE_INSERTION']} <br>" .
"FEE_FINAL :{$row['FEE_FINAL']} <br>" .
"FEE_LISTING :{$row['FEE_LISTING']} <br>" .
"PIC_XXL :{$row['PIC_XXL']} <br>" .
"PIC_DIASHOW :{$row['PIC_DIASHOW']} <br>" .
"PIC_COUNT :{$row['PIC_COUNT']} <br>" .
"ITEM_SITE_ID :{$row['ITEM_SITE_ID']};
 }

include 'closedb.php';

?>

However I get this error:

Parse error: syntax error, unexpected $end in C:\Programme\EasyPHP 2.0b1\www\test.php on line 56

I would also like to know if there is perhaps an easier way to obtain mysql records instead of typing by hand?

edit:

I fixed the semicolon and quote issue, and now get:

Parse error: syntax error, unexpected T_STRING in C:\Programme\EasyPHP 2.0b1\www\test.php on line 51

I am sorry I don't know how to make colors in the code.

+8  A: 

Edit

You say that you're still getting an error. Did you remember to add a . when you removed that extra semi-colon?


You have a semi-colon in the middle of your string, two lines after the echo.

Also, the end of the string is missing a double-quote.


As far as a cleaner way to output all the values goes, you can loop over the result array like this:

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
  foreach($row as $field=>$value)
  {
    echo "$field: {$value} <br />";
  }
}
Mark Biek
Also, HTML injection holes leading to cross-site scripting vulnerabilities. Always wrap text output in htmlspecialchars().
bobince
A good point. Although I think it overcomplicates this example since we don't have any idea where the database values actually came from.
Mark Biek
I know this opens a huge can of worms, but I think it's that very mentality that breeds thousands of PHP rookies who know absolutely nothing about security. Teach security first. If they decide their situation doesn't require it, they can pull it out.
Lucas Oman
+2  A: 

For the second part of your question, you could do this if field names are all logical:

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
  foreach($row as $key => $value)
  {
    echo "$key: $value\n";
  }
}
Kip
A: 

Edit: Your SQL query only returns 3 fields. But you try to access a lot more than that. Try "SELECT *" if you want to return all the fields of a table. Otherwise make sure you select the fields you try to read (probably not why you get an error though, just an observation).

The syntax coloring of

include 'closedb.php';

?>

indicates that the string lacks a closing quote. The line

"ITEM_SITE_ID :{$row['ITEM_SITE_ID']};

confirms that.

Also the line

"Message : {$row['message']} <br><br>";

ends the string concatenation. The semi-colon should probably be a period.

jakber