Inserting Hebrew text into mysql using php - blank - php

I have a problem when I upload csv file to mysql.
I used this code:
if(isset($_POST["submit"])) {
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
$row=1;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false) {
if ($row ==1) {
$row++;
continue;
}
$name = $filesop[0];
$phone = $filesop[1];
$sql = runsql("INSERT INTO messages_recipient (user_id,full_name, phone,date_added) VALUES ($SVARS[user_id],'$name','$phone',NOW())");
}
if($sql) {
echo "You database has imported successfully";
} else {
echo "Sorry! There is some problem.";
}
}
it's worked fine except when i upload csv file with hebrew values i get blank values in mysql.
i tried to change the type to text/long text without success.
the collation is: utf8_general_ci.
thanks for the help

You may be looking for:
mysqli_query("SET NAMES 'utf8'");
and
mysqli_query("SET CHARACTER SET utf8 ");
Use it before the query.
mysqli.set-charset:
http://php.net/manual/en/mysqli.set-charset.php

Related

Importing data from Excel to MySQL showing text in different language

I am trying to import data from Excel to MySQL using PHP. The code I am using imports the data but the language is strange.
Below is the PHP code:
<?php
include_once("conn.php");
$filename= "Financial Sample.xlsx";
$file = fopen($filename, "r");
$count = 0; // add this line
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//print_r($emapData[0]);
//exit();
$count++; // add this line
if($count>1){ // add this line
//echo html_entity_decode("žūų");
//echo json_encode($emapData[0]);
$insert_q = "INSERT into questions(q_describe) values ('$emapData[0]')";
if($query_q=$mysqli->query($insert_q))
{
$final=array();
$final['status']="success";
$final['message']="Inserted Successfully";
}
else
{
$er = $mysqli->error;
$final['status']=$er;
$final['message']="Error";
}
echo json_encode($final);
} // add this line
}
fclose($file);
?>
Once imported, data is seen like this in phpMyAdmin:
Actual Excel is shown below:
Where am I going wrong?
Edit
Showing the structure of my table:
If you look at the structure of your table, you're looking for two fields: encoding and Collation.
Make sure they're set to cp1252 West European and latin1_swedish_ci respectively.

importing excel sheet into mysql table with php

I want to import the excel sheet data into mysql table with php but i am getting these errors someone please get me out from this.. please look on php code only ignore html stuff.
<?php
include ("connection.php");
if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$name = $filesop[1];
$email = $filesop[2];
$sql = mysql_query("INSERT INTO co (name, email) VALUES ('$name','$email')");
$c = $c + 1;
}
fcose($file);
if($sql){
echo "You database has imported successfully. You have inserted ". $c ." recoreds";
}else{
echo "Sorry! There is some problem.";
}
}
?>
this is excel sheet
in database it is showing different format
There are libraries excellibrary/php-excel-reader/excel_reader2.php and excellibrary/SpreadsheetReader.php
you can use these libraries to read your content and convert it into array.
Once you convert your spreadsheet data into an array, you can easily insert into database using iterations.

importing csv files into database

here is my code to import csv data to my database
if (isset($_POST["submit"])) {
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
**while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$name = $filesop[0];
$email = $filesop[1];
$sql = mysql_query("INSERT INTO selleruser (emaili) VALUES ('$name')");
$c = $c + 1;
}**
if ($sql) {
echo "You database has imported successfully. You have inserted ". $c ." recoreds";
} else {
echo "Sorry! There is some problem.";
}
}?>
</div>
I have a csv file where there is a column which contains emails
the import is done suvcessfull but the issue is that it just imports the value in another format like in other language or encripted or something un readable
Try this query to import csv from phpmyadmin or any other mysql client
Before firing query, please make sure that table matching the fields is already created into db.
LOAD DATA LOCAL INFILE 'local machine path to csv file' INTO TABLE `selleruser`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(emaili);

uploading csv file into mysql through php

