I'm using HostMonster as my web host and I'm trying connect to a database I created using MySQL inside of HostMonster. In order to call that database in my website do I need to use PHP? Or is there a way to create a javascript OnClick function that can call the database. I'm not using ASP.Net so it's not quite as simple as I would like it. Just curious if the best solution is PHP, if so I guess I should go learn it.
4 Answers
what are you planning to do with the database, other than just 'calling it'? You will need some language like PHP to connect to the DB to retrieve, insert, update or delete data in the DB.

- 10,816
- 4
- 33
- 50
-
I'm planning on doing all the above. inserting, updating, and deleting. I now understand that I do need to use php, but I'm not sure if I use both .html files and .php files or if I just make everything .php . I'm working on a login page where a user enters his/her name and password and then I want to use php to varify the credentials and send them to a new page. I don't know if I use javascript with this that will just call the php --> the php verifies --> and then sends to the next .html page. Or if there is a more efficient way about this. – Weston Jun 17 '11 at 15:04
-
@weston naming a page with a .php extension means that the page can contain PHP code along with your standard HTML stuff, provided your webserver supports PHP. You can rename test.html to test.php and it would work just fine. The purpose of PHP is to help you generate the HTML you need for the site using dynamic data source like a database. It would also help you put the data in the database. It is NOT a replacement of HTML, it just assists in generating it. – Sabeen Malik Jun 17 '11 at 20:39
-
@weston You don't really need javascript for login form, maybe just for validation. On clicking submit on your form, the data will be sent to a page which has PHP which checks that data against a DB, if all is well, sends to users welcome page otherwise asks for login details again. You will need to look into `sessions` if you want to implement a secure site accessed by username and password. Hope this helps get you started. Search on google and you will find plenty of good tutorials on the subject. – Sabeen Malik Jun 17 '11 at 20:41
here is a code for connection MySQL from PHP using MYSQLI extension
<?php
$dba_host='localhost';
$dba_name='root';
$dba_pass='';
$dba_db='sn';
$con=mysqli_connect($dba_host,$dba_name,$dba_pass,$dba_db) or die('Connection Refused !');
$stmt=mysqli_prepare($con,"SELECT UID FROM Main");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $value);
while(mysqli_stmt_fetch($stmt))
$result[] = $value;
mysqli_stmt_close($stmt);
mysqli_close($con);
?>

- 17,065
- 35
- 101
- 159
Your javascript onClick
function is running on the client side (in the browser) and the database is running on the server-side. You will need a server-side language to get the information from the database and send it to the browser.

- 91,079
- 21
- 114
- 132
You do not HAVE to use PHP to connect to a MYSQL database. Also, you can't connect to your database using only client-side javascript (ie. an onClick()
function). You need to use a server side language, PHP is one choice.
To connect to a MYSQL database on hostmonster using PHP you will need to know your credentials that use to log into phpMyAdmin from your cpanel. Once you have made the connection you can then select the MYSQL database that you created. Once the database is selected you can query it using the "mysql_query" function in PHP. The following code does all of that and stores the results of the MYSQL query in a PHP variable called $result.
<?php
$con = mysql_connect("www.yourdomain.com","phpMyAdmin_username","phpMyAdmin_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql_database_name", $con);
$query = "SELECT * FROM TableName"
$result = mysql_query($query);
?>
Now you've got the results of the query inside the PHP variable $result and you can use it anyway you like.
If you put this in your 'public_html' folder and named it 'index.php' or 'index.html' this would automatically be run when someone went to www.yourdomain.com.
You can find a great tutorial series on PHP here http://thenewboston.org/list.php?cat=11.

- 4,238
- 9
- 41
- 69