How to build advance query builder in PHP - php

I want to create advance query builder using PHP code.
Input :
search_String_1 AND( search_String_2 OR search_String_3) ... so on
Goal :
check AND / OR operator rules including () parenthesis and convert it into following String:
Query :
db.table.search_field LIKE '
search_String_1' AND ( db.table.search_field LIKE 'search_String_2' OR db.table.search_field LIKE 'search_String_3')'
My efforts:
I have use preg_match() but I dont know how to check all operator and query sequences by best solution. Also I have exploded user input by "blank_space" , so i have all token in one array. I am still trying to find solution. If I am wrong or there is a good solution please suggest me to do so.
Thanks for your help.

Take a look at this class. It may be usefull.
http://www.phpclasses.org/browse/file/12314.html

Related

how to link array element to a string

The following instruction -
$this->menuTablesArr[$i]."_tablelink_attrs"
works correctly in older versions but in PHP5 it gives this error:
error type 8 Array to string conversion
Could anyone help ? many thanks
In your question isn't clear, what you mean by "works correctly". What is the outcome, when it works correctly?
I will assume, that this error is due to concatenating array and string. It can solve PHP function implode. This function can have one or two parameters.
In your case you can use it e.g. with only one parameter
implode($this->menuTablesArr[$i])."_tablelink_attrs"
If you want to use some glue between items of array, you can use two parameters. More documentation about this function is in the link above.

Insert into sql, insert values within loop

What is the right syntax for code above?
$_POST[firstname{$i}], $_POST[firstname+$i], $_POST[firstname.$i]
I searched over and over again but I couldn't find any answer yet : /
How am I suppose to put the $i after those names?
The correct syntax:
"... values ( ".$_POST['firstname'.$i]." ) ..."
try this
$firstname = $_POST['firstname'.$i];
and user $firstname variable in your query
You cannot use array subscripts in a string like that. You'd have to write "{$_POST['firstname'.$i]}" (you were also missing the quotes around the first part of the array subscript, such as 'firstname').
If you plan on doing that though, you will open your application to hackers (read here on how to prevent sql injections in php).
"hi dear mistysnake u can use 'Peter''".$i."' peter is the static value and u can insert value and your problem is the u can put the $_GET"

Build a php search engine using an array as DB?

new php programmer here, I apologize if this has already been asked:
I have an array filled with strings. Each string is the exact name of a page on my site, so the array is a sort of 'database'.
I'm wondering how to create a search engine that will let the user enter a keyword. The engine will then crawl my array of strings for any matches, and return a list of pages on my site that include the keyword in the pagename.
Any help is appreciated, not sure which functions to use to build this.
Thanks in advance!
I guess that you could just use strpos to see if the users term is contained inside of an array key. You would have to loop through each page, though, and see if the search term is contained within the pagename.
The way I would approach this is by creating a binary search tree with all the strings. That would make the search faster and more efficient. Here is a LINK that could help you achieve that.
Hope this helps.
I ended up solving this problem by using a flat file and conditionals, here is the code:
$userlink = $_POST['link_to_test'];
$linkdatabase = array('...');
if (in_array($userlink, $linkdatabase))
{
echo 'Data already exists';
}
else { ....

How to Work with PostgreSQL Function Output in PHP

I have a postgresql (V 8.4) function that returns SETOF a custom type. An example of the string output from this function is the following 4 rows:
(2,"CD,100"," ","2010-09-08 14:07:59",New,0,,,,,,"2010-09-06 16:51:51","2010-09-07 16:51:57",)
(5,CD101,asdf,"2010-08-08 14:12:00",Suspended-Screen,1,10000,,,,,,,)
(4,DNR100,asdf,"2010-09-08 14:10:31",Suspended-Investgate,0,,,,,,"2010-09-06 16:51:51","2010-09-07 16:51:57",)
(3,MNSCU100," ","2010-09-08 14:09:07",Active,0,,,,,,,,)
I need to work with this data in PHP and I'm trying to figure out the best way to work with it. What I would love is if there was a way for postgresql to return this like a table where columns represent each value within a record rather than as a comma-separated string.
Is this possible? If not, what is the best way to work with this comma-separated string of values in PHP?
I've see this post (Convert PostgreSQL array to PHP array) and can use the function mentioned there but I wanted to ask if anyone has other ideas or suggestions.
Thanks,
Bart
There's str_getcsv() which'll parse a string as CSV data and return an array of the individual fields
Yep, its real easy, just change the way you are calling the function.
Instead of
SELECT my_srf(parm1);
Do either:
SELECT * FROM my_srf(parm1);
SELECT (my_srf(parm1)).*;
You'll even get the column names out this way.

Selecting only the first item of an xpath result set in PHP

I am currently achieving the desired outcome with two PHP statements:
$thisBlarg = $xmlResource->xpath('//blarg[#ID='.$someBlargID.']');
echo $thisBlarg[0]->name;
But, not wanting to settle for second best, I'd really prefer this to be one statement, but PHP doesn't like this:
echo $xmlResource->xpath('//blarg[#ID='.$someBlargID.']')[0]->name;
And for good reason. But I can't find a way to force an xpath query to return the result directly. Any suggestions?
Try this
echo current(($xmlResource->xpath('//blarg[#ID='.$someBlargID.']')))->name;

Categories