Kalaimaan

Doing simple things to make great life

Posts Tagged ‘PHP with MSSQL’

PHP with MSSQL Connectivity and Datamanipulation

Posted by kalaimaan on February 4, 2009

PHP with MSSql Connectivity and Datamanipulation

The following example is describe the details of insert delete and view the datas from my sql using PHP script

Note : Add line in PHP.ini

extension=php_mssql.dll

Example:
<html>
<body>

<table width=”50%” border=”2″>
<tr><td><center>Welcome to PHP script</center></td></tr>
</table>

<?php

$myServer = ‘localhost’;
$myUser = ‘sa’;
$myPass = “”;
$myDB = ’emp’;

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass);

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle) or die(“Couldn’t open database”);

//declare the SQL statement that will query the database
$query = ‘select * from empm’;

printData();

// insert details to the selected mysql table
insertData();

// delete details to the selected mysql table
deleteData();

function printData()
{
//execute the SQL query and return records
$result = mssql_query(‘select * from empm’);

/*$numRows = mssql_num_rows($result);
echo “<h1>” . $numRows . ” Row” . ($numRows == 1 ? “” : “s”) . ” Returned </h1>”;

//display the results
while($row = mssql_fetch_array($result))
{
echo “<li>” . $row[’empid’] . $row[‘name’] . $row[‘pno’] . “</li>”;
}*/

echo “<br><table border=’1′ width=’50%’>”;
echo “<tr> <th>Id</th> <th>Name </th> <th>Phone No. </th> </tr>”;

while($row = mssql_fetch_array($result))
{
echo “<tr><td>” . $row[’empid’] . “</td><td>” . $row[‘name’] . “</td><td>” . $row[‘pno’] . “</td>” . “</tr>”;
}

echo “</table>”;
}

function insertData()
{
// Insert a row of information into the table “example”
mssql_query(“INSERT INTO empm (empid,name,pno) VALUES(‘0006′,’Sri’,’98418′ ) “);
echo “<br><b>Data Inserted!</b>”;
printData();
}

function deleteData()
{
mssql_query(“Delete from empm where empid =’0006′”);
echo “<br><b>Data Deleted!</b>”;
printData();
}

?>

</body>
</html>

mssql

Posted in PHP | Tagged: , , , , | Leave a Comment »