MYSQL search functions
- untitled
-
untitled
- Member since: Jul. 17, 2000
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
This is bugging me, i've been trying to figure what function in MYSQL will return true or false if it finds a certain word in a string. I've been trying to use MATCH...AGAINST and LIKE but it's not working for me here's a simple script that isn't working for me.
<?
dbconnect();
$test=mysql_query("SELECT test FROM test (test LIKE '%$var%')");
if($test){echo"$var was found!";}else{echo"$var was not found!";}
?>
Whats wrong with this?!
- liljim
-
liljim
- Member since: Dec. 16, 1999
- Offline.
-
- Send Private Message
- Browse All Posts (11,644)
- Block
-
- Forum Stats
- Staff
- Level 28
- Blank Slate
At 9/18/01 08:41 PM, untitled wrote: This is bugging me, i've been trying to figure what function in MYSQL will return true or false if it finds a certain word in a string. I've been trying to use MATCH...AGAINST and LIKE but it's not working for me here's a simple script that isn't working for me.
<?
dbconnect();
$test=mysql_query("SELECT test FROM test (test LIKE '%$var%')");
if($test){echo"$var was found!";}else{echo"$var was not found!";}
?>
Try mysql_num_rows() and amend your query to something like this:
<?
$query = @mysql_query("SELECT column1, column2 FROM table WHERE column1 LIKE '$somestring'", $connection)
or die ("Some error.");
if (mysql_num_rows($query) == 0) {
echo "Nothing found.";
} else {
echo "Bingo.";
}
?>
You will want to iterate the results from the search when something's found with mysql_fetch_array() or mysql_fetch_row() or mysql_fetch_object() or another related function.
Let me know how you get on.
Ps. Have a look for "Full text search" in php.net - I remember someone mentioning that somewhere.
- untitled
-
untitled
- Member since: Jul. 17, 2000
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
At 9/19/01 05:37 AM, liljim wrote:
Let me know how you get on.
Ps. Have a look for "Full text search" in php.net - I remember someone mentioning that somewhere.
This worked for me:
$query=mysql_fetch_row(mysql_query("SELECT text FROM test WHERE (text LIKE '%$var%')"));
if($query){
echo"$var was found!";
}else{
echo"$var was not found!";
}
when i tried with mysql_affected_rows i got an invalid link resource error and it will only work if i do mysql_fetch_row or array on the query which i don't understand, because i should only need mysql_query.
- liljim
-
liljim
- Member since: Dec. 16, 1999
- Offline.
-
- Send Private Message
- Browse All Posts (11,644)
- Block
-
- Forum Stats
- Staff
- Level 28
- Blank Slate
No, you're going about things half ass here. The $query is returning true, because it has been able to return true to the fact that it's:
1/. fetched a row (which may or may not exist).
2/. Performed a successful query (i.e. the connection to the db and mysql has been esablished and the table has been searched).
It doesn't mean that the query returned any results - it just means that the query was executed.
Try this:
<?
$var = "mon";
$query = @mysql_query("SELECT text FROM test WHERE text LIKE '%$var%'", /* connection and db details */);
if (!$query) {
die("Db and connection to sql failed.");
}
if (mysql_num_rows($query) == 0) {
echo "No results returned.";
} else {
while($row = mysql_fetch_array($query)) {
$thevar = $row['text'];
echo $thevar . "<br>";
}
?>
Matches will be made against "monkey", "money", "Mona", "monarch", or anything with "mon" in the column text....
Note, these are simple work arounds. The first I wrote when I was in a meeting, and now I'm about to sleep - so there may very well be some errors in there somewhere.
- NightRaid
-
NightRaid
- Member since: Sep. 15, 2001
- Offline.
-
- Forum Stats
- Member
- Level 12
- Blank Slate
dbconnect();
$test=mysql_query("SELECT test FROM test (test LIKE '%$var%')");
Whats wrong with this?!
First of all, you have the syntax wrong.. how long have you been programming PHP? To connect to a MySQL database, you must do the folowing (values in parentheses you will have to modify yourself):
// Assign login values
$host = (The machine hosting MySQL)
$db = (Name of the database you are accessing)
$user = (username to log in with)
$pass = (password, if any, that is required)
// Connect to the database
$link = $mysql_connect( $host, $user, $pass );
// Send a query
$res = mysql_db_query( $db, "INSERT query HERE", $link );
- liljim
-
liljim
- Member since: Dec. 16, 1999
- Offline.
-
- Send Private Message
- Browse All Posts (11,644)
- Block
-
- Forum Stats
- Staff
- Level 28
- Blank Slate
At 9/21/01 02:23 PM, NightRaid wrote:dbconnect();First of all, you have the syntax wrong.. how long have you been programming PHP? To connect to a MySQL database, you must do the folowing (values in parentheses you will have to modify yourself):
$test=mysql_query("SELECT test FROM test (test LIKE '%$var%')");
Whats wrong with this?!
How long have you been programming PHP? His connection details probably reside in the function he's written as dbconnect();. Assuming that the function is working as expected, there is nothing with the way he is connecting to his database. In fact, it's a more elegant way of handling things than your method.
- untitled
-
untitled
- Member since: Jul. 17, 2000
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
At 9/20/01 07:14 PM, liljim wrote:
It doesn't mean that the query returned any results - it just means that the query was executed.
Woah that something i should have known, no wonder i've been having lots of little problems. thanks for the help.

