Joomla DB Object with where and Now() - database

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);

Related

Getting value from database custom table not working in wordpress

I have created a wordpress table with wp_compare_post with following column id, user_id, post_id, post_type. I have attached the image of the database.
Below is the coading for viewing data from database
function compare_counting(){
global $wpdb;
//get user id
$user_ID = get_current_user_id();
$sql = "SELECT * FROM $wpdb->compare_post WHERE user_id = '$user_ID '";
$results = $wpdb->get_results($sql) or die(mysql_error());
foreach( $results as $result ) {
echo $result->post_id.',';
}
}
But getting following error.
Notice: Undefined property: wpdb::$compare_post in
I think it could be because you need to use $wpdb->prefix
Try this:
$sql = "SELECT * FROM ".$wpdb->prefix."compare_post WHERE user_id = '$user_ID'";

How to bind pdo key and value from Question mark?

I want to make a query select data :
$pdo = new PDO($dsn, $user, $pass, $options);
$query = "SELECT ? FROM ? WHERE ? = ?";
$pdo->prepare($query);
How can I do it?
PDOpreapare statement is not support this query right row

Query execution in codeigniter

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'];

How can i insert into database in joomla?

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

drupal 7 , multiple FROM tables, need a very sample example

Someone can give me an example where there is a multiple 'FROM' table ?
I don't understand corectly the documentation (thanks my bad english lvl -_-).
I would like use a query like :
SELECT a.one a.two b.one FROM {table1} a, {table2} b WHERE a.one = b.one
thx =)
You can use join:
http://drupal.org/node/1848348
<?php
$query = db_select('table1', 'a');
$query->join('table2', 'b', 'a.one = b.one');
$query->fields('a', array('one', 'two'));
$query->fields('b', array('one'));
$result = $query->execute();
?>
Here is asked and answered:
https://drupal.stackexchange.com/questions/27723/drupal-7-join-two-tables
More about dynamic queries
http://drupal.org/node/310075

Resources