i know how to upload a csv file into mysql database but only through cmd. i want to know how to upload csv file into mysql database using php form and will disregard some information on the excel and will only start importing starting from a certain line. ? kindly help me.
(PHP 4, PHP 5)
fgetcsv
See php manual http://php.net/manual/en/function.fgetcsv.php
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
Try this it's working well, you can add as many values as possible depending on the number of columns you have in the CSV file. Then in the HTML code put the uploading syntax in the tag.
**
$fname = $_FILES['csv_file']['name'];
$chk_ext = explode(".",$fname);
$filename = $_FILES['csv_file']['tmp_ name'];
$handle = fopen($filename, "r");
if(!$handle){
die ('Cannot open file for reading');
}
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE)
{
$query = "INSERT INTO tablename (col1_csv, col2_csv)
values ('$data[0]', '$data[1]');
mysql_query($query) or die(mysql_error ());
}
fclose($handle);
?>
**
You can use the MySQL LOAD DATA INFILE statement to bulk-insert thousands of records at once. PHP can handle the file upload. The PHP code would be something similar to:
$query = sprintf("
LOAD DATA LOCAL INFILE '%s'
INTO TABLE `table1`
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\\r\\n'
IGNORE 1 LINES
",
mysql_real_escape_string($FILES["file1"]["tmp_name"])
);
The LOCAL keyword should allow you to workaround some security restrictions. Change the FIELDS TERMINATED BY and LINES TERMINATED BY parameter to match the separators used by excel while exporting. IGNORE 1 LINES tells MySQL to skip the header row(s).
Note: Excel does not seem to use an escape character; but it will (i) enclose the fields that contain , and " with " (ii) use "" to escape a single " inside data. I believe MySQL will understand this encoding and import the data correctly.
You could use the "LOAD DATA INFILE " statement with the " IGNORE ... LINES " option which you can use from the command line as well as from PHP.
try this:
$filename=$_FILES["upload_file"]["name"];
$extension = end(explode(".",$filename));
if ($extension=='csv') {
$tmp_file=$_FILES["upload_file"]["tmp_name"];
$handle = #fopen($tmp_file, "r");
//specify your own database connection parameter
$db = new PDO('mysql:host=localhost;dbname=demo','user','password');
$stmt = $db->prepare("INSERT INTO writers (writer_name, writer_email) VALUES (?, ?)");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$array=explode(",",$buffer);
$count=1;
foreach ($array as $value) {
$stmt->bindParam($count, $value);
$count++;
}
$stmt->execute();
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
$db = null;
echo "<p>Success</p>";
}
else {
$error="<p style='color:red;'>Invalid file type</p>";
}
Refer to http://pradipchitrakar.com.np/programming/upload-csv-mysql-php/

Need help with updating a mysql table by a csv file upload

Following is my script to upload the table employee with a csv file. The file is uploading and upldating the employee table perfectly. BUT the problem is i specified a row with headings in the csv file. That heading row is also getting updated in the table. I want only those datas to get uploaded except the heading row in the csv file, any help or ideas?.
<?php
require_once '../config.php';
if(isset($_POST['upload']))
{
$fname = $_FILES['sel_file']['name'];
$chk_file = explode(".",$fname);
if(strtolower($chk_file[1]) == 'csv')
{
$filename = $_FILES['sel_file']['tmp_name'];
$handle = fopen($filename,"r");
while(($data = fgetcsv($handle,1000,",")) != false)
{
$sql = "INSERT into employee(employee_code,employee_name,employee_address,emp_dateofjoin,emp_designation,emp_hq,pf_num,esic_num,emp_state,month,tot_work_days,lop_days,arrear_amt,leave_encash) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]')";
/$upd = "UPDATE student SET month='',tot_work_days='',lop_days='',arrear_amt='',leave_encash='' where employee_code=''";
mysql_query($sql) or die(mysql_error());
}
fclose($handle);
echo "Successfully Imported";
}
else
{
echo "Invalid File";
}
}
?>
Skip the first line by calling fgetcsv once before your loop:
fgetcsv($handle,1000,",");
while(($data = fgetcsv($handle,1000,",")) != false)
You also could use LOAD DATA INFILE MySQL statement with 'IGNORE 1 LINES' clause.

Categories