Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database connection - Help please

Status
Not open for further replies.

asfaw

Programmer
Jun 28, 2004
36
CA
I am able to connect to mysql but not to a database. This is the code I am trying to execute.
Code:
<?php

// Establish a connection to the MySQL DBMS
$connection = mysql_connect("localhost", "root", "");
// Use the winestore database
mysql_select_db("winestore", $connection);

// Run a query through the connection
$result = mysql_query("SELECT cust_id, surname, firstname FROM Customer", $connection);

// Fetch each row of the results into an array $row
while ($row = mysql_fetch_array($result)
{
	print "ID:\t{$row["cust_id"]}\n";
	print "Surname\t{$row["surname"]}\n";
	print "First Name:\t{$row["firstname"]}\n\n";
}
?>
There is not result from the above code when I run it using the browser.

I was able to verify that I can connect to mysql by using the following code:
Code:
<?php
$link = mysql_connect("localhost", "root", "");
if (!$link) {
  // unable to connect to database
  echo 'Failed to connect';
}
else {
  // you were able to connect to the database
  echo 'Database connect succeeded';
}
?>
Thank you.

Asfaw
 
I solved my problem - this part of the code was incorrect:

while ($row = mysql_fetch_array($result)

There should have been a closing ).

What do I need to do the print the heading only once i.e. cust_id, surname and firstname and print the query result i.e. each row on one line.

Thank you.

Asfaw

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top