I have this query:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName( array('#__content.id', '#__content.title', '#__content.introtext', '#__content.images', '#__content.catid', 'cat.alias') ))
->from($db->quoteName('#__content', '#__categories'))
->join('INNER', $db->quoteName('#__categories', 'cat') . ' ON (' . $db->quoteName('#__content.catid') . ' = ' . $db->quoteName('cat.id') . ' )')
->where($db->quoteName('cat.alias') . ' = ' . $db->quote($alias) )
->order($db->quoteName('#__content.created'), ' DESC');
$db->setQuery($query, 0, 4);
$results = $db->loadObjectList();
When I try to run this query I get following error:
1054 - Unknown column 'tqei2_content.id' in 'field list'
What is going on here?
You are using the database table names in the select and order parts of your query.
#__content and #__categories (#__ being the table prefix) is a table name and should only be used in the ->from() and ->join() sections.
Have a look at the Joomla documentation to get a good example of how it should look:
http://docs.joomla.org/Selecting_data_using_JDatabase
Related
I'm improving my search bars in a project's back-office.
I want to make a research in the protfolio table, joining the categorytable to include the category's names in the available keywords. Seems simple, but the only message I'll get is :
Warning: PDOStatement::execute(): SQLSTATE[42S22]: Column not found:
1054 Unknown column 'search_0' in 'where clause' in [...]
I am creating the SQL query dynamically before executing it, like this :
$keywords = explode(" ", $_POST['search']);
$query = "SELECT * FROM portfolio LEFT JOIN category
ON portfolio.portfolio_category_id = category.category_id
WHERE ";
$query_array = array();
for ($i = 0; $i < count($keywords); $i += 1) {
$query .= "portfolio.portfolio_title LIKE :search_" . $i;
$query .= " OR category.category_name LIKE search_" . $i;
if ($i != (count($keywords) - 1)) {
$query .= " OR ";
}
$query_array['search_' . $i] = "%" . $keywords[$i] . "%";
}
$list_portf = $bdd->prepare($query);
$list_portf->execute($query_array);
I'm not a crack in SQL I don't really understand where is the error (I didn't find an answer in the question I found). Thanks.
seems you missed colon : before your second ref to search_
" OR category.category_name LIKE :search_" . $i;
I followed Joomla doc about how to select data from a single table. I need 3 OR conditions.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('column1', 'column2', 'column3' )));
$query->from($db->quoteName('#__table_example'));
$query->where($db->quoteName('column1') . ' LIKE '. $db->quote('condition1'));
OR where column2 == condition2
...
How to make where statement with many ORs in my example? I didn't find any topics on syntax correctness.
You just have to add or conditions in the same where statement :
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('column1', 'column2', 'column3' )));
$query->from($db->quoteName('#__table_example'));
$query->where($db->quoteName('column1') . ' LIKE '. $db->quote('condition1') . ' OR ' . $db->quoteName('column2') . ' LIKE '. $db->quote('condition2') . ' OR ' . $db->quoteName('column3') . ' LIKE '. $db->quote('condition3') );
I give a general answer which will cover not only OR but also several AND problems. Use WHERE query like this:
ex: query should run if one of (cond1 or cond2 or cond3) is true AND one of (cond4 or cond5 is true):
$query->where( ( (cond1)OR(cond2)OR(cond3) ) AND ( (cond4)OR(cond5) ) );
So you can use OR and AND any how you like, just be sure that you use () properly to define your condition.
In your particular case, you will use format like this:
$query->where( (var LIKE cond1) OR (cond2) );
obviously your cond2 is: column2 == condition2
I hope it resolves confusion.
I want to run an UPDATE query on a record after selecting it.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query
->select(array('p.*', 'u.id', 'u.name' ,'pr.*'))
->from($db->quoteName('#__chichi_photos', 'p'))
->join('LEFT', $db->quoteName('#__users', 'u') . ' ON (' . $db->quoteName('p.userID') . ' = ' . $db->quoteName('u.id') . ')')
->join('LEFT', $db->quoteName('#__chichi_profile', 'pr') . ' ON (' . $db->quoteName('pr.userID') . ' = ' . $db->quoteName('u.id') . ')')
->where($db->quoteName('p.id') .'=' . $photoID .'');
$db->setQuery($query);
$results = $db->loadRow();
I want to run an UPDATE query on a field in #__chichi_photos table that has an id in the result and if possible do it with a single query statement.
Basically what i want to do is update "hits" column on the field after it is selected. Thank You.
Im trying to rewrite mysql_ into mysqli_, but got 2 errors
mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
and
mysqli_num_rows() expects parameter 1 to be mysqli_result,
why? Ive fixed mysql_query("SELECT...) into mysqli_query($db, "SELECT..) and all others
<?php
ob_start();
session_start();
include('config/configuration.php');
if($_POST['Login'])
{
$UserName=$_POST['username'];
$Password=md5($_POST['password']);
$UserQuery=mysqli_query($db, "SELECT Id, UserName, FirstName, LastName, Level FROM users WHERE UserName='$UserName' AND Password='$Password' AND IsActive=1 and level >= 3");
$UserDetails=mysqli_fetch_array($UserQuery);
if(mysqli_num_rows($UserQuery))
{
$_SESSION['UserName'] = $UserDetails['UserName'] . ' (' . $UserDetails['FirstName'] . ' ' . $UserDetails['LastName'] . ')';
$_SESSION['UserId'] = $UserDetails['Id'];
$_SESSION['Level'] = $UserDetails['Level'];
mysqli_query("UPDATE users SET NumberOfLogin = NumberOfLogin + 1, LastLoginDate = NOW() WHERE Id = " . $_SESSION['UserId'] . " ");
Your query is failing.
Try this to see the issue:
if (!$UserQuery) {
echo "MySQLi Error: " . mysqli_error($con);
die();
}
I am asking this question on behalf of a small group of my users that have this problem.
Once the script they are using gets to the 21st ID, it generates the following error:
The SELECT would examine more than
MAX_JOIN_SIZE rows; check your WHERE
and use SET SQL_BIG_SELECTS=1 or SET
SQL_MAX_JOIN_SIZE=# if the SELECT is
okay
I have researched this as much as possible and found something of an answer : http://dev.mysql.com/doc/refman/5.0/en/set-option.html
The problem is that they are on shared hosting so they cannot change their MySQL settings to fix the errors.
Is there anything I can write into my script so that they do not have this problem?
This is the function that generates the database query based on which modules are loaded:
$sql = 'SELECT a.id as id , a.address as address';
$query = 'SELECT'
. ' name AS module_name'
. ', databasename AS module_database'
. ', pregmatch AS module_pregmatch'
. ', pregmatch2 AS module_pregmatch2'
. ', html AS module_html'
. ', sqlselect AS database_sqlselect'
. ', sqljoin AS database_sqljoin'
. ', sqlupdatewithvalue AS database_sqlupdatewithvalue'
. ', sqlupdatenovalue AS database_sqlupdatenovalue'
. ' FROM #__aqsgmeta_modules'
. ' WHERE enabled = 1'
. ' ORDER BY id';
$db->setQuery($query);
$results = $db->loadObjectList();
if (count($results) != 0) {
foreach ($results as $result) {
$sqlselect .= ', ';
$sqlselect .= $result->database_sqlselect;
$sqljoin .= ' ';
$result->database_sqljoin = preg_replace('/\{DATABASENAME\}/Ui', $result->module_database, $result->database_sqljoin);
if (!(preg_match("/" . $result->database_sqljoin . "/Ui", $sqljoin)))
$sqljoin .= $result->database_sqljoin;
}
}
if ($use_sh404sef)
$sqlselect .= ', g.oldurl AS sefurl';
$sql .= $sqlselect;
$sql .= ' FROM #__aqsgmeta_address AS a';
$sql .= $sqljoin;
if ($use_sh404sef)
$sql .= ' LEFT JOIN #__redirection AS g ON g.newurl = a.address';
$sql .=
//. ' WHERE a.id IN (' . $cids . ')'
' WHERE a.id = ' . $id
. ' ORDER BY a.address asc,a.id '
;
$db->setQuery($sql);
$rows = $db->loadObjectList();
MAX_JOIN_SIZE is a safety catch commonly used on the shared hostings.
It won't let you accidentally run long queries which would hang the server.
Issue this command:
SET SQL_BIG_SELECTS = 1
before running the query you know to return lots of values.
The MAX_JOIN_SIZE gets hit when MySQL calculates the Cartesian product of a join, not the actual expected records back. Therefore, if you're joining a massive table to another massive table, this will creep up. Use indexes and views to pare down the possible table hits if it's really that large.
See more here: MySQL - SQL_BIG_SELECTS