PHP - Search A Word With Query Select
hi all,
in mysql, how can i extract 100 words before and 100 words after my search key? example: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla est nibh, mattis eu mattis id, pulvinar eu augue. Duis ut sem nisi. Sed id ante sed orci vestibulum lacinia ac id nibh. Donec cursus, elit eget auctor semper, ipsum eros laoreet quam, nec ullamcorper" vestibulum is my search key Similar TutorialsHello, Does anyone know a tutorial I can follow to create my own search engine over the items I have in my SQL database? I find a lot of tutorials but they are all 'one word searches' which means if you type two words you will get all results that contain either word (too many hits). If the search engine tutorial displays result with AJAX even better. Thanks for help. df Hi there, I'm learning PHP and decided that I'll try to create a word search generator for my own use. I know there are hundreds of them out there but it's just for learning purposes. I'd like to enter a number of words and then the script should generate a pdf containing a box filled with random letters (among which there will be my words). My question is what keywords/functions should I look at to get started? I'm a php beginner but would like to make my learning more interesting by doing this project. The form with words should be easy, it's the next bit where it's supposed to generate a pdf containing words that I have no idea about. thank you I am trying to find the last word before a search pattern. I tried preg_match without any luck. I found something close online but it does a match for the first match for the pattern. I know that I will always see , some # pressure. I want that # right before the word pressure but there are a lot of , in the page. Thanks in advance. Code: [Select] function get_string_between($string, $start, $end){ $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); } $fullstring = "today, Sat, 750 pressure"; $parsed = get_string_between($fullstring, ", ", "pressure"); Hey All, I'm trying to add multiple word search to my site and I'm getting the following error: "mysql_num_rows(): supplied argument is not a valid MySQL result resource " I know, my search is posting because I'm getting my search terms back with the "nothing found" message I have set up. Here's what I have: Code: [Select] $search = $_POST["search"]; $arraySearch = explode(" ", $search); $arrayFields = array(0 => "title", 1 => "content"); $countSearch = count($arraySearch); $a = 0; $b = 0; $query = "SELECT * FROM table1 WHERE desc LIKE '$arraySearch'"; $countFields = count($arrayFields); while ($a < $countFields) { while ($b < $countSearch) { $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'"; $b++; if ($b < $countSearch) { $query = $query." AND "; } } $b = 0; $a++; if ($a < $countFields) { $query = $query.") OR ("; } } $query = $query.")"; $query_result = mysql_query($query); if(mysql_num_rows($query_result) < 1) { echo '<p>No matches found for "'.$search.'"</p>'; } else { Print "<table border cellpadding=3>"; while($row = mysql_fetch_assoc($query_result)) { Print "<th>Description:</th> <td>".$row['desc']. "</td> "; } Print "</table> "; } You guys are the best, thanks so much. Hello all,
Based on the suggestion of you wonderful folks here, I went away for a few days (to learn about PDO and Prepared Statements) in order to replace the MySQLi commands in my code. That's gone pretty well thus far...with me having learnt and successfully replaced most of my "bad" code with elegant, SQL-Injection-proof code (or so I hope).
The one-and-only problem I'm having (for now at least) is that I'm having trouble understanding how to execute an UPDATE query within the resultset of a SELECT query (using PDO and prepared statements, of course).
Let me explain (my scenario), and since a picture speaks a thousand words I've also inlcuded a screenshot to show you guys my setup:
In my table I have two columns (which are essentially flags i.e. Y/N), one for "items alreay purchased" and the other for "items to be purchased later". The first flag, if/when set ON (Y) will highlight row(s) in red...and the second flag will highlight row(s) in blue (when set ON).
I initially had four buttons, two each for setting the flags/columns to "Y", and another two to reverse the columns/flags to "N". That was when I had my delete functionality as a separate operation on a separate tab/list item, and that was fine.
Now that I've realized I can include both operations (update and delete) on just the one tab, I've also figured it would be better to pare down those four buttons (into just two), and set them up as a toggle feature i.e. if the value is currently "Y" then the button will set it to "N", and vice versa.
So, looking at my attached picture, if a person selects (using the checkboxes) the first four rows and clicks the first button (labeled "Toggle selected items as Purchased/Not Purchased") then the following must happen:
1. The purchased_flag for rows # 2 and 4 must be switched OFF (set to N)...so they will no longer be highlighted in red.
2. The purchased_flag for row # 3 must be switched ON (set to Y)...so that row will now be highlighted in red.
3. Nothing must be done to rows # 1 and 5 since: a) row 5 was not selected/checked to begin with, and b) row # 1 has its purchase_later_flag set ON (to Y), so it must be skipped over.
Looking at my code below, I'm guessing (and here's where I need the help) that there's something wrong in the code within the section that says "/*** loop through the results/collection of checked items ***/". I've probably made it more complex than it should be, and that's due to the fact that I have no idea what I'm doing (or rather, how I should be doing it), and this has driven me insane for the last 2 days...which prompted me to "throw in the towel" and seek the help of you very helpful and intellegent folks. BTW, I am a newbie at this, so if I could be provided the exact code, that would be most wonderful, and much highly appreciated.
Thanks to you folks, I'm feeling real good (with a great sense of achievement) after having come here and got the great advice to learn PDO and prepared statements.
Just this one nasty little hurdle is stopping me from getting to "end-of-job" on my very first WebApp. BTW, sorry about the long post...this is the best/only way I could clearly explaing my situation.
Cheers guys!
case "update-delete": if(isset($_POST['highlight-purchased'])) { // ****** Setup customized query to obtain only items that are checked ****** $sql = "SELECT * FROM shoplist WHERE"; for($i=0; $i < count($_POST['checkboxes']); $i++) { $sql=$sql . " idnumber=" . $_POST['checkboxes'][$i] . " or"; } $sql= rtrim($sql, "or"); $statement = $conn->prepare($sql); $statement->execute(); // *** fetch results for all checked items (1st query) *** // $result = $statement->fetchAll(); $statement->closeCursor(); // Setup query that will change the purchased flag to "N", if it's currently set to "Y" $sqlSetToN = "UPDATE shoplist SET purchased = 'N' WHERE purchased = 'Y'"; // Setup query that will change the purchased flag to "Y", if it's currently set to "N", "", or NULL $sqlSetToY = "UPDATE shoplist SET purchased = 'Y' WHERE purchased = 'N' OR purchased = '' OR purchased IS NULL"; $statementSetToN = $conn->prepare($sqlSetToN); $statementSetToY = $conn->prepare($sqlSetToY); /*** loop through the results/collection of checked items ***/ foreach($result as $row) { if ($row["purchased"] != "Y") { // *** fetch one row at a time pertaining to the 2nd query *** // $resultSetToY = $statementSetToY->fetch(); foreach($resultSetToY as $row) { $statementSetToY->execute(); } } else { // *** fetch one row at a time pertaining to the 2nd query *** // $resultSetToN = $statementSetToN->fetch(); foreach($resultSetToN as $row) { $statementSetToN->execute(); } } } break; }CRUD Queston.png 20.68KB 0 downloads i am having the database consists of morethan one lac rows. we have a search option in our website to search the database for required information. the present code is like this: form.php `<input type="radio" name="tag" value="city" /> CITY <br/>` `<input type="radio" name="tag" value="name" /> NAME OF CUSTOMER <br/>` `<input type="radio" name="tag" value="amount" /> CHEQUE AMOUNT <br/>` `<input type="radio" name="tag" value="somethingelse" /> some thing else ` `Enter the part of any of the above Here :<input type="text" name="value" />` search.php `$tag = $_POST['tag'];` `$value = $_POST['value'];` `$query = "SELECT * FROM database WHERE $tag LIKE '%$value%' "` note: we always input the part field only. with this some times the output comes in thousands of rows. with which we are facing problems. we want to search the two or more fields for getting more precise results. hence i tried this form: `<h3 align="center">ENTER ALL OR DESIRED ITEMS YOU WANT TO SEARCH</h3>` `<div width="80%" align="center">` `<input type="text" name="city" /> CITY <br/>` `<input type="text" name="name" /> NAME OF THE CUSTOMER <br/>` `<input type="text" name="amount" /> AMOUNT <br/>` `<input type="text" name="somethingelse" /> SOME OTHER SEARCH FIELD </div> ` `$query = "SELECT * FROM database WHERE city LIKE %$city%' || name LIKE %$name%' || amount LIKE %$amount%' || somethingelse LIKE %$somethingelse%';` it worked in the mysql console, and even in our website when we give all the variables. but it displaying the entire database when we dont give even one field in the search box. i tried to assign NULL to the variable which was not given. it is also not worked. it works if any variable is replaced with NULL in the query. i don't know how to do that. i tried a lot of queries after searching in lot of code provider websites. but none of them gave the desired results.hence i request you to provide me a sql query code for search the database using all of the above fields or any two or even with one. the code must work independent of number of fields we entered. Hi guys, I have a simple query he $sql="SELECT user_id FROM $tbl_name WHERE email='$loginuseremail' AND password='$loginpassword'"; $result=mysql_query($sql); $row = mysql_fetch_assoc($result); //START SESSION session_start(); $_SESSION['username']=$loginuseremail; $_SESSION['user_id']=$row; echo $_SESSION['user_id']; It should obviously output an ID number but instead I just get the word 'Array'. If I remove the mysql_fetch_assoc() I just get 'Resource ID #4'. Can anyone point me in the right direction? Thanks! So basically my select is giving me results that I don't want , please take a look at it , if you can help me I would appreciate it thank you.
Im searching for something with the letter j in it , be it the user or the nome_proprio row
it gives me this:
heres the code aswell
As you can see I just want where user LIKE '$procura' , i had the ,OR nome_proprio LIKE '$procura' , but it just gives more posts come from here :
If you need something more like the code itself , i can provide it Thank you for your time and help:D Edited June 15 by ribeiroI have this code below: Code: [Select] members_count <= ".$diff." || members_count >= ".$defmemcount." My test says $diff = 6 and $defmemcount = 11, members_count = 14 So how come this returns as if members_count isn't less or equal to 6 OR higher or equal to 11? i am unable to select total_comment from mysql table related to each post..
For detail information check http://stackoverflow...-query-in-mysql
Hi, Everyone! This my first time posting here. Anyways, I've been having problem getting the correct data from my database. I want to select the total_on/off_hours,exposure,plate_number, and terminal_status. But when I select the terminal_status, there are duplicates, I already used the DISTINCT function. It's a little hard to explain so, I'll just show you.
Here's a screen cap of my code trying to get just the terminal_status
What's the problem? What should I do? Any suggestions/help will be much appreciated.
ok, so I need to know how I can perform an operation of $total equaling $price1 + $price2 etc.... if they are not empty in the database............... does this make sense? Hi All. I am looking to search a database of products based upon someone typing the name of the product they are looking for. I am using the below SQL request: Unfortunately this only shows what ever your typing and anything before or after (%) is there any way that it could also search unplaced string instances? For example $q = "SELECT * FROM products WHERE prod_name LIKE '%$id%' order by fee asc LIMIT 20"; If i type black ops ps3 it brings back search result until i get to the PS3 part. because entries in the db are "black ops for the PS3" because i havent conformed it says no results. I need it to show all results. is there another way? or can someone point me in the right direction? Hi everyone! So, this is probably one of the most simplest things to all you pros out there, but unfortunately I think I'm using the wrong words in Google as I can't seem to remember what they're called (Yeah I'm not that brilliant.) Anyway, a quick one for you guys. What I'm looking for is a simple script or method to send a Search Query to end with a URL. For example. Lets say I'm on http://mysite.com/index.php, and on that page is a search box. Now, lets say I search the word "Pizza" in that search box. I want the end result to be "http://myspace.com/search.php?s=pizza" I already have the script I'd like to use to read that value, I just simply don't know how to put that value there in the first. Yes, I know, noobish question, I'm sorry. XD Thanks in advance! Hi, I have a search form where users can search by age and country. Users can also save their search so they can go back and do it again but the problem is when they reload their saved search, I need it to select the country that has been saved in the drop down. So if someone searched the United Kingdom, I need it to show United Kingdom in the select drop down instead of (Select Country). Is there any easy and quick way around this? Many Thanks Hi I need to do a SELECT query like $query = "SELECT name FROM myusers WHERE myidnum = ".$_SESSION['myid'].""; and put the results into an array like $myusers = array("David","John","Lucy","Sarah"); But I cannot figure out how to do this Can anyone help? Thanks Hello there, Having a nightmare here. It feels like what I need to do is really easy but I just can't get it to work. I have a table called "groups2" that holds a unique id and the name of an activity. I already have a query that finds the users selected groups using a while loop but I also want to show how many other members are in that group by counting the number of times the activity comes up or the id comes up. I don't know whether I need 2 while loops nested or what but I get the error Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource on line 32 which is highlighted. Can anyone help. I am no expert at php and still learning so some advice or example code would be great. $result = mysql_query("SELECT * FROM groups2 WHERE L_ID = ". $_SESSION['member_ID'] .";"); $data = mysql_query("SELECT COUNT('name') AS num FROM groups2 WHERE name = " . $row['name'] . ""); $count = mysql_fetch_assoc($data); $numbers = $count['num']; while ( $row = mysql_fetch_assoc($result) ) { echo("<tr> <td><font face='Arial, Helvetica, sans-serif' size='3'><strong>" . $row["name"] . "</strong></font></td> </tr><tr> <td><font face='Arial, Helvetica, sans-serif' size='1' color='#0000FF'><strong>Members (" . $numbers . ")</strong></font></td> </tr><tr> <td><hr width=95%><br></td> </tr>"); } hi i have this function for select / function select($table, $rows = '*', $where = null, $group = null, $order = null, $limit = null) this is what i do to display the records. $db->select('loan'); $records = $db->getResult(); foreach($records as $row) { $user = $row['id']; //$name = $row['firstname']; //print_r($row); echo $user; ?> the codes is running.but when i want to use WHERE id = $_POST['id']; this is what im doing. $db->select('member')->where(array('id' => $_POST['client'])); an error as occured where in function where is not existing. please help how to use the proper way of using select * $table where id=$id; in array. thanks Hello, Im trying to write a select query that has two conditions. First the playername is = username and second shipyard = 1 I keep getting a parse error: Parse error: syntax error, unexpected ',' in C:\wamp\www\SWB\planet1.php on line 85 Heres the code: Code: [Select] $tester = "1"; $colname_Planet1 = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_Planet1 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query_Planet1 = ("SELECT * FROM planet WHERE ShipYard = %s AND PlayerName = %s", GetSQLValueString($tester, "int"),GetSQLValueString($colname_Planet1, "text")); $Planet1 = mysql_query($query_Planet1, $swb) or die(mysql_error()); $row_Planet1 = mysql_fetch_assoc($Planet1); $totalRows_Planet1 = mysql_num_rows($Planet1); Please help :| |