Database connections using PHP and MS Access
1. Display list of records from MS Access database to PHP web page. (for this example you need to create a DSN from control panel, to create DSN click here)
<html> <body> <h1> Connecting to Ms Access using PHP </h1> <hr> <?php $conn=odbc_connect('acc_php','',''); if (!$conn) { exit("Connection Failed: " . $conn); } $sql="SELECT * FROM Cust"; $rs=odbc_exec($conn,$sql); if (!$rs) { exit("Error in SQL"); } echo "<table border=1><tr>"; echo "<th>Customer Code</th>"; echo "<th>Customer Name</th>"; echo "<th>Address</th></tr>"; while (odbc_fetch_row($rs)) { $c_code=odbc_result($rs,"ccode"); $c_name=odbc_result($rs,"cname"); $c_addr=odbc_result($rs,"caddr"); echo "<tr><td>$c_code</td>"; echo "<td>$c_name</td>"; echo "<td>$c_addr</td></tr>"; } odbc_close($conn); echo "</table>"; ?> </body> </html>
2. PHP and MySQL Database connection
<?php $con=mysql_connect("localhost","root", ""); if(!$con) { die('could not connect'. mysql_error()); } mysql_select_db("mars_db",$con); $result=mysql_query("select * from emp"); echo "<h1> Database connection...................</h1>"; echo"<table border=1>"; echo"<tr><th>Employee Number</th><th>EmpName</th> <th>Address </th> <th>Phone </th> <th>Salary </th>"; while ($row=mysql_fetch_array($result)) { echo"<tr>"; echo "<td>".$row['empno']."</td>"."<td>".$row['ename']."</td>" ."<td>".$row['addrs']."</td>" ."<td>".$row['phone']."</td>" ."<td>".$row['salary']."</td>"; echo "</tr>"; } echo"</table>"; mysql_close($con); ?>
3. Searching records using PHP MYSQL
<?php $searchTerm = trim($_GET['keyname']); if($searchTerm == "") { echo "Enter code you are searching"; exit(); } $host = "localhost"; $db = "mars_db"; $user = "root"; $pwd = ""; $link = mysqli_connect($host, $user, $pwd, $db); $query = "SELECT * FROM emp_db WHERE empno LIKE '%$searchTerm%'"; $results = mysqli_query($link, $query); if(mysqli_num_rows($results) >= 1) { $output = ""; while($row = mysqli_fetch_array($results)) { $output .= "Employee Number: " . $row['ecode'] . "<br />"; $output .= "Name: " . $row['ename'] . "<br>"; $output .= "Address: " . $row['sal'] . "<br>"; } echo $output; } else echo "There was no matching record for the name " . $searchTerm; ?>
4. Inserting records from html page through php to mysql database.
a) First create following html file
<html> <body> <h1> Inserting Records - PHP MySQL </h1> <form action="process5.php" method="POST"> Name: <input type="text" name="name" /><br /> DOB : <input type="text" name="dob" /><br /> Gender : <select name="gender"> <option value="M">Male</option> <option value="F">Female</option> </select><br /> Country : <select name="country"> <option value="USA">United States of America</option> <option value="Australia">Australia</option> <option value="Canada">Canada</option> <option value="India">India</option> </select><br /> <input type="submit" value="Submit Info" /> </form> </body> </html>b) Now create a php file called "process5.php" with following code.
<?php $nm=$_POST['name']; $db=$_POST['dob']; $gn=$_POST['gender']; $cnt=$_POST['country']; $host="localhost"; $username="root"; $password=""; $dbname="mars_db"; $con=mysql_connect("$host","$username","$password"); mysql_select_db("$dbname",$con); mysql_query("INSERT INTO dts (ename, dob, gen, cntry) VALUES ('$nm','$db','$gn', '$cnt')"); echo "<h1> One record added </h1>"; mysql_close($con); echo "<img src='MI LOGO.png'>"; ?>
No comments:
Post a Comment