My code is working fine in windows but in Linux when I export it exports all data into a single column instead of exporting into multiple columns.
Here's my code:
$this->load->dbutil();
$this->load->helper('download');
$query = $this->db->query("SELECT * FROM products");
$csv = $this->dbutil->csv_from_result($query);
force_download('result.csv', $csv);
Why not use the delimiter and newline options
$this->load->dbutil();
$this->load->helper('download');
$query = $this->db->query("SELECT * FROM products");
$delimiter = ";";
$newline = "\r\n";
$csv = $this->dbutil->csv_from_result($query,$delimiter,$newline);
force_download('result.csv', $csv);
Related
hello i want to update the row by clicking on edit button, but it giving me this error, kindly help
function updateUser()
{
$data = stripslashes(file_get_contents("php://input"));
$mydata = json_decode($data, true);
$id = $mydata['sid'];
//retrieve specific info
$sql = "SELECT * FROM admin WHERE id = {$id}";
$result = $this->db->query($sql);
$row = $result->fetch_assoc();
echo json_encode($row); //returning json formate
}
Use $result->row_array(); instead of $result->fetch_assoc();
To prevent sql injection, you may also want to bind the $id separately instead of concatenating it into the query, like this:
$sql = "SELECT * FROM admin WHERE id = ?";
$result = $this->db->query($sql, array($id));
$row = $result->row_array();
See also: https://codeigniter.com/userguide3/database/queries.html#query-bindings
$name = $_POST['name'];
$statement = $db->prepare("SELECT * FROM authors WHERE name = ?");
$statement->execute(array($name));
$author_id = $statement->fetch()["id"];
$statement = $db->prepare("SELECT * FROM quotes WHERE author_id = ?");
$statement->execute(array($author_id));
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
$quote = $row['quote'];
}
How can i do same operation in laravel query builder ?
You can try like this:
$name = $_POST['name'];
$where[] = ['authors.name ', $name];
$data = \DB::table('authors')
->join('quotes', 'quotes.author_id', '=', 'authors.id')
->where($where)
->get();
i want to export a table to a csv file. that file should save to a folder in assets. here i am using force download.How can set path to the downloading csv file? And is there any other method to save the table as csv file to a specified folder.? pls help me ..
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$filename = "filename.csv";
$query = "SELECT * FROM cyber_details";
$result = $this->db->query($query);
$data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
force_download($filename, $data);
I assume that you want to save CSV file on server end.
you can do as following
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$filename = "filename.csv";
$query = "SELECT * FROM cyber_details";
$result = $this->db->query($query);
$data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
if ( ! write_file('./path/to/file/file_name.csv', $data))
{
echo 'Unable to write the file';
}
else
{
echo 'File written!';
}
make sure the path you choose has enough permission to perform write operation.
try this, worked for me:
function createcsv(){
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$filename = "filename.csv";
$query = "SELECT * FROM YourTable";
$result = $this->db->query($query);
$data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
force_download($filename, $data);
}
I also had come through the same issue.
Below code worked good enough for me.
$arry_to_csv[] = $data;
$output_file_name = 'path/to/Rejected.csv';
$temp_memory = fopen($output_file_name, 'w');
$csv_headers = array_keys($arry_to_csv[0]);
$delimiter = ",";
fputcsv($temp_memory, $csv_headers, $delimiter);
$csv = '';
foreach ($arry_to_csv as $line) {
$csv.= $line['first_name'] . ',' . $line['last_name'] . "\n";
}
fwrite($temp_memory, $csv);
fclose($temp_memory);
I'm trying to output a query result as a csv file, using csv_from_result, but for some reason, it only echoes the field names, and not the full results, like so:
"code","heb_name","eng_name","intro","heb_code","rank"
This is the code I'm using, based on CI documentation here:
$query = $this->db->query($sql);
if($query->result()) {
$this->load->dbutil();
$delimiter = ",";
$newline = "\r\n";
echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
}
$sql holds the query syntax, but I dont give it here since I get the same results even with the simplest "SELECT * FROM table" query.
The query itself does return full results. When I'm dumping $query->result_array() I get the expected result array.
What am I missing here?
Personally i prefer PHPExcel. make sure you setup proper headers and encoding. try this:
$this->load->dbutil();
$query = $this->db->query($sql);
if($query->result()) {
$delimiter = ",";
$newline = "\r\n";
header ("Content-disposition: attachment; filename=csv_file" . time() . ".csv") ;
echo mb_convert_encoding($this->dbutil->csv_from_result($query, $delimiter, $newline), "ISO-8859-1", "UTF-8");
}
How to use:
JFactory::getDbo()->insertObject('#__card_bonus', $object);
with on duplicate key update ?
You have a few options:
1) Check for an entity id. This is my preferred option, because it only uses a single query, is reusable for any object, and is database agnostic - meaning it will work on whichever DBMS you choose, whereas the other two options are exclusive to MySQL.
if (isset($object->id)) {
$db->updateObject('#__card_bonus', $object);
}
else {
$db->insertObject('#__card_bonus', $object, 'id');
}
I often create an abstract model with a save(stdClass $object) method that does this check so I don't have to duplicate it.
2) Write your own query using the MySQL ON DUPLICATE KEY UPDATE syntax, which is a proprietary extension to the SQL standard, that you have demonstrated understanding of.
3) Write your own query using MySQL's proprietary REPLACE INTO extension.
<?php
$jarticle = new stdClass();
$jarticle->id = 1544;
$jarticle->title = 'New article';
$jarticle->alias = JFilterOutput::stringURLSafe($jarticle->title);
$jarticle->introtext = '<p>re</p>';
$jarticle->state = 1;
$jarticle->catid = 13;
$jarticle->created_by = 111;
$jarticle->access = 1;
$jarticle->language = '*';
$db = JFactory::getDbo();
try {
$query = $db->getQuery(true);
$result = JFactory::getDbo()->insertObject('#__content', $jarticle);
}
catch (Exception $e){
$result = JFactory::getDbo()->updateObject('#__content', $jarticle, 'id');
}
I use this method - are not fully satisfied, but ...
or for not object method:
$query = $db->getQuery(true);
$columns = array('username', 'password');
$values = array($db->quote($username), $db->quote($password));
$query
->insert($db->quoteName('#__db_name'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$query .= ' ON DUPLICATE KEY UPDATE ' . $db->quoteName('password') . ' = ' . $db->quote($password);
$db->setQuery($query);
JFactory::getDbo()->insertObject('#__card_bonus', $object, $keyName);
The name of the primary key. If provided the object property is updated.
Joomla doc ...