0

Ok, I've just started learning php, and for some reason the mysql_connect() and mysql_select_db() do not appear to connect to the database. I also need a good example of a mysql table, as I am not sure what to put in my table. How do I manage to connect my site to the database?! Here is the code I am using in my connect.php page.

    <?php

    //not working
    $connect = mysql_connect('localhost',
    'myuser',  'password', 'mypass')
    or die("This function should work.");

    //still a lingering error

    $db_select = mysql_select_db("mydbname", $connect);
    ?>
Stedy
  • 7,359
  • 14
  • 57
  • 77
CJ101
  • 3
  • 1

1 Answers1

1

If you just start with PHP then a good idea would be to get Apache and MySQL on your own computer. There are plenty of easy installable packages like WAMP (http://www.wampserver.com/en/). There are examples and HOWTOs there. The good in this package, is that there is a PHPMyAdmin that comes packed, and you can use it to make a sample table, or just to play around.

Next, for the concrete code, there is a nice example at the PHP documentation site (http://php.net/manual/en/function.mysql-connect.php).

<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
?>

When using the local server you can use the ROOT user in MySQL. For a test table you can make one called, for example friends, with two VARCHAR fields for name and phone number. And you can play around to list them edit them and so on.

Good luck.

Djumaka
  • 500
  • 4
  • 10