Drupal db_query_range SQL query - drupal

I'm trying to get this working and the query executes but nothing comes back. I've tried everything I can think of, can you spot what I'm doing wrong?
$nido = $node->nid;
$result = db_query_range('
SELECT i.nid, i.iid, a.fid, p.filename, p.filepath
FROM {drup_image_attach} i
LEFT JOIN {drup_image} a ON i.iid = a.nid
LEFT JOIN {drup_files} p ON a.fid = p.fid
WHERE i.nid = %d AND p.filename = "recipe_thumb"', $nido, 0, 10);
echo "Filepath = " . $result ->filepath. "<br>";
echo "Filepath = " . $result ->filename . "<br>";
echo "IID = " . $result ->iid. "<br>";
echo "NID = " . $result ->nid . "<br>";
}
EDIT - I sorted out a couple of bits, but the output is still empty!
EDIT - this is the working code:
$nodeid = $node->nid;
$get_image = db_query('
SELECT p.filepath as imagefilename
FROM {image_attach} i
LEFT JOIN {image} a ON i.iid = a.nid
LEFT JOIN {files} p ON a.fid = p.fid
WHERE i.nid = %d AND p.filename = "recipe_thumb"', $nodeid);
$obj_image = db_fetch_object($get_image);
$imagefilename = $obj_image->imagefilename;

$result is a mysql(i) resource only. You first have to fetch the row/object.
$result = db_query_range(....);
$object = db_fetch_object($result);
print_r $object;

Assuming you have a range ob results not a single result this should work. You need to store the results in an array or process them in a different manner. for a single result see db_result()
$nodeid = $node->nid;
$get_image = db_query('
SELECT p.filepath as imagefilename
FROM {image_attach} i
LEFT JOIN {image} a ON i.iid = a.nid
LEFT JOIN {files} p ON a.fid = p.fid
WHERE i.nid = %d AND p.filename = "recipe_thumb"', $nodeid);
while(($obj_image = db_fetch_object($obj_image)) == TRUE){
$imagefilename[] = $obj_image->imagefilename;
}

Related

Different results if using: $wpdb->get_row($wpdb->prepare())

Simple database structure:
If I do:
SELECT info FROM `wp_participants_database` WHERE standard = '456' AND zertifikatsnummer = 'iso777' AND _firmennamen = 'Firma789';
I get: "d", which is fine.
But when im using $wpdb, like here:
$query = "SELECT info FROM wp_participants_database WHERE standard = %d AND zertifikatsnummer = %d AND _firmennamen = %d";
$firmennamenx = $wpdb->get_row($wpdb->prepare( $query, '456', 'iso777', 'Firma789' ));
echo $firmennamenx->info;
I get: "b", why?
Try with below code
$query = "SELECT info FROM wp_participants_database WHERE standard = %d AND zertifikatsnummer = %s AND _firmennamen = %s";
$firmennamenx = $wpdb->get_row($wpdb->prepare( $query, '456', 'iso777', 'Firma789' ));
echo $firmennamenx->info;
Here %d is for the decimal values and %s for the string value. Your other fields are having the string value so for that you need to set %s
Hope now you got my point why you need to set %s.

Operator "NOT LIKE" not work correctly

i am creating a new query in my app.
I use filters, with different options (Contain, equal or different).
If I use operator "different", my query generate like this:
SELECT p FROM ProductosBundle:Producto p
LEFT JOIN p.tipo t
LEFT JOIN p.departamento d
WHERE 1=1 AND t.nombre NOT LIKE '%articulo%'
I have product different from "articulo", but return 0 results...
I tried with NOT LIKE, != and <>, but not return results.
Is there something wrong in my condition where? I do not see the problem, with contain and equal it works.
Any idea? Thanks
EDIT 1:
This is my controller:
...
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT p FROM ProductosBundle:Producto p LEFT JOIN p.tipo t LEFT JOIN p.departamento d";
if (isset($_GET['filterField']) && isset($_GET['filterValue'])){
$valores = explode(",",$_GET['filterValue']);
$filas = explode(",",$_GET['filterField']);
$operadores = explode(",",$_GET['filterOperator']);
$dql .= " WHERE 1=1";
for($i = 0; $i < count($valores); $i++){
$dql.= " AND ". $filas[$i]. " " . $operadores[$i] ;
if($operadores[$i] == "LIKE" OR $operadores[$i] == "NOT LIKE"){
$dql .= " '%".$valores[$i]."%'";
}else{
$dql .= " '".$valores[$i]."'";
}
}
}
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$productos = $paginator->paginate(
$query,
$request->query->get('page', 1),
25
);
EDIT2:
My profiler is executing this:
SELECT DISTINCT p0_.id AS id_0
FROM producto p0_
LEFT JOIN tipo t1_ ON p0_.tipo = t1_.id
LEFT JOIN departamento d2_ ON p0_.departamento = d2_.id
WHERE t1_.nombre LIKE 'articulo'
AND 1 = 1
AND t1_.nombre NOT LIKE '%articulo%'
LIMIT 25 OFFSET 0
Its a LIKE 'Articulo'... WHY???!!!
Presumably, there's an error in your UI logic / the code setting the query parameters. This:
$valores = explode(",",$_GET['filterValue']);
$filas = explode(",",$_GET['filterField']);
$operadores = explode(",",$_GET['filterOperator']);
generates both
t1_.nombre LIKE 'articulo'
and
t1_.nombre NOT LIKE '%articulo%'
so you should take a look at the code that populates filterValue, filterField and filterOperator.

Doctrine 2 gives 500 Error on simple query

I have this doctrine query:
$attributeQuery = $em->createQuery("SELECT PARTIAL p.{id} FROM m:Product p JOIN m:BtgNode n WITH n.attribute = p.amazonCatUs1 WHERE n.id = :node")->setMaxResults(5);
$attributeQuery->setParameter('node', $amazonCategoryId);
$products = $attributeQuery->getResult();
Which causes a 500 error. When I comment out the getResult line, the page finishes loading, and when I run $attributeQuery->getSql() and execute the result though phpmyadmin the result return correctly in less than 0.001 seconds.
I cannot find anything at all to explain this behavior.
The full script:
$em = $app['orm.em'];
echo ini_get('memory_limit').'<br>';
ini_set('xdebug.var_display_max_depth', -1);
ini_set('xdebug.var_display_max_data', -1);
$productQuery = $em->createQuery("SELECT p FROM m:Product p WHERE p.amazonUsBrowse1 = :node OR p.amazonUsBrowse2 = :node");
$productQuery->setParameter('node', $amazonCategoryId);
$productQuery->useQueryCache(false);
//if results, then not clothing and simple, else complicated
$products = $productQuery->getResult();
print memory_get_usage(true);
if (count($products) == 0) {
$em->clear();
echo 'complex';
$attributeQuery = $em->createQuery("SELECT PARTIAL p.{id} FROM m:Product p JOIN m:BtgNode n WITH n.attribute = p.amazonCatUs1 WHERE n.id = :node")->setMaxResults(5);
$attributeQuery->setParameter('node', $amazonCategoryId);
//$attributeQuery->useQueryCache(false);
var_dump($attributeQuery->getSql());
$products = $attributeQuery->getResult();
var_dump($attributeQuery->getSql());
}
var_dump($products);
die();

How to get all data from easyui datagrid

I had try to use the getData e.g.
data = $("#dg").datagrid("getData");`
var total = data.total; (total is 100)`
var rows = data.rows; (rows.length is 25)`
It can result: the total number is correct like 100 records
but the rows return only get the current page rows like 25 rows.
I need to get all of the records (100 rows).
Is there something i missed ? Or how can we do this ? Please help me.
This is because of pagination. let me describe model with pagination for showing 100 row in php.
function get_invoiceinfoList($search_keyword,$custID){
$page = 1;
$rows = 100;
$sort = '';
$order = 'asc';
$offset = ($page - 1) * $rows;
if($custID > 0 && $search_keyword == 0){
$search = " and a.customer_id =$custID";
}else{
$search = "and (b.name like '%$search_keyword%' || a.inv_no like '%$search_keyword%')" ;
}
$vQuery=$this->db->query("SELECT a.inv_id,a.inv_no,a.inv_org,a.inv_date,
a.customer_id,a.heading,a.quot_id,a.total_cost as total_amount,
a.paid_amount,(a.total_cost-a.paid_amount)as due_amount,b.name
from sales_invoice a,view_customer_info b
where
a.customer_id = b.customer_id $search order by a.inv_date DESC limit $offset, $rows");
$result = $vQuery->result();
$rows = array();
foreach ($result as $rowData){
array_push($rows, $rowData);
}
$data['rows'] = $rows;
return $data;
}

Google geocoding using v3 not showing error

Kindly help on the code below. What happens is that I get the latitude and longitude of the name of a place when I click on a button. However, of late, it is not working. It prints that "Address x failed to Geocode. Received status " Note that x is a given address and no status code is given.
$id=$_REQUEST['id'];
define("MAPS_HOST", "maps.googleapis.com");
define("KEY", "xxxxxxx");
$query = "SELECT * FROM markers WHERE mid = $id LIMIT 1";
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml?address=";
if($row = #mysql_fetch_assoc($result)){
$geocode_pending = true;
$address = $row["address"];
while ($geocode_pending) {
$request_url = $base_url . urlencode($address) . "&sensor=false&key=" . KEY;
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
// Successful geocode
$geocode_pending = false;
$coordinates = $xml->Response->Placemark->Point->coordinates;
//$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
//$lat = $coordinatesSplit[1];
//$lng = $coordinatesSplit[0];
list($lat,$lng) = explode(",",$coordinates);
$query = sprintf("UPDATE markers " .
" SET lat = '%s', lng = '%s' " .
" WHERE mid = '%s' LIMIT 1;",
mysql_real_escape_string($lng),
mysql_real_escape_string($lat),
mysql_real_escape_string($id));
$update_result = mysql_query($query);
if (!$update_result) {
die("Invalid query: " . mysql_error());
}else{
echo "$lat;$lng";
}
} else if (strcmp($status, "620") == 0) {
// sent geocodes too fast
$delay += 100000;
} else {
// failure to geocode
$geocode_pending = false;
echo "Address " . $address . " failed to geocode. ";
echo "Received status " . $status . "
\n";
}
usleep($delay);
}
}
I'm not sure on which API your code is based on, but the response of the current API will not work with these lines:
$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
There are no elements Response,Status or code, furthermore the response will not contain a numeric status-code.
use this instead:
$status = $xml->status;
if (strcmp($status, "OK") == 0) {
To fix the rest of the code please take a look at the structure of the returned XML, there are also no elements Placemark,Point and coordinates.
it should be:
$lat = $xml->result->geometry->location->lat;
$lng = $xml->result->geometry->location->lng;

Resources