Medoo php sql - change search term - php

i am using the "medoo"-php/mysql-class (http://www.medoo.in) for my latest project. I like the quiet easy way to work with my SQL-stuff.
But i wondered if it is possible to change the search term according to an option from an input-select-form. Lets say we got search-option "user by name", we would go with:
$data = $database->select("account", [
"user_name",
"user_id",
"phone",
"email",
"age"], [
"user_name" => $_POST['user_name']]);
Fine so far. Now we get the search option "user by ID". Every thing else stays just the same. So i need only the
["user_name" => $_POST['user_name']]);
to be like
["user_id" => $_POST['user_id']]);
Is there a way to change that without writing the whole statement again?
It is just a example. Moreover for my project i will need to change other options of the query like update to insert oder different join-options. But for the moment i am fine with an answer to that.
Anyway, what do you think about the Medoo-Class. Got any cool alternative Class-solutions for me? Basically i know how to work with SQL querys but getting stuff to the array at the end is always driving me crazy. So i would love to get stuff faster and easier with a class.
Thx a lot for your help!
Best, Lox

You can just change the $where array before running the query like that:
if ($type == 'id')
{
$where = ["user_id" => $_POST['user_id']];
}
if ($type == 'user_name')
{
$where = ["user_name" => $_POST['user_name']];
}
$data = $database->select("account",
[
"user_name",
"user_id",
"phone",
"email",
"age"
],
$where
);

Related

Substring search is failing...Any ideas?

I am trying to find the occurrence of a substring in an array.
Here is the string:
$string = "πŸŽ₯πŸŽ₯πŸ”₯πŸ”₯πŸ”₯Watch the game at #TheGoalTX on #GoaltimeSaturdays!"
Here is the array:
$venues = array (
[ "name" => "McDonalds", "slogan" => "Im Lovin It" ],
[ "name" => "The Goal", "slogan" => "GoaltimeSaturday" ],
[ "name" => "McDonalds", "slogan" => "McRibs are back" ],
);
Im running an array search in which I want to run a script if either search condition is true:
foreach ($venues as $venue) {
if ((stristr($string,$venue['name'])) OR (stristr($string,$venue['slogan'])) !== false) {
include "do.stuff.php";
}
}
The two array rows with McDonalds obviously fails. No problem.
But for some reason, the search is failing for the "slogan" element "GoaltimeSaturday" and Im not quite sure why. It should meet the "if" criteria but "do.stuff.php" is never reached.
A little help here / any ideas? Are the ""πŸŽ₯πŸŽ₯πŸ”₯πŸ”₯πŸ”₯" emotes or the "#" or "#" characters in the string the issue perhaps? Im at a loss as to why Im not getting a true/positive search result in the foreach loop...
I appreciate the assistance on this...
If include "do.stuff.php" never reached, but you await for reaching it, it means your condition is incorrect.
I think the problem is extra braces, try this:
if ((stristr($string,$venue['name']) OR stristr($string,$venue['slogan'])) !== false)
Also i don't know about difference between OR logic operators 'OR' or '||'.
UPD
May you show do.stuff.php file content?
Is there are any errors or notices in logs or console output?

Prevent duplication in between same id's item but it's ok to duplicate between different id

This is a bit hard to solve for me. I want to prevent duplication between same id's item in DB. But it's ok to duplicate between different id.
For example when the scraping bot run. It scrapes a website. Taking "plan names" from different pages. Some times different pages has same plan name. it means duplication is ok for the different id. But not the same page's item(I mean same id)
The code block below. It prevent all duplication. I am not sure how to improve it.
foreach($planNames as $k => $names)
{
$database = [];
$database = [
"place_id" => $insertedPlaceId,
"plan_name" => $names,
"plan_price" => $planPrice[$k],
"people" => $people[$k]
];
if ($place = Plan::where("plan_name", "=", $names)->first()) {
} else {
Plan::insertGetId($database);
$this->line("Plans inserted.");
}
}
If I understand correctly you want to prevent same plan_name in same place_id therefore you should query this
$place = Plan::where("plan_name", "=", $names)->where("place_id", $insertedPlaceId)->first();
then check if its empty or not.
Hope it helps. If my understanding on your question is wrong then kindly correct me. Thanks

Php error trying to update MongoDB array

