Page 1 of 1

problem with code very new to php [SOLVED]

Posted: Sun Jan 11, 2004 11:46 am
by nutstretch
I have installed an apache server and mysql and am trying to access the database through a page.

my database is called new_db and the table is customers

can some one tell me what i have done wrong here please.

Code: Select all

<html>
<head>
<title>Retrieve info from database</title>
</head>
<body>

<h2> Customers</h2>

<?php

$linkID = @mysql_connect("localhost", "", "");
mysql_select_db("new_db", $linkID);

$resultID = mysql_query("SELECT * FROM customers", $linkID);
print "<table border=1><tr><th>Customer ID</th>";
print "<th>Name></th><th>Address1</th><th>Address2</th><th>Phone</th>

while ($row = mysql_fetch_row($resultID))
{
	print "<tr>";
	foreach ($row as $field)
		{
			print "<td>$field</td>";
		}
	print "</tr>";

}
	print"</table>";

mysql_close($linkID);
?>

</body>
</html>
when i run this all i get in the browser is

Code: Select all

Customers
"; foreach ($row as $field) &#123; print "$field"; &#125; print ""; &#125; print""; mysql_close($linkID); ?&gt;
any help would be greatly appreciated

[Edit: Added php tags for eyecandy. --JAM]

Posted: Sun Jan 11, 2004 2:54 pm
by DuFF
Take a closer look at this line:

Code: Select all

print "<th>Name></th><th>Address1</th><th>Address2</th><th>Phone</th>
Looks you're missing an end quotation and semicolon. Should be:

Code: Select all

print "<th>Name></th><th>Address1</th><th>Address2</th><th>Phone</th>";

Posted: Sun Jan 11, 2004 3:09 pm
by nutstretch
thanks i have done that and i took the "" marks out round the <tr> and <td> etc as well


now i get a parse error on the last line of the page which is </html>


is there supposed to be anything else

any help greatly appreciated

if i can just make sure i am connectig with the database i think everything else will fall into place (that is supposing i have installed everything right !!!!

Posted: Sun Jan 11, 2004 4:00 pm
by basdog22
$linkID = @mysql_connect("localhost", "", "");
mysql_select_db("new_db", $linkID);


make it like :

$linkID = @mysql_connect("localhost", "", "")or die(mysql_error());
mysql_select_db("new_db", $linkID)or die(mysql_error());

Posted: Sun Jan 11, 2004 9:42 pm
by JAM
nutstretch wrote:thanks i have done that and i took the "" marks out round the <tr> and <td> etc as well
As in removed them? If so, thats bad.
Try your pasted script again, along with DuFF's and basdog's changes only.

Resolved

Posted: Mon Jan 12, 2004 9:20 am
by nutstretch
It's working now. I did a re install of the php and mysql and it worked. I think it was the " left out originally. Many thanks peeps

Angie