How do I insert into database?
function create(){
$db=JFactory::getDbo();
$query=$db->getQuery(true);
$query="INSERT INTO '#__mycomp_posts' (`title`,`body`) VALUES ('First Post','This is enter code herepost body')";
$db->setQuery($query);
$db->$query();
}
You should really have done a little research before asking your question. I've taken your code and converted it to up to date Joomla coding standards:
function create(){
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$columns = array('title', 'body');
$values = array($db->quote('First Post'), $db->quote('This is enter code herepost body'));
$query->insert($db->quoteName('#__mycomp_posts'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$db->setQuery($query);
$db->query();
}
You then need to call this function to execute the code
Related
I have this code on my component.
$results = \Db::select('select * from engegraph_forms_membros where id = ?', [1]);
echo $results;
but i have this error
"Array to string conversion"
Someone can help a newbie ??
The problem is you are printing array as as string.
returning result of \Db::select is an array so to print array you can use print_r()
$results = \Db::select('select * from engegraph_forms_membros where id = ?', [1]);
echo '<pre/>'; print_r($result); exit();
you can print result like this or you can use build in debugger function.
$results = \Db::select('select * from engegraph_forms_membros where id = ?', [1]);
dd($result);
// or dump($result);
if you use dump/dd function you dont need to worry it can print anything. dd [die and dump] stops php flow to next statements. dump will continues flow so you can print another values/or/execute next statements if you need.
if any doubts please add comments.
CI_Controller ..
public function index(){
$this->load->database();
$this->load->model('model_contoh');
$data = $this->model_contoh->GetDataByProc();
echo '<pre>' ;
print_r($data->result_array());
}
}
CI_Model ..
function GetDataByProc(){
$data = $this->db->query("spCekSelisihJurnal '1/1/2018 00:00:00','12/31/2018 23:00:00'");
return $data ; }}
if this query execute in sql tool success to show the data
but not run in codeigniter ?
enter image description here
Do you not call like this. P1 and P2 would need to need procedure parameter names
$p_1 = '1/1/2018 00:00:00'
$p_2 = '12/31/2018 23:00:00'
$stored_procedure = "CALL spCekSelisihJurnal(?,?) ";
$result = $this->db->query($stored_pocedure,array('P1'=>$p_1,'P2'=>$p_2));
I have a situation where I need to fetch details of an employee from the database using his ID and display them in the browser.
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$query = $this->db->query($sql);
These are the statements that I have written to get the result array. My problem is how do I take a single field/column from this array? Also have I done it correctly?
Thanks in advance.
If your query return single data from database then you need to use
$row = $query->row_array()
Try this
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$result = $this->db->query($sql)->result_array();
echo $result[0]['field_name'];
can someone help me with this? I want to query some data for my joomla 2.5
template from the db.
The known mysql syntax
$result = mysql_query("SELECT * FROM `jos_fieldsattach_values` WHERE value > NOW() and value < NOW() + INTERVAL 5 DAY");
works as expexted but if I try to write this in the joomla db syntax the query fails
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$db->setQuery($query);
$query
->select(array('*'))
->from('#__fieldsattach_values');
->where('WHERE value > NOW() and value < NOW() + INTERVAL 5 DAY');
$result = $db->loadObjectList();
print_r($result);
It seems somethings wrong with the where clause ?
Thankful for you answers,
tony
First of all. When you develop anything with Joomla you should turn on FULL Error reporting (Configuration/Server/Error Reporting/Development) then set system debugging (Configuration/System/Debug System/Yes). It will show all errors, those from system and those from php/mysql. But remember make sure website is not visible for other users to. Now, your code should look more like this:
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select("*")->from('#__fieldsattach_values')->where('value > NOW() AND value < DATE_ADD(NOW(),INTERVAL 5 DAY)');
$db->setQuery($query);
$result = $db->loadObjectList();
print_r($result);
The following code seems to be just too much, for getting a single count value.
Is there a better, recommended way to fetch a single COUNT value using plain DBI?
sub get_count {
my $sth = $dbh->prepare("SELECT COUNT(*) FROM table WHERE...");
$sth->execute( #params );
my $($count) = $sth->fetchrow_array;
$sth->finish;
return $count;
}
This is shorter, but I still have two statements.
sub get_count_2 {
my $ar = $dbh->selectall_arrayref("SELECT ...", undef, #params)
return $ar->[0][0];
}
Easy enough to do in one line with no extra variables:
$count = $dbh->selectrow_array('SELECT count(*) FROM table WHERE...', undef, #params);
I don't know Perl, but if it's syntax is logical I would think this would work based on your 2nd example:
sub get_count {
return $dbh->selectall_arrayref("SELECT ...", undef, #params)->[0][0];
}
I probably wouldn't do this myself, but you could always make it a new top-level function of the DBH object you're using:
WARNING: untested code follows!
sub DBD::SQLite::db::count
{
my($dbh, $table, $where) = #_;
my($stmt) = "SELECT COUNT(*) FROM $table";
$stmt .= " WHERE $where" if $where;
my($count) = $dbh->selectrow_array($stmt);
return $count;
}
and then call it like this:
my($cnt) = $dbh->count('Employee', 'year_hired < 2000');
Besides polluting a namespace that's not yours, you'd also have to write this for every DB driver you use, though I'm sure your could work something up that allows you to construct and eval some code to auto-configure this for a given DBH object.