problem with code very new to php [SOLVED]

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

problem with code very new to php [SOLVED]

Post 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]
Last edited by nutstretch on Mon Jan 12, 2004 4:04 pm, edited 2 times in total.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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>";
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post 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 !!!!
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post 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());
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Resolved

Post 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
Post Reply