I'm developing a Joomla 3 component and I'm trying to select data from database using the Joomla framework classes. I'm getting a error Not unique table/alias. What could be the reason ?.
Code Snippet...
$app = JFactory::getApplication();
$job_id = JRequest::getVar('Jobid', null);
try {
$db = JFactory::getDbo();
$query = $db->getQuery(true);//Here Was The Problem
$query->select(array('A.state AS approval_state', 'A.*', 'B.*', 'C.district_name', 'D.educational_qualification', 'E.current_job_status'))
->from($db->quoteName('#__pes_job_provider_request_cv_info') . 'AS A')
->join('LEFT', '#__pes_jobseeker_profile AS B ON B.jobseeker_profile_id = A.jobseeker_profile_id')
->join('LEFT', '#__pes_district AS C ON C.district_id = A.district_id')
->join('LEFT', '#__pes_highest_educational_qualification AS D ON D.highest_educational_qualification_id = B.highest_educational_qualification_id')
->join('LEFT', '#__pes_current_job_status AS E ON E.current_job_status_id = B.current_job_status_id')
->where($db->quoteName('A.job_order_registration_id') . ' = ' . $db->quote($job_id))
->order('B.name_in_full ASC');
$db->setQuery($query);
$results = $db->loadObjectList();
Thank You.
OK ! I figured out the reason.
The reason why this happened is that I haven't instantiated the query object as a new query.
i.e. $query = $db->getQuery();
So, in order to resolve the issue I simply gave the parameter boolean true for the query method
i.e. $query = $db->getQuery(true);
Related
someone can tell me what is wrong in this code? I just want to get the last date in Joomla 2.5. Thanks
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query
->select($db->quoteName('MAX(created)'))
->from($db->quoteName('#__content'))
$db->setQuery($query);
$result = $db->loadResult();
return $result;
don't quote functions:
$query
->select('MAX('.$db->quoteName('created').')')
->from($db->quoteName('#__content'));
A ; is missing at the end of the line ->from($db->quoteName('#__content'))
i keep having this error "mysql_fetch_array() expects parameter 1 to be resource, null given in" when i try to display the returned value of count in sql. heres my code.
$query="SELECT med_rec_ID, COUNT(med_rec_ID)
FROM med_issue
WHERE MONTH(issue_date) = MONTH('2013-02-05')
GROUP BY med_rec_ID";
$result= mysql_query($query);
while($count = mysql_fetch_array($display3)){
echo $count[0];
}
i have tried to run the query in sql alone it displays 2 columns (the med_rec_ID, and the COUNT). guys how do i display the count and fix the error too?
First of all, don't use mysql_* functions since they're deprecated. Use mysqli or PDO.
Secondly, look at what you're passing into the fetch_array function.
You probably want to do something like:
$link = mysqli_connect("localhost", "admin", "pass", "db_name");
$result = mysqli_query($link, $sql);
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$medIds[] = $row['med_rec_ID'];
...
}
Then fix the count by giving it an alias.
Please note that you should actually store how you access the DB in a more secure manner, but I use this only to illustrate the example. Here's a pretty good post: How to create global configuration file?
Is your query even executing? that error will happen if mysql_query doesnt return the resource, in case query fails
$query="SELECT med_rec_ID, COUNT(med_rec_ID) as C FROM med_issue where MONTH(issue_date) = MONTH('2013-02-05') GROUP BY med_rec_ID";
$result= mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
echo $row["C"];
}
Note: Please do not use mysql_* functions anymore
Give it an alias:
SELECT
med_rec_ID,
COUNT(med_rec_ID) TheCount
FROM med_issue
where MONTH(issue_date) = MONTH('2013-02-05') GROUP BY med_rec_ID
then you can select that column TheCount inside the while loop with $row['TheCount'], also use lope through the $result:
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo $row['TheCount'];
}
I use Yii Framework and i need to build difficult query with many conditions.
I'm filling 2 arrays $conditions and $values.
And i have one problem.
Below is example
When i use
$usersId = '1,2';
$conditions[] = 'e.user_id IN(:usersId)';
$values[':usersId'] = $usersId;
I get only value from user_id = 1
When i'm not use option and write manually
$usersId = '1,2';
$conditions[] = 'e.user_id IN(' . $usersId . ')';
no problem.
Of course i can use second construction, but it seems not very good.
You should addInCondition
$criteria->addInCondition('e.user_id',array(1,2));
Yii way would be to use CDbCriteria addInCondition function
$usersId = array(1,2); //must be array
$criteria=new CDbCriteria();
$criteria->addInCondition('user_id',$usersId);
$result = MyModel::model()->findAll($criteria);
$values[':usersId'] = $usersId;
If I understand your wuestion correctly, you can use the BindParam function in yii?
Instead of this - $values[':usersId'] = $usersId;
Write this - $command->BindParam(':usersId', $usersId, PDO::PARAM_STR);
Very simply, you're binding your parameters to your command statement.
Hope it works!
this is my custome query:
$query = $em->createQuery("SELECT max(d.id) FROM MyBundle:DBTableEntity d ");
$max_incoming_id = $query->execute();
I want it to return the Entity Object, just like this one below:
$EntityObj = $resource->getRepository("MyBundle:DBTableEntity")->findAll();
Any idea how to do that?
Something like this should work
$EntityObjects = $resource->getRepository('MyBundle:DBTableEntity')
->createQuery()
->orderBy('id', 'DESC')
->getResult();
$EntityObject = array_pop($EntityObjects);
Try This
$query = sprintf("SELECT s FROM BundleName:EntityClass s where s.field1 = %d and s.field2=%d", $field1, $field2);
$em = $this->getDoctrine()->getEntityManager();
$queryObj = $em->createQuery($query);
$entities = $queryObj->execute();
With excute you receive a array of results (entities).
so you can do $entities = $query.excute();
return $entities[0]; //this is if you have one result;
array_pop($entities) will not work this gives a error in symfony 2.6
I think you will like this style:
$EntityObj = $resource->getRepository("MyBundle:DBTableEntity")
->findOneBy(array(), array('id' => 'DESC'));
:)
I'm having a weird problem.
I have this code
$topic contains "海賊_(ONE_PIECE)" from URI /trends/about/海賊_(ONE_PIECE)
I checked to echo $topic and it prints out 海賊_(ONE_PIECE)
$sql="SELECT wti.redirect_title FROM wikipedia_timelines AS wti WHERE wti.redirect_title = ? LIMIT 1";
$query = $this->db->query($sql,array($topic));
if ($row = $query->result_array())
{
The problem is that this code returns $row to be an empty array
Array
(
[0] => Array
(
[redirect_title] =>
)
)
However, if I use this code (replacing ? with the actual value of $topic, it works perfectly
$sql="SELECT wti.redirect_title FROM wikipedia_timelines AS wti WHERE wti.redirect_title = '海賊_(ONE_PIECE)' LIMIT 1";
$query = $this->db->query($sql,array($topic));
if ($row = $query->result_array())
{
Replacing ? with {$topic} will not make it working too.
This problem only occurs when $topic contains ( ) if it doesn't have () it works fine
I wonder what is the problem.
I suppose there is a problem with URI encoding, but I'm not sure how to fix it.
Please help me.
Thank you
I'm pretty sure your problem is that query binding escapes your input values (im guessing it doesn't like brackets).
try building your sql string with concatenation, like:
$sql = "SELECT wti.redirect_title FROM wikipedia_timelines AS wti WHERE wti.redirect_title = '".$topic."' LIMIT 1";
$query = $this->db->query($sql);
and see if that helps. Or try the active record class, I use it for japanese characters all the time and it seems to work ok.
// EDIT:
To allow brackets in the URI, try and change application/config.config.php and set
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-()';
also try urldecode($topic); to get the kanji out.