Using a php html form to search MongoDB database and return search value


#1

Hi All,

Can somebody more experienced than me help figure this out, I am trying to understand how to use a php form to search for a specific ["Street"] or ["Town" and more attributes from the Mongodb shell and output all the results from the search box in the form.. So what I want is for the user to search for "London" and all attributes linking to London appear in the results.. here is what I currently have

PHP page - Search.php - The page containing the form

<form action="mongophp/Search.php" method="GET">
    Street: < input type="text" name="Street"> 
    < input type="submit" value="submit">
</form>

PHP SCRIPT - mongodb/Search.php

// Connecting to server
$m = new MongoDB\Client("mongodb://localhost:27017");
$db=$m->CSV;
$mObj=$db->Schools->find();

$Street = $_GET['Street'];

foreach($mObj as $row) {
    echo $row['Street'];
}

What I currently see outputted into the browser - JSON text from MongoDB shell

St James's PassageSt Giles' Terrace2 New ChangeQueen Victoria Street49 Mecklenburgh SquareAgincourt RoadRobson House,Tonbridge StreetDornfell StreetCliff VillasChester RoadGrafton Road174 Ossulston StreetFleet RoadHawley Road74 Stanhope StreetStreatley PlacePrincess RoadRhyl StreetCamden StreetTorriano AvenueMansfield Road86A Fitzjohn's AvenueLupton StreetChrist Church HillRedhill Street152-158 Mill LaneHolly Bush ValeTrinity


#2

When you post code on the forums, you need to format it so it will display correctly. You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don't have that character.


#3

Thank you for the update .. I hope someone can help answer my question


#4

It seems that in this line of code:

$mObj=$db->Schools->find();

you are retrieving all rows, rather than specifying your search parameters. I don't know anything about mongoDB but this page may help: https://docs.mongodb.com/manual/reference/method/db.collection.find/

It seems to me that you need to extract your search parameter before you call find() and pass it in as a parameter so that the only rows returned match the search.

You could just call find() as you do, and then in your foreach() loop process each row with your search criteria using strpos() to search for the string, but it would be much better practice (and quicker, once you have a decent number of rows in the database) to allow the database to match them.


#5

I now have the following error showing can anybody help me with

> <form action="mongophp/Search.php" method="GET">
> Street: <input type="text" name="Street"><br><br>
> Town: <input type="text" name="Town"><br><br>
> Postcode: <input type="text" name="Postcode"><br><br>
> <input type="submit" value="submit">
> </form>

Code -

$m = new MongoDB\Client("mongodb://localhost:27017");
$db=$m->CSV;
$mObj=$db->Schools->find();

$Street = $_GET['Street'];
$Town = $_GET['Town'];
$Postcode = $_GET['Postcode'];

foreach($mObj as $oneElement) {
foreach($oneElement['Street'] as $comment) {
	
}
       
}

Now the for each loop shows me the following can someone help me to find out what I am doing wrong

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\PhpApp\mongophp\Search.php on line 33

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\PhpApp\mongophp\Search.php on line 33

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\PhpApp\mongophp\Search.php on line 33

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\PhpApp\mongophp\Search.php on line 33

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\PhpApp\mongophp\Search.php on line 33

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\PhpApp\mongophp\Search.php on line 33

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\PhpApp\mongophp\Search.php on line 33

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\PhpApp\mongophp\Search.php on line 33

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\PhpApp\mongophp\Search.php on line 33

#6

Which is line 33? I expect it's the "inner" foreach loop. Within the outer loop, you are surely presented with an array called $oneElement, which has several elements? If that's the case, I can't see how the inner foreach would work because there is only one $oneElement['Street'], not several.

(Again though, I know nothing about mongoDB).


#7

I have changed the code for now mate, I am no longer doing this although I am now looking to find out how to use MongoDB to show the execution time once data is selected from the database through php


#8

Well, I posted elsewhere on how to do that in PHP, but I'm not sure how you'd use MongoDB itself to do it. I did wonder whether the results object contained execution time, but didn't look very far to find out.


#9

yes I believe you can use Microtime to do so I have this all covered I just now need to find MongoDB


#10

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.