3

I am a complete database newbie. So far, I know that I can connect to MySQL using PHP's mysql_connect() command, but apart from that, I really don't see how to take that data and put it onto a web page.

a) are there ways other than mysql_connect()

b) lets say I had a table of data in mysql and all I wanted was for that table (for example: list of names and telephone numbers) to now appear on my web page. I can't for the life of me find a tutorial for this.

2
  • ad a: there are basically two ways to connect to MySQL database, but both of them are very similar in function: the mysql extension and the mysqli extension ("i" stands for "improved"). Commented Jul 2, 2010 at 14:42
  • 2
    (also, edited out the intro - you have nothing to apologize for, everyone was a newbie once :)) Commented Jul 2, 2010 at 15:22

1 Answer 1

3
<?
$database_name = "dbname";
$mysql_host = "localhost"; //almost always 'localhost'
$database_user = "dbuser";
$database_pwd = "dbpass";
$dbc = mysql_connect($mysql_host, $database_user, $database_pwd);
if(!$dbc)
{
    die("We are currently experiencing very heavy traffic to our site, please be patient and try again shortly.");
}
$db = mysql_select_db($database_name);
if(!$db)
{
    die("Failed to connect to database - check your database name.");
}

$sql = "SELECT * FROM `table` WHERE `field`= 'value'";
$res = mysql_query($sql);

while($row = mysql_fetch_assoc($res)) {
    // code here
    // access variables like the following:
    echo $row['field'].'<br />'; 
    echo $row['field2'];
}
?>

Check out mysql_fetch_assoc mysql_fetch_array and mysql_fetch_object

This is the very basics, you will want to search for tutorials. There are many about.

Sign up to request clarification or add additional context in comments.

1 Comment

Typo on your select statement. "SELEET" becomes "SELECT."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.