Run UPDATE query on SELECT query result - mysql

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.

Related

Selecting records on condition - Joomla

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.

how to delete 2 rows with one query with mysql

Hi guys how i can delete 2 rows in one query from one table. my code is:
DELETE FROM panel_friends
WHERE friends_friend_id = ' . $k . ' AND friends_member_id = ' . $key2
AND
WHERE friends_friend_id = ' . $k2 . ' AND friends_member_id = ' . $key
DELETE
FROM panel_friends
WHERE (friends_friend_id = :k1 AND friends_member_id = :k2)
OR
(friends_friend_id = :k2 AND friends_member_id = :k1)

Joomla 3 - gettin 1054 - Unknown column error

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

MySQL Update Not Updating Certain Rows

Here is my double-minded query:
$Quest = "SELECT * FROM TOAWorkorders";
$FindTechResult = mysql_query($Quest, $cxn)
or die ('The easter bunny is watching you' . mysql_error());
while ($row = mysql_fetch_array($FindTechResult))
{
if (strpos($BBT, 0, 3) != 'Sys')
{
$IdNum = $row['IdNum'];
$BBT = $row['BBT'];
$BBTArray = explode("-", $BBT);
$TechNum = $BBTArray["0"];
$Title = $BBTArray["2"];
$Name = explode(" ", $BBTArray['1']);
$FirstName = $Name["0"];
$LastName = $Name["1"];
}
echo $TechNum . ' !! ' . $FirstName . ' !! ' . $LastName . ' !! ' . $Title . '<br>';
$Quest = "UPDATE TOAWorkorders SET TechNum = '$TechNum', FirstName = '$FirstName', LastName = '$LastName', Title = '$Title' WHERE IdNum = '$IdNum'";
$result = mysql_query($Quest, $cxn) or die(mysql_error());
}
Everything works for about 2/3s of the database. That leaves 33,000 rows that are not updated. I cannot find any difference between the data that works and the data that doesn't.
Since you're doing an UPDATE, and you say the rest of the code works (meaning, I hope, that you get 109,112 echo'ed results), it must be that the ID isn't being found (WHERE IdNum = '$IdNum').
Try preceding that command with "SELECT COUNT(*) from TOAWorkorders WHERE IdNum = '$IdNum'" and see if you get 33,000 zeros when the program runs. If you do, then you have missing IdNum values in your table.
If you don't, please provide details and I'll let you know.

MySQL MAX_JOIN_SIZE errors

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