|
|
#1 (permalink) |
|
Registered User
Seasoned Poster
Joined in Feb 2006
Lives in In Front of My Computer, in the UK
Hosted on SH92/Pass59
32 posts
Gave thanks: 2
Thanked 0 times
|
Hi All,
I am having a little problem with PHP as I have just started to use this after 3 years of ASP and 2 of ASP.NET . if you could tell me why my code is not echoing the site_message field it would help me a great deal Code:
<?php
// Connecting, selecting database
$link = mysql_connect('localhost', 'xbitwan_user', 'password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('xbitwan_db') or die('Could not select database');
// Performing SQL query
$remotehost = $_SERVER["HTTP_HOST"];
$query = "SELECT site_status, site_message FROM sites where (site_url='$remotehost')";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>Site Message: $line['site_message']</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
Dean
__________________
Dean (2BitWannabe Admin) Site: http://www.2bitwannabe.com Server: SH92(72.29.92.190) Site: http://www.twobitwannabe.org Server: Pass59(72.29.89.118) C|EH (Certified Ethical Hacker)
|
|
|
|
|
|
#3 (permalink) |
|
All Ur Base R Belong 2 Us
Excelling Contributor
Joined in Feb 2005
Lives in Vegas & New York
824 posts
Gave thanks: 2
Thanked 6 times
|
Hey 2bit
You have three options: escape out of the double quotes for the variable, remove the single quotes around site_message, or add {} around $line['site_message'] i.e. echo "\t\t<td>Site Message: ".$line['site_message']."</td>\n"; echo "\t\t<td>Site Message: $line[site_message]</td>\n"; echo "\t\t<td>Site Message: {$line['site_message']}</td>\n"; Explanation: Since you are currently in double quotes, any key inside $array_var[key] is read as a string, not a constant. the use of the single quotes is not expected and causes a parse error. You don't need them. However, if you want to use the single quotes for consistency, the use of {} around a variable inside quotes causes it to be read the same way it would be outside of the quotes. Additionally, you don't need the foreach statement around the echo statement. ------------- And to David, \n as you know is new line, \t stands for tab, and \r stands for carriage return And a snippet of Geek knowledge: On windows, new lines in text files are \r\n On mac, new lines in text files are \r On linux, new lines in text files are \n
__________________
Nobody doing nothing |
|
|
|