Thursday, June 26, 2014

print data by reusable code in PHP


database namd : student table name : stud table name : course Def: print data by reusable code and table name as per given can have any fields

<?php
$link=mysql_connect("localhost","root","");
mysql_select_db("student");
$res=mysql_query("select * from stud");
printdata($res);

echo "<hr>";
$res=mysql_query("select * from tblcourse");
printdata($res);

function printdata($datas)
{
echo "<table border=1>";
while($row=mysql_fetch_array($datas,MYSQL_NUM))
{
echo "<tr>";
foreach($row as $k=>$v)
{
echo "<td>",$v,"</td>";
}
echo "</tr>";
}
echo "</table>";

}
?>


Monday, June 23, 2014

mysql with php connection sample programme


database name :dbstud
table name : tblstud
fields
rno tinyint autoincrement and primary key
name varchar 20
gender enum "m","f"
city varchar 20

<?php
$link=mysql_connect("localhost","root","");

$db=mysql_select_db("dbstud");

$res=mysql_query("select *from tblstud");
echo $res,"<BR>";
echo "<table border=1>";
while($row=mysql_fetch_array($res,MYSQL_NUM))
{
echo "<TR bgcolor='yellow'>";
foreach($row as $k=>$v)
{

if($k == 2)
{
if($v=="f")
{
echo "<td bgcolor='pink'>Female</td>";
}
else
{
echo "<td bgcolor='cyan'>Male</td>";
}
}
else
{
echo "<td>",$v,"</td>";
}

}

echo "</TR>";
}
echo "<table>";
?>