Click here to Skip to main content
15,908,834 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi....
I want to connect MySQL database in my HTML page. I want to insert and retrieve data using MySQL database which I have created using phpMyadmin in WAMP server. How can it possible using HTML 5. I saw many examples for creating table and insert, delete etc. But first I have to connect the database to my HTML page. Then insert data into the database and retrieve data from it. I also want to connect a remote database in my HTML page. I searched on google and none of them helped me. If you have any idea, please share.

Thank you....
Posted
Updated 7-Dec-17 19:03pm
Comments
Mohibur Rashid 11-Aug-12 13:47pm    
what was the search string you used?

and besides, you cannt connect mysql with html you need to use php

1 solution

I think you can't connect MySQL database in html page directly but there is some way to hiding the backhand/server side scripting code's and display html pages. now I am talking about php(because of WAMP).
Create a php file first to make connection to mysql
for example connect.php
PHP
<?
$host="localhost"; // Host name.
$db_user="root"; // MySQL username.
$db_password=""; // MySQL password.
$database="test"; // Database name.
$link = mysql_connect($host,$db_user,$db_password);
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
else
{
echo "Mysql Connected Successfully";
}

mysql_close($link);
?>

and Create a Index.html using ajax
HTML
<html>
<head>
<script type="text/javascript">
function checkmyconnect()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","connect.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>Let Connect to mysql in html file</h2>
<div id="myDiv">No connection</div>
<button type="button"  önclick="checkmyconnect()">Check Mysql connection</button>

</body>
</html>

and test now
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900