Zend DB Expr getting values from previous join - mysql

I have the following code in a zend db query
->join(array('z' => new Zend_Db_Expr('(' . $this->_dbhInstance->select()->from('zipcode', array('lat', 'lon', 'zip'))
->group('zip') . ')')), 'zip = f.zipcode')
->join(array('distance' => new Zend_Db_Expr('(SELECT GetDistance(' . $lat . ',' . $lon . ',z.lat,z.lon) as distance)')))
The issue is that z.lat,z.lon is not getting the values from the previous join (>join(array('z'), it just uses the actual text.
How can I format z.lat,z.lon) to grab the values from >join(array('z' ?

This is not working because Join has tree parameter
->join(tableName,joinCondition,fetchParameters)
but you have wrote only two...

Related

Export the result of MySQL query to my PC as a CSV file

I would like to export some MySQL-query results to my PC as a CSV-file. According to some contributions in the internet (e.g. here) it should work like the following:
mysql> SELECT 1,2,3,4,5 INTO OUTFILE 'C:\Users\MyPC\Desktop\numbers.csv';
ERROR 1 (HY000): Can't create/write to file 'C:\Users\MyPC\Desktop\numbers.csv' (Errorcode: 22 Invalid argument)
But theat leads to the given error. Waht am I doing wrong here? How can I save the output of my query as a csv ot a txt file?
P.S.: Im am using Windows
First of all, you didn't mention FROM which database you want the entrys.
SELECT 1,2,3,4,5 FROM ??????????????? INTO ...
With PHP you can do it like this:
$dz=fopen("file.csv", "w+");
$sql = "SELECT 1,2,3,4,5 FROM ?????????";
if ($result = $connectiontodatabase->query($sql)) {
while ($row = $result->fetch_assoc()) {
fputs($dz, $row['1'] . "\t" . $row['2'] . "\t" . $row['3' . "\t" . $row['4'] . "\t" . $row['5'] . "\t\n");
}}
fclose($dz);
$connectiontodatabase of course needs to be the new mysqli statement

Joomla Jdatabase query code with join does not work

I run the php code below within the 'Eval' section of a Fabrik form element. The code is supposed to return/put a number in a form field, but nothing appears in the form field.
When I used another query (refer to '$query-> ' lines) the code does work, so I get the impression that the query contains errors; however, when executing the related webpage with the form fields no sql error appears.
I have no idea what is wrong with the query(?)
Code:
$form_productname = 'testproduct';
$form_username = 'myname';
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
//$query->select($db->quoteName(array('a.id', 'b.productid')));
$query->select($db->quoteName('b.productid'));
$query->from($db->quoteName('#__products', 'b'));
$query->join('INNER', $db->quoteName('#__extendedreg_users', 'a') . ' ON (' . $db->quoteName('a.user_id') . ' = ' . $db->quoteName('b.owner') . ')
AND (' . $db->quoteName('a.cf_collectivename') . ' = ' . $db->quote($form_username) . ')
AND (' . $db->quoteName('b.productname') . ' = ' . $db->quote($form_productname)).')'.;
//echo $query;exit;
$db->setQuery($query);
$db->execute();
$results = $db->loadObjectList();
return count($results);
UPDATE: cause was syntax php error in where statement:
. $db->quote($form_productname)).
must be:
. $db->quote($form_productname).
You are not getting any errors because you are not catching any errors. Have a look at How to do SQL exception / error handling.
Do at least a $query->dump() and run your query in a console if you can't figure out what is wrong.
I don't understand why are you quoting the value you are comparing $form_username and $form_productname. But maybe it's late and I am tired.

Perl: Breaking out of foreach loop when last array element is encountered

Perl noob here. I have a small script (see below) that I'm using to build a MySQL INSERT statement.
use strict;
my #records = qw/Record1 Record2 Record3/;
my $insert = "
INSERT INTO table
VALUES
";
foreach my $record (#records) {
$insert .= "('" . $record . "'),\n ";
}
print "$insert\n";
Current Output
INSERT INTO table
VALUES
('Record1'),
('Record2'),
('Record3'),
I need to know how to break at the last element of the #records array and append a ; instead of ,
Desired Output
INSERT INTO table
VALUES
('Record1'),
('Record2'),
('Record3');
You can do that with map and join.
my #records = qw/Record1 Record2 Record3/;
my $insert = "
INSERT INTO table
VALUES
";
$insert .= join ',', map { '(' . $dbh->quote($_) . ')' } #records;
$insert .= ';'; # this line is not needed
The quote method of $dbh is better than just putting the stuff into quotes because it handles bad stuff for you. The map is not much different from a foreach loop, and the join will take care of putting the commas in between the elements, and not the last one.
On a related matter, I always try to avoid putting data and sql statements on the same line, thus minimize the risk of sql injection. In perl you have a prepare/execute mechanism available for this:
my #records = qw/Record1 Record2 Record3/;
$sth = $dbh->prepare("INSERT INTO table VALUES ?");
foreach my $record (#records) {
$sth->execute($record);
}
http://bobby-tables.com/perl.html

Searching MySQL DB using PHP

I use CodeIgniter + MySQL.
I am succeeding in running this query and parsing the result.
$var1 = $this->db->query("select title from stores where title like \"" . $str . "%\" union select title from coupons where title like \"" . $str . "%\"");
return $var1;
And in the controller, I parse the result() of this query to a JSON file using json_encode
But when I run the same query on another table, and follow the same step in parsing, I am facing parsing problems.
$var1 = $this->db->query("select tagword from tags where tagword like \"" . $str ."%\"");
Am I doing something wrong here?
Try echo-ing your SQL Statement
echo $this->db->last_query();
Then try Run SQL query/queries on your database for your debugging purpose

How to use agregate Mysql functions with CakePHP

There is a way to use agregate functions with cakephp? like sum() or avg() with find() method.
UPDATE:
I missed a line in the book
array('fields'=>array('Product.type','MIN(Product.price) as price'), 'group' => 'Product.type');
Showing the basic structure for doing that.
Thanks for the help
In the fields parameter of a find method call, you may pass the field processed by an aggregated function. Example:
$Model->find('all',
array(
'anything' => array(/* */),
'fields' => array(
'SUM (Model.attribute) AS total',
'OTHERFUNCTION(OModel.other_attribute) AS other'
),
'otherthing' => array(/* */)
)
);
In exactly the same way, since CakePHP is just a PHP framework.
<?php
// Make a MySQL Connection
$query = "SELECT type, SUM(price) FROM products GROUP BY type";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "Total ". $row['type']. " = $". $row['SUM(price)'];
echo "<br />";
}
?>