The basic prototype of the connection is this:
Code: Select all
$dbResource = mysql_connect(host, username, password, database);
Host is the machine where the db resides. Your webhost will give that to you.
Username is the username to sign into your db with. Depending on your host, they will either give this to you, or you will have limited ability to create your own username.
Password is the password for the database. You set this up through your control panel.
Database is the database you want to connect to.
Alternatively, as you did in your code, you can leave off the last parameter (database) and select it later with mysql_select_db(database), but I think it is easier just to select it with the initial statement.
It looks like your issue is the password. You should have some kind of control panel on your webhost account. If you are having problems finding it or setting it up, you may need to contact your webhost. However, 000webhost is pretty common, so maybe someone else around here uses them and can give you more info.
Ohhh, I also just noticed that you have
die("string") parameters in IF statements. Remove those.
die() should only be used in actual database connection statements. Change your code to:
Code: Select all
<?php
@ $db = mysqli_connect("yourHost", "yourUserName", "yourPassWord", "database") or die("Could not connect to db");
if (!$db) {
echo 'No database resource';
exit;
}
if (mysqli_connect_errno()) {
echo 'Error in connecting to database.';
exit;
}
Add the "@" at the beginning of the database connection. This suppresses errors, so that you can give better error messages. It is also a security issue to allow users to see your database errors (it can give them the database name). The second part, mysqli_connect_errno() is to find out if any other errors occurred while connecting to the db. Also, the use of "mysqli_..." instead of "myql_" is to use the more modern MySQL PHP functions.