I'm trying to construct a wp query that creates a string for each record:
$ttr_a = $wpdb->get_results("
SELECT CONCAT(
'"TR4":"',
t2.TaxAccount,
'", "',
CASE WHEN t1.TypeID = '1' THEN '"TR6":"Text A", '
WHEN t1.TypeID = '2' THEN '"TR6":"Text B", '
ELSE
CONCAT('"TR6":"', t1.NewID, '", ')
END
)
AS TID
FROM " . $wpdb->prefix . "table1 t1
LEFT JOIN " . $wpdb->prefix . "table2 t2
ON t1.NewID = t2.TaxCode
WHERE ID = " . $ID);
This query works fine in phpMyAdmin but the double quotes break the wpdb query. Is there some way to escape these? I've tried replacing with apostrophe and escaping the double qoutes with a double quote but nothing seems to work. If there are syntax errors here, please ignore them as I've just extracted what is needed, my sql query does work in mysql.
You should use a prepared statement here if at all possible. If that be not possible, then to escape literal double quotes inside a string defined with double quotes, try escaping the literal ones with backslashes:
$ttr_a = $wpdb->get_results("
SELECT CONCAT(
'\"TR4\":\"',
t2.TaxAccount,
'\", \"',
CASE WHEN t1.TypeID = '1' THEN '\"TR6\":\"Text A\", '
WHEN t1.TypeID = '2' THEN '\"TR6\":\"Text B\", '
ELSE
CONCAT('\"TR6\":\"', t1.NewID, '\", ')
END
)
AS TID
FROM " . $wpdb->prefix . "table1 t1
LEFT JOIN " . $wpdb->prefix . "table2 t2
ON t1.NewID = t2.TaxCode
WHERE ID = " . $ID);
Related
i need to use dynamic table names on model select query like this:
$this->db->select("$this->table_car.id as carId, $this->table_car.price as carPrice, $this->table_car.name as carName, $this->table_car.used as carUsed, $this->table_car.visible as carVisible, $this->table_cat.name as catName, $this->table_brand.name as carBrand, $this->table_model.name as carModel");
But variable is not working. This is what I get:
SELECT .`id` AS `carId`, .`price` AS `carPrice`, .`name` AS `carName`, .`used` AS `carUsed`, .`visible` AS `carVisible`, .`name` AS `catName`, .`name` AS `carBrand`, .`name` AS `carModel`
LEFT JOIN ON .`car_id` = .`id`
LEFT JOIN ON .`id` = .`cat_id`
LEFT JOIN ON .`id` = .`brand_model_id`
LEFT JOIN ON .`id` = .`model_id`
LEFT JOIN ON .`id` = .`brand_id`
WHERE .`used`= 0 LIMIT 3
Is there a workaround for this?
Thanks in advance
Whilst double quotes do allow the use of variables inside them without the need to close and concatenate, it doesn't always work for the likes or object and arrays, like you are showing. A better solution would be:
$this->db->select($this->table_car . ".id as carId, "
. $this->table_car . ".price as carPrice, "
. $this->table_car . ".name as carName, "
. $this->table_car . ".used as carUsed, "
. $this->table_car . ".visible as carVisible, "
. $this->table_cat . ".name as catName, "
. $this->table_brand . ".name as carBrand, "
. $this->table_model . ".name as carModel");
Another option would be to put your object variables in braces {} such as :
$this->db->select("{$this->table_car}.id as carId,
{$this->table_car}.price as carPrice,
{$this->table_car}.name as carName,
{$this->table_car}.used as carUsed,
{$this->table_car}.visible as carVisible,
{$this->table_cat}.name as catName,
{$this->table_brand}.name as carBrand,
{$this->table_model}.name as carModel");
I need to write an db query based on the Joomla jdatabase format, but I get stuck with writing the 2 'AND' parts for the native sql query below (I could not figure it out based on http://docs.joomla.org/Selecting_data_using_JDatabase).
How to include the 2 AND parts? Appreciate your help
//get userid of collective based on entered collective name, and get productid based on entered productname
//NATIVE MYSQL:
SELECT #__users.id, #__products.productid
FROM #__users INNER JOIN #__products
ON #__users.id = #__products.owner
AND #__users.name = $form_username
AND #__products.productname = $form_productname;
What I have sofar:
//Joomla Jdatabase query sofar:
$query
->select($db->quoteName(array('a.id', 'b.productid')))
->from($db->quoteName('#__users', 'a'))
->join('INNER', $db->quoteName('#__products', 'b') . ' ON (' . $db->quoteName('a.id') . ' = ' . $db->quoteName('b.owner') . ') ') //HERE I think some AND parts need to be included in some way?
->order($db->quoteName('a.created') . ' DESC');
I haven't tested this so please let me know if it works and if not, I will come back with an edit
->join('INNER', $db->quoteName('#__products', 'b') . ' ON (' . $db->quoteName('a.id') . ' = ' . $db->quoteName('b.owner') . ')
AND ' . $db->quoteName('#__users.name') . ' = ' . $form_username . '
AND ' . $db->quoteName('#__products.productname') . ' = ' . $form_productname)
I hope that someone can tell me what I am doing wrong.
I would like to query my SQL table and receive back a ClassID and a ClassName.
The ClassID is a char(8) and holds values such as 123.45L1 or 350.12.
I am attempting to shorten that value so that I receive back 123 or 350 only.
Here is my code:
$classSelect = "SELECT LEFT(ClassID , 3), ClassName FROM Class GROUP BY ClassID";
$result = mysql_query($classSelect);
echo "<td><select multiple size='10' name='Class'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='Choice'" . $row['ClassID'] . " - " . $row['ClassName'] . "'>" . $row['ClassID'] . " - " . $row['ClassName'] . "</option>";
}
echo "</select></td>";
SELECT LEFT(ClassID , 3) AS somename, ClassName
and then refer it from PHP as $row['somename']
If you are seeking to extract the string up to the decimal point, and that might not always be in the same place, you can use MySQL's SUBSTRING_INDEX() function:
SELECT SUBSTRING_INDEX(ClassID, '.', 1) FROM Class
If you are seeking to extract the integer up to the first non-decimal character, you can simply CAST() the string and MySQL will do the rest:
SELECT CAST(ClassID AS UNSIGNED) FROM Class
The benefit of this latter approach is that the resulting column will be of the correct datatype.
I have the query below which works great.
SELECT `Despatch`.`DespatchNumber` , `Despatch`.`Product` ,
`Stock`.`ProductNumber` , `Stock`.`Description` ,
`Stock`.`ProductGroup` , `Stock`.`Size` , `Stock`.`IPICODE` , `Stock`.`Fit` ,
`Stock`.`62`
FROM Stock , Despatch
WHERE `Despatch`.`DespatchNumber` = '" . $Despatch . "'
AND `Despatch`.`Product` = `Stock`.`ProductCode`
My problem is i want to expand it that im using the value of $Despatch to search another column in the same table using an OR function. So the query will look into Despatch Number and if it does not find one matching then it looks into order number as below. It doesnt work and my mysql server just hangs trying to run this query.
SELECT `Despatch`.`DespatchNumber` , `Despatch`.`Product` ,
`Stock`.`ProductNumber` , `Stock`.`Description` , `Stock`.`ProductGroup` , `Stock`.`Size` , `Stock`.`IPICODE` , `Stock`.`Fit` , `Stock`.`62`
FROM Stock , Despatch
WHERE `Despatch`.`DespatchNumber` = '" . $Despatch . "'
OR `Despatch`.`OrderNumber` = '" . $Despatch . "'
AND `Despatch`.`Product` = `Stock`.`ProductCode`
You need parentheses around your OR clause:
WHERE (`Despatch`.`DespatchNumber` = '" . $Despatch . "'
OR `Despatch`.`OrderNumber` = '" . $Despatch . "')
AND `Despatch`.`Product` = `Stock`.`ProductCode`
I'd also very strongly recommend you to explicitly use the JOIN keyword when you perform joins. Not only is this best practice, but it also would have prevented the error occuring in this case.
SELECT -- ...columns...
FROM Stock AS S
JOIN Despatch AS D
ON D.Product = S.ProductCode
WHERE D.DespatchNumber = 'DN001234'
OR D.OrderNumber = 'DN001234'
I want that when someone votes for article information, it gets inserted into two tables
(or run any two queries, does not matter, insert, update or select).
I am using Joomla! 2.5.0 Stable.
components/com_content/models/article.php
public function storeVote($pk = 0, $rate = 0)
when executing this query:
$db->setQuery(
'INSERT INTO #__content_rating ( content_id, lastip, rating_sum, rating_count )' .
' VALUES ( '.(int) $pk.', '.$db->Quote($userIP).', '.(int) $rate.', 1 )'
I want that the information in #__content table will be inserted too.
How do I achieve that?
I tried following, but it does not work:
$db->setQuery(
'INSERT INTO #__content_rating ( content_id, lastip, rating_sum, rating_count )' .
' VALUES ( '.(int) $pk.', '.$db->Quote($userIP).', '.(int) $rate.', 1 )'
// 'UPDATE #__content ' .
' SET testas2 = rating_sum + '.(int) $rate .
' WHERE content_id = '.(int) $pk
// 'INSERT INTO #__content ( testas2 )' .
' VALUES (7799)'
);
This is picture with the syntax:
http://i49.tinypic.com/1ruux0.jpg
I read about MySQL transaction, will it help me in this case? If yes, then what should the syntax should look like?
Any advice is much appreciated.
Try to run the following directly in the DB (use phpAdmin):
UPDATE edqfi_content_rating , edqfi_content
SET edqfi_content_rating.rating_count = edqfi_content_rating.rating_count + 1,
edqfi_content_rating.rating_sum = edqfi_content_rating.rating_sum + 3,
edqfi_content_rating.lastip = '88.119.189.154',
edqfi_content.testas2=edqfi_content_rating.rating_sum + 3
WHERE edqfi_content_rating.content_id = 13
AND edqfi_content.id= 13
and see if you get any errors.