Tag Archives: MySQL
Command to zip a folder – linux ubuntu
zip folder-name *
It compress all the files in the respective folder and create an archive folder-name.zip
zip – r folder-name *
It compress all the data including its sub folder contents
pagination with php and ajax
1) Create a file and enter the database details .let it be “db.php”
<?php
mysql_connect(“localhost”,”root”,”password”);
mysql_select_db(“store”);
?>
2) Create a page to list the details . here it is “ajax.php”
<?php
include(“db.php”);
$itemPerPage=3;
$totalCategories=mysql_num_rows(mysql_query(“SELECT category_id as total FROM category”));
$lastPage =ceil($totalCategories/$itemPerPage);
$categoryResult=mysql_query(“SELECT * FROM category ORDER BY category_id DESC LIMIT $itemPerPage”);
?>
<script type=”text/javascript”>
function getNextPage(pageno){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
alert (“Your browser does not support AJAX!”);
return;
}
var url=”next-page.php?”;
url=url+”&page_no=”+pageno;
url=url+”&sid=”+Math.random();
//alert(url);
xmlHttp.onreadystatechange=stateChangednextPage;
xmlHttp.open(“GET”,url,true);
xmlHttp.send(null);
}
function stateChangednextPage(){
if (xmlHttp.readyState==4)
document.getElementById(“nextPage”).innerHTML=xmlHttp.responseText;
}
if(!window.GetXmlHttpObject) {
function GetXmlHttpObject(){
var xmlHttp=null;
try {// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e) {// Internet Explorer
try {
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e){
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}
}
</script>
<div id=”nextPage”>
<table>
<tr style=”font-weight:bold”>
<td>ID</td><td>Name</td><td>Added Date</td>
</tr>
<?php while($category=mysql_fetch_assoc($categoryResult)){ ?>
<tr style=”background:#CCCCCC”>
<td><?php echo $category[‘category_id’] ?></td><td><?php echo $category[‘category_name’] ?></td><td><?php echo $category[‘category_added_date’] ?></td>
</tr>
<?php }?>
<tr>
<td> </td><td> </td>
<td align=”right”><a href=”javascript:getNextPage(<?php echo $lastPage; ?>);”>Previous </a>| <a href=”javascript:getNextPage(2)”>Next</a></td>
</tr>
</table>
</div>
3) Create the page that run behind the scene and past the below given code . Here its “ext-page.php”
<?php
include(“db.php”);
$itemPerPage=3;$pageNo=$_REQUEST[‘page_no’];
$totalCategories=mysql_num_rows(mysql_query(“SELECT category_id as total FROM category”));
$lastPage =ceil($totalCategories/$itemPerPage);
$start = ($pageNo-1)*$itemPerPage;
if($lastPage<$pageNo){ $start=0;$pageNo=1;}
if($totalCategories<$start){ $start=0;$pageNo=1; }
if($start<0){$pageNo=$lastPage;$start= ($lastPage-1)*$itemPerPage;}
$categoryResult=mysql_query(“SELECT * FROM category ORDER BY category_id DESC LIMIT $start,$itemPerPage”);
?>
<table>
<tr style=”font-weight:bold”>
<td>ID</td><td>Name</td><td>Added Date</td>
</tr>
<?php while($category=mysql_fetch_assoc($categoryResult)){ ?>
<tr style=”background:#CCCCCC”>
<td><?php echo $category[‘category_id’] ?></td><td><?php echo $category[‘category_name’] ?></td><td><?php echo $category[‘category_added_date’] ?></td>
</tr>
<?php }?>
<tr>
<td> </td><td> </td>
<td align=”right”><a href=”javascript:getNextPage(<?php echo $pageNo-1; ?>);”>Previous </a>| <a href=”javascript:getNextPage(<?php echo $pageNo+1; ?>)”>Next</a></td>
</tr>
</table>
4) Load the page ajax.php in your browser
Find the total records in a mysql table – PHP
mysql_connect(“localhost”,”root”,”password”);
mysql_select_db(“store”);
$total=mysql_num_rows(mysql_query(“SELECT category_id as total FROM category”));
echo $total;
?>
List databases – PHP
<?php
mysql_list_dbs();
Example :
$connection=mysql_connect(“localhost”,”root”,”password”);
$dataBases = mysql_list_dbs($connection);
while ($db = mysql_fetch_object($dataBases)) {
echo $db->Database . “<br />”;
}
?>
Get number of affected rows in mysql operation – PHP
<?php
mysql_affected_rows();
?>
Close MySQL connection – PHP
<?php
mysql_close();
?>
Select MySQL database – PHP
<?php
mysql_select_db (DB-NAME)
?>
Create a Connection to a MySQL Database – PHP
mysql_connect(SERVER-NAME,USER-NAME,PASSWORD);
eg:
<?php
mysql_connect(“localhost”,”root”,”user1″);
?>
Command to check the status of MySql server – Ubuntu linux
service mysql status EX:service mysql status mysql start/running, process 1300