how to use round function with query have where clause? - php

i want to query something from mysql database and put condition that check equality between a value in the table but after round it and other value also after round it how to do this
in php codeigniter ,plz help
i put this instruction put it didn't work
$t=round($latitude,4);
$t1=round($longitude,4);
$this->db->select('place_name');
$this->db->from('place');
$this->db->where(round(`Latitude`,4), $t);
$this->db->where(round(`Longitude`,4),$t1);
$q = $this->db->get();

You need to quote the round call, because right now you're trying to execute a PHP round() function, not the SQL one:
$this->db->where('round(`Latitude`,4)', $t);
^-- ^--
quoting it turns the whole thing into a string, which gets passed into the DB.

Related

Fat Free Framework PHP Sql statement with in expression

I'm trying to include a list of strings to be used in an "in" expression in a sql statement for example:
select * from poop where id in ('asd','sas','ser')
I want to pass the in parameter from a variable. The quoting is really screwing me up. Should I be passing this as a string which I have been trying to no avail by making a comma seperated string that looks like this:
282366381A,240506808A,244154247A,491404349A,242443808B,328409296A,239723812A,383423679M
or "282366381A","240506808A","244154247A","491404349A","242443808B","328409296A"
or
'282366381A','240506808A','244154247A','491404349A','242443808B','328409296A'
None of these work or is there a different way using an array of values?
This is the statement I'm using with the string:
$cernerResults = $this->cernerdb->exec( "select
pat as HICN,
from pat
where
HICN in ( ? )", $hicsString );
Edit:
I was able to get around this by constructing the entire query as a string like this:
$query = "select pat as HICN from pat where HICN in (".$hicsString.")";
$hicsString has single quotes around each item like so:
'282366381A','240506808A','244154247A','491404349A','242443808B','328409296A'
The problem is that providing the string to the exec would result in no results. When looking at the freetds log file the in expression values would be double quoted as a whole or each one would be double single quoted and if i used no quotes they would not be quoted at all.
All of these would make the statement return no results. I should also mention that this is a Sybase database.
I think your problem may come from the fact that PDO parser needs to have one value per question mark so it is able to validate it. So your "hack" with one question mark which is assigned to more than one value is where it fails IMHO.
This is how I handle case like that:
$values = ['asd','sas','ser'];
$count = count($values);
$results = $db->exec(
"select * from poop where id in ( ?".str_repeat(", ?", $count-1).")",
$values
);
In general I would advice you using data mappers instead of running the queries on a DB object. It is easier to iterate through them and it is more secure.

Error code 1582: Incorrect parameter count in the call to native function 'FROM_UNIXTIME'

My code is like:
$current = time();
$nexttime = mktime($hour,$minute,$second,$month,$day,$year); //$hour...from a table
and mySQL statement is like:
INSERT INTO table(...) values
('FROM_UNIXTIME(".$current.")', 'FROM_UNIXTIME(".$nexttime.")');
I found the first FROM_UNIXTIME(".$current.") is accepted by mySQL, while the second FROM_UNIXTIME(".$nexttime.") shows
Error code 1582: Incorrect parameter count in the call to native function 'FROM_UNIXTIME'.
Any suggestion is appreciated.
Remove the quotes, MySQL functions must be outside the quotes (or else it's handled as a string).
INSERT INTO table(...) values
(FROM_UNIXTIME(".$current."), FROM_UNIXTIME(".$nexttime."));

How to insert a variable into Kohana DB::expr?

I would like to do something like this in Kohana:
$var=someFunction($id);
$q=DB::select(array(DB::expr('table.field-',**$var**),'aliasname'))->from('table')->where('id','=',$id);
What I wanna do is to get a field's value, but a I'd like to subtract a PHP variable from its value.
But the problem is that I cannot execute() the query, because I have to pass it to a method as its argument (without execute), so I can't execute it, and I can't get the value I want from the result array, and subtract my variable from it. I have to do it in MySQL query in some way, and I hope it's possible with DB::expr.
Is that any possible way to do this ?
How about:
$var=someFunction($id);
$q=DB::select(array(DB::expr("(table.field - {$var})"),'aliasname'))->from('table')->where('id','=',$id);
$var = 5; $q = DB::select(array(DB::expr("(table.field - 0)", [$var]),'aliasname'))->from('table')->where('id','=',$id);

CakePHP is converting MySQL integers into strings... messing up rand() function

I want to grab a random sample of data out of my database using CakePHP. Here's my function:
function categories_list()
{
$this->paginate['limit'] = 6;
$this->paginate['order'] = '';
$this->paginate['conditions'] = '';
// Sort Randomly Start
if ($this->Session->check('Category.randomSeed'))
{
$seed = $this->Session->read('Category.randomSeed');
} else {
$seed = mt_rand();
$this->Session->write('Category.randomSeed', $seed);
}
$this->paginate['order'] = sprintf('RAND(%d)', $seed);
// Sort Randomly End
$this->set('cat_ajax_items', $this->paginate('Category'));
}
The problem is, the query that Cake sends to the DB always does this to the RAND() portion, sending MySQL into a hissy fit:
ORDER BY RAND(`1235123412341`)
Testing on a manual query, it works just fine, and returns a sample when it's formatted like this:
ORDER BY RAND(1235123412341)
Is there any way to get Cake to back off of the autoformatting? Anything I put into that RAND() function gets dumped into string quotes.
Anything I put into that RAND() function gets dumped into string quotes.
No, this isn't correct. If it used string quotes then it would work fine, however backticks aren't string quotes. The problem is that CakePHP is quoting the number as if it were a column name. Try quoting the value using single quotes instead:
"RAND('%d')"
This should result in the following SQL being produced:
ORDER BY RAND('1235123412341')
This gives the same result as when you don't include the quotes.
many applications and frameworks try to use a so called smart determination of the type of variable before they insert them into a database
however, many of these also fail with integers and strings :)
because of PHP's automatic typecasting, you can do a following check: is_int('01234') and that would return TRUE - but that actually is not true - the "number" is actually a string, starting with 0 - and so it should be handled (unless manually converted into an integet before, if that's what it should be)
you will need to adjust CakePHP's database class where it checks for data types
I'm not familiar with CakePHP, but CodeIgniter did use a following check in its escape() function:
if (is_string($str))
... which I've changed to:
if (is_string($str) && (mb_strlen((int) $str) != strlen($str)))
... and now it all works :)
P.S.: I've tried using (int) $str === $str, however that always yielded incorrect result

Getting single value from a single row with Mysql/PHP

Often I just need to get a single value from MySQL that I know exists there. I use the following construct:
$result = end(mysql_fetch_array(mysql_query('SELECT FOUND_ROWS()', $db)));
Is there a proper single function in PHP that would do this?
Yes, mysql_result will do this.
$result = mysql_result(mysql_query('SELECT FOUND_ROWS()', $db), 0);

Categories