Powered By
Showing posts with label Mysql. Show all posts
Showing posts with label Mysql. Show all posts

Previous & Next Functionality with PHP, AJAX, JQuery and MySQL

View Comments
add to del.icio.us
saved by 0 users
Here i have designed a very simple application to fetch data from mysql server with php, through Ajax. In this application i have used two image input component for previous and next functionality and a table to display the data. Here is the preview of the application.
On clicking the next button, next set of data with the specified limit is fetched from the database through Ajax and displayed in the table, vice versa for the previous button. The limit parameter can be changed at any time by changing the limit value.


You can download the application:
In db.php file, you can change the limit of data to fetch from the server. The design goes in the index.php file and the core of the application lies in the prevnext.js file.

On clicking the next button, the ajax function is called through this JQuery function

$("#next").click(function(){
var current = parseInt($("#currenthid").val()) + limit;
var data="current="+current;
$.ajax({
type:"POST",
url:"ajaxpost.php",
data:data,
cache:false,
success:function(html){
$("#showtable").html(html);
$("#currenthid").val(current);
checkVisible();
}
});
});
It sends the index of the next set of data to fetch, to the ajax function. On success the data in the view is updated and the index of current data set is updated. The checkVisible function is to ensure the next previous functionality. i.e, the Next button will be disables on reaching the last data set and the previous button will be disable on reaching the first data set.
  function checkVisible(){
var current = parseInt($("#currenthid").val());
var total = parseInt($("#totalhid").val());
$("#previous").css('display','block');
$("#next").css('display','block');
if(current <>= total) {
$("#next").css('display','none');
}
}
In the ajaxpost file the MySql database fetch is executed.

$limitquery = "select id,name,category,price,discount from previousnext limit ".(int)$_REQUEST['current'].",".$limit;
$result = mysql_query($limitquery, $dbHandle);
The data send from sql server is parsed in the php file and send to the client.
while($row = mysql_fetch_array($result)){
echo "
".$row['id']."
".$row['name']."
".$row['category']."
".$row['price']."
".$row['discount']."
";
}
In the next post i have planned to expand this application to have pagination. Donot forget to comment on my ideas.
Read More

Need php script to import csv/excel file into Mysql

View Comments
add to del.icio.us
saved by 0 users





Using a script to parse and import the file


If you need to take a csv or other delimited data file and import it INTO MySQL, here is a php script that can do it for you:








$connection = mysql_connect("localhost", "test", "test") or die ("Unable to connect to server");
 $db = mysql_select_db("test", $connection) or die ("Unable to select database");

 // first get a mysql connection as per the FAQ

 $fcontents = file ('./spreadsheet.xls');
 // expects the csv file to be in the same dir as this script


  for($i=0; $i<sizeof($fcontents); $i++) { 
        $line = trim($fcontents[$i]); 
  $arr = explode("\t", $line);
  //if your data is comma separated  instead of tab separated, 
  // change the '\t' above to ','

  $sql = "insert into TABLENAME values ('".implode("','", $arr) ."')";
  mysql_query($sql);
  echo $sql ."\n";
   if(mysql_error()) {
    echo mysql_error() ."\n";
   }
 }
?>








And you can get the source from here




Get It and enjoy....

Note: 
Upload a spreadsheet file into the same directory as this script. Then you edit this script to put in the correct table name instead of "TABLENAME".





Read More

Play with mysql import, export operations

View Comments
add to del.icio.us
saved by 0 users

We can easily do import/export operations in mysql very easily. Here is the example for both.


In some cases you may have to have backup of your database and you want to use it some other server.


In such cases you just follow the instructions given below.


You can do the mysql import, export operations in two ways.


  1. If you are having phpmyadmin like interface to access your mysql means, it will be very easier.


Just access the phpmyadmin from your browser(domainname/phpmyadmin)







Once you provided authentication its all done.



Choose a database which you want to export






In the top you can find import,export options.






You can export to many number of formats



Once you chosen a format and tables which are to export means it will ask you to save the file. Export is over.


You can import sql files easily from here,



It has a disadvantage too. If your database is too big there will be time out problem.


In such case you can follow the below one or just contact your hosting company to do this.








  1. And also you can do export, import operations from command line very easily without any of the above conditions.


Here you need to ssh access in case if you are accessing the remote server or if it is local you just need terminal.


First export the existing database by using the below command


mysql -u USERNAME -p DATABASENAME > FILE.SQL


Once you have the sql file means yoou can import the sql file by


mysql -u USERNAME -p DATABASENAME < FILE.SQL


Here you need to provide password to access


For example



mysql -u root -p forum < forumdb.sql



It will be stored in your present directory.




mysql -u root -p forum < forumdb.sql
The sql file should be in the present directory.


Leave your comments….
Read More