I have some documents in mongodb that look like this
{ "_id" : ObjectId("576f46ca6803fab30e7b23c8"),
"username" : "Dallas",
"likes" : [ "576f46ca6803fab30e7b23c8", "576f4c446803faae0e7b23c9" ]}
{ "_id" : ObjectId("576f46ca6803fab31e7b23c8"),
"username" : "Dallas",
"likes" : [ ]}
<?php
$m = new MongoClient();
$db = $m->testing;
$collection = $db->testCollection;
addLike();
//Add likes
function addLike()
{
$id = new MongoId("576f46ca6803fab30e7b23c8");
$collection->update(array('_id' => $id),array('$addToSet' => array('likes' => '4d0b9c7a8b012fe287547157')));
echo 'addLike seemed to work';
}
I know the line of code that is wrong (syntax-wise I'm guessing) is the line that does the ...->update()
If i comment this out, I make it to the echo.
What I'm trying to do is store all of the user ids inside of the likes array, as you see one of the posts has an empty array, that's how I plan to start all posts.
I am confused as to why this code doesn't work, I pretty much copied it directly from here
EDIT:
To add, I really like what the above post says about it not allowing it to make duplicates when using $addToSet. As it shouldn't be possible for the user to like a post more than once, there is never a time the id should be in more than once
It appears all of my code was actually correct, except for some failure to understand php syntax.
I was under the impression that creating a collection variable outside of the function was global, It turned out that it didn't want me to call collection inside of that function so by deleting the function it was fixed.

mongo->php, querying inside embeded objects

I have this structure:
"_id": NumberInt(1),
"link_id": {
"1000748": {
"pi": NumberInt(34),
"li": NumberInt(8)
},
"1002836": {
"pi": NumberInt(21),
"li": NumberInt(1002836)
}
}
I want to make a query to select only the link_ids with a 'pi' => 34. I have tried in php $res = $collection->findOne(array("_id" => intval($_catids['categoryid'])), array("linkid.$.pi" => intval(34)));
No success. Any ideas? Thx a lot!
First off I recommend to use the MongoId object for the _id field, has a lot of options and a lot of usefull functions within it
But that did not answer the question, the query is as followed
{
"linked_id.pi": 34
}
Then you translated to PHP it is
array(
'linked_id.pi' => 34,
)
Then depening on the result you want you need to use find or findOne

Query mongodb with php-mongo

I am building a simple messaging system, and i have a collection in mongodb with documents like this:
{ "_id" : ObjectId("50ad003f9811e5bc5c000000"), "between" : [ "user1,user2,user3" ] }
I want to perform this query:
db.conversations.find({'between': ["user1,user2,user3"]});
to get this exact document back. This query works in mongo shell.
in php-mongo, i tried this:
$collection->find(array("between"=>array("user1", "user2", "user3")));
but it does not work.
What am i doing wrong ?
Wouldn't you want to do an In query here?
db.collection.find( { "between" : { $in : ["user1", "user2", "user3"] } } );
See In query here:
Mongo Advanced $in query
making your PHP query look like:
$collection->find(array("between"=>array("$in"=>array("user1", "user2", "user3"))));
//untested, should be something similar to this.
or if you're trying to find it exactly wouldn't you just be able to do:
$collection->find(array("between"=>array("user1,user2,user3")));
First of all when you are saving your data you have to use array not a string
{ "between" : [ "user1,user2,user3" ] }
this stores in "between" an array of one element "user1,user2,user3"
Basically when you do your query in shell everything is ok, because you are asking for a array with one element. But in php you are asking for an array of three elements.
So, when you save your data, most probably that is what you need :
{ "between" : [ "user1","user2","user3" ] }
and this will give you an array of three users.
Then read the documentation http://www.mongodb.org/display/DOCS/Advanced+Queries to and adjust your query depends on what you need: either the exact array or only some elements in the array
Have you tried:
$collection->find(array("between"=>"user1,user2,user3"));
or
$collection->find(array( "$elemMatch" => array( "between"=>"user1,user2,user3" ));
The $in operator is analogous to the SQL IN modifier, allowing you to specify an array of possible matches.
Consider the following example which uses the $or operator.
$collection->find([
'select' => ['$in' => ['option 1', 'option 2', 'option 3']]
]);
References

Categories