icon Get the most out of Surmunity, read our tips here! Need an interesting blog to read? You've got to read the Surpass Blog! | Welcome! Please register to access all of our features.
Old February 7th, 2006, 2:35 PM   #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
Post Help using PHP with MySQL

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);
?>
Thanks in Advance
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)
2BitWannabe is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 7th, 2006, 5:31 PM   #2 (permalink)
I'm Dope as Crack
Resident.
 
David's Avatar
 
Joined in Mar 2004
Lives in Asheboro, NC
Hosted on Pass 7
13,036 posts
Gave thanks: 7
Thanked 29 times
what is \t? I know what \n does but what does \t do?
__________________
David is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 7th, 2006, 6:56 PM   #3 (permalink)
All Ur Base R Belong 2 Us
Excelling Contributor
 
mr_fern's Avatar
 
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
mr_fern is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 7th, 2006, 9:46 PM   #4 (permalink)
I'm Dope as Crack
Resident.
 
David's Avatar
 
Joined in Mar 2004
Lives in Asheboro, NC
Hosted on Pass 7
13,036 posts
Gave thanks: 7
Thanked 29 times
Ah, thanks for the explination. It's hard to google things like that.
__________________
David is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On