Using the zend framework i have used a query,having a IN clause,this is my query
$select->where('p.brandid IN (?)',$details[brand]);
in the above query, $details[brand] has a value like this array(1,2,3) .
Actually the query has to return all the values which are all related to this array(1,2,3).
But my query is returning the result related to the first value present in the above array(1,2,3).ie 1 alone other 2,3 is not considered.
when i print this query it shows like this
[where] => Array
(
[0] => (p.brandid IN ('1,2,3'))
)
Can anyone show me what is the mistake i have made or solution for this..
This is because your query is getting formed wrongly p.brandid IN ('1,2,3') instead of p.brandid IN (1,2,3) you can try using implode function in php
$select->where('p.brandid IN (?)',implode(",",$details[brand]));
Just small research, because I have the same problem.
I am not sure what version of Zend do you use. But solution provided by #Omesh doesn't work with my version 1.12.
In my case it is absolutely opposite explode solution:
$select->where('p.brandid IN (?)', explode(',',$details[brand]));
Probably it depends on the type of $details['brand']. In my case I have string like 555,666,777,877. But even if you have array there. That is strange if Zend accept in your case string (result of implode) and does not accept array. And in my case it does not accept string but accept array.
You can modify this according to Zend framwork
locate(concat(',',$details[brand],','),concat(',',p.brandid,','))>0
Just use
$select->where->in('field_name', $your_simple_array);
If you using where() function with something criteria before, like
$select->where(['field_name' => $value]);
just use the first one after it, like
$select->where(['field_name' => $value]);
$select->where->in('field_name', $your_simple_array);
Always remember to use where not as a function where(), but just as a keyword.
This is valid and tested by me in the following context:
$select = $this->tableGateway->getSql()->select()->where(['field1' => $v1, 'field2' => $v2]);
$select->where->in('field_name', ['v1', 'v2', 'v3']);
That is, using these libraries in the beginning of the model class:
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
Related
I am try to getting started with Sphinx. I add some results to index, download sphinxapi.php, and when I do this:
$cl = new SphinxClient();
$cl->SetServer( "localhost", 9312 );
// SPH_MATCH_ALL will match all words in the search term
$cl->SetMatchMode(SPH_MATCH_ALL);
$result = $cl->Query("test");
I getting this (row with id = 5 where title = test):
array (size=1)
5 => // id of post in database
array (size=2)
'weight' => string '2' (length=1)
'attrs' =>
array (size=0)
empty
But why I didnt get row from database with id = 6, where title field equal to test1 ?
And $cl->SetMatchMode(SPH_MATCH_ALL); fire error:
DEPRECATED: Do not call this method or, even better, use SphinxQL instead of an API
I comment this line in code of api file:
trigger_error ( 'DEPRECATED: Do not call this method or, even better, use SphinxQL instead of an API', E_USER_DEPRECATED );
But I dont know if it fine. Can somebody help me to understand what I am doing wrong? Thanks!
To get 'substring' matches, you need to specifically enable them.
http://sphinxsearch.com/docs/current.html#conf-min-prefix-len
(or min_infix_len)
If you dont want to see the depreciated notice, then set error_reporting
http://php.net/manual/en/function.error-reporting.php
(but even better is to rewrite the code to avoid calling the depreciated method)
warning setmatchmode
SetMatchMode are deprecated, you can still use it but it can be removed in next versions.
More info about it in:
http://sphinxsearch.com/docs/current.html#api-func-setmatchmode
http://sphinxsearch.com/blog/2013/09/11/deprecations-and-changes-in-the-2-2-series/
extracted from sphinx forum (barryhunter):
Changing the 'match mode' actually did TWO things, it changed the matching >behaviour - by
rewriting the query itself. AND changing the ranking mode.
By decoupling these concepts, I guess the idea is reduce confusion.
(for example, as soon as you choose a different matching mode, you can't >actully choose a
ranking mode)
... the match modes made sence before the 'extended syntax' was fully developed, but now
everything can be done directly via the extended syntax.
about search results
barryhunter answer is right
I suggest to read more about charset tables, morphology and stemming because i think are a better way to achieve success search than wilcard searches.
http://sphinxsearch.com/docs/current.html#conf-charset-table
http://sphinxsearch.com/docs/current.html#conf-morphology
I use this script to get the collection of my mongo database: http://datatables.net/development/server-side/php_mongodb
My question is: how to retrieve the rows where foo == 'mystring' only?
As you will notice (on line 29) from the source code in the file the mongo collection has been given the name: $m_collection as such:
$m_collection->find(array('foo' => 'mystring'))
Should work.
If this is not what you are looking for maybe you can be more specific and explain exactly what you are trying to do.
UPDATE
It has come to my attention you might want to instead edit the $searchTermsAll variable to search by this field in a doc. By the looks of it this PHP class links in the same as it would normally for SQL as such you should need to do anyhting special and can just enable filtering on datatables and add the value mystring to the foo field.
However to know if it is the right answer you will need to clarify.
UPDATE 2
A more destructive way of doing this that should keep filtering is to replace line 99 with:
$cursor = $m_collection->find(array_merge($searchTerms,
array('foo' => 'mystring')), $fields);
That will always make sure that your condition is added to the search terms but keeps the users own search terms.
Use as below
$cursor = $collection->find(array("foo" => "mystring"));
Here is more details: http://www.php.net/manual/en/mongo.queries.php
I am trying to save some db action by compiling a looped bit of code with a single query, Before I was simply adding to the the like statements using a loop before firing off the query but i cant get the same idea going in Mongo, id appreciate any ideas....
I am basically trying to do a like, but with the value as an array
('app', replaces 'mongodb' down to my CI setup )
Here's how I was doing it pre mongofication:
foreach ($workids as $workid):
$this->ci->app->or_like('work',$workid) ;
endforeach;
$query = $this->ci->db->get("who_users");
$results = $query->result();
print_r($results);
and this is how I was hoping I could get it to work, but no joy here, that function is only designed to accept strings
$query = $this->ci->app->like('work',$workids,'.',TRUE,TRUE)->get("who_users");
print_r($query);
If anyone can think of a way any cunning methods I can get my returned array with a single call again it would be great I've not found any documentation on this sort of query, The only way i can think of is to loop over the query and push it into a new results array.... but that is really gonna hurt if my app scales up.
Are you using codeigniter-mongodb-library? Based on the existing or_like() documentation, it looks like CI wraps each match with % wildcards. The equivalent query in Mongo would be a series of regex matches in an $or clause:
db.who_users.find({
$or: [
{ work: /.*workIdA.*/ },
{ work: /.*workIdB.*/ },
...
]});
Unfortunately, this is going to be quite inefficient unless (1) the work field is indexed and (2) your regexes are anchored with some constant value (e.g. /^workId.*/). This is described in more detail in Mongo's regex documentation.
Based on your comments to the OP, it looks like you're storing multiple ID's in the work field as a comma-delimited string. To take advantage of Mongo's schema, you should model this as an array of strings. Thereafter, when you query on the work field, Mongo will consider all values in the array (documented discussed here).
db.who_users.find({
work: "workIdA"
});
This query would match a record whose work value was ["workIdA", "workIdB"]. And if we need to search for one of a set of ID's (taking this back to your OR query), we can extend this example with the $in operator:
db.who_users.find({
work: { $in: ["workIdA", "workIdB", ...] }
});
If that meets your needs, be sure to index the work field as well.
I am new to php. I was wondering how I could declare a static array in php. Here is what I would do in C. How is the corresponding php code for it?
char a[][] = { (1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3) };
From what I read it has to be something like this -
$a = array( 1 => array(1,1), 2 => array(1,2), ... );
Is this correct? If so it sucks :) I hope I am wrong.
Thanks,
- Pav
You've already found the way to do it natively.
Another option would be to declare your data as JSON (a very concise and human-friendly format). This could be either in a separate file bundled with your app, or directly in your code in a string. Then parse the JSON at runtime. Since PHP isn't exactly known for speed, this may or may not make your noticeably app slower to start.
you have it already figured out in your question.
One thing I would add is that you do not need to explicitly define the keys if you are intending to use a zero based array, this is assumed and can be done like so...
$a = array(array(1,1),array(1,2), ... );
You can also use what is called associative arrays which use string keys and you define them the same way you do in your example, except use strings instead of numbers...
$ass_array = array( 'array_1' => array(1,1), 'array_2' => array(1,2), ... );
you would then call your associative array like this...
$ass_array['array_1'];
Also, if you want to append single items to an array (for example in a loop to load an array)...
$ass_array[] = $item;
Further to jondavidjohn's anwser, you could just write a quick script to grab your list of values and generate the array statement for you.
No need to care how verbose the syntax is then. If the task is long and repetitious enough to care, don't do it by hand. :)
My MySQL queries are returning arrays with duplicate entries: numbered keys and labeled keys with the same data inside. This may be standard, but it seems like a waste, and something that could cause problems if I'm printing values. I mean, not a huge problem, obviously. But I'm just curious if I can stop that. It seems unnecessary. For example:
Array(
[0] => "Ted",
[first_name] => "Ted",
[1] => "Schmidlap",
[last_name] => "Schmidlap"
)
And so on.
I'm pretty new to a lot of this, so this may be a simple question, but Googling doesn't seem to have any answers for me. Anyone know the reason this happens? I'm using PHP's PDO now, but I was doing it straight through the MySQL functions before and the same thing was happening, so I assume it's a byproduct of MySQL interaction.
I can iterate through and unset the numeric ones, because I don't need them, but they're not really in the way right now, so that's just an extra step. Still, is there a way to simply not have them fetched in the first place?
Presumably this is happening after you use mysql_fetch_array (or similar).
You need to add in a flag to specify what array type you want returned or PHP assumes both.
i.e. mysql_fetch_array($result_set, MYSQL_ASSOC|MYSQL_NUM|MYSQL_BOTH)
That depends on the function you are using.
Some functions return both types, others return only one of them.
If you are using PDOStatement->fetch, notice the optional $fetch_style argument it takes.
Your issue is with the mysql_fetch_array function.
If you want only the numbers, use:
$row = mysql_fetch_array($result, MYSQL_NUM)
If you want only the text indexes, use:
$row = mysql_fetch_array($result, MYSQL_ASSOC)
I usually use MySQLi but for PDO you can find more information here: http://us3.php.net/manual/en/pdostatement.fetch.php
Which seems to mean that you should be using this for text indexes:
$row = $statement->fetch(PDO::FETCH_ASSOC);
And this for numeric indexes:
$row = $statement->fetch(PDO::FETCH_NUM);
Also, just to note this since it isn't listed here, if you want an associative array, you can use mysql_fetch_assoc(), or if you want an enumerated (numbered) array use mysql_fetch_row() instead of mysql_fetch_array(). I use this mostly because without it I would often forget the flag, so I got into the habit of just using the specific function.