Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 04-30-2006, 03:18 AM   #1
Member (8 bit)
 
Join Date: Apr 2004
Location: Phoenix
Posts: 223
MySQL pattern search using PHP

I'm trying to set up a script on my server that does certain things with the locally-installed MySQL database ( > ver 4.0), in this case I have a table that consists of 3 columns (one called word). I'm trying to run a pattern check on all items in the "word" column. However, every time I run this code all I get is a narrow line on the screen (if only one or two items are found). It should also be returning actual text but it doesn't. I downloaded the MySQL manual so I know the syntax is correct, but it's just not doing it. Any ideas? Something to note is that the table is definitely not empty; I have many entries.

The code I'm using is the following (on a Windows 2000 Server machine):

mysql_connect("192.168.0.100", "root", "myPassword") or die(mysql_error());

mysql_select_db("vocab") or die(mysql_error());

$query = "SELECT * FROM entries WHERE word LIKE '%$var%';";
$result = mysql_db_query("vocab",$query);
if(!$result) { echo "Not in the database or database error.<p>" . mysql_error(); }
else
{
$count = 1;
while ($r = mysql_fetch_array($result))
{
if($count%2 > 0)
{
echo '<TABLE border=0 bgcolor="#d4cbcb" width=100%><tr bgcolor="#d4cbcb"><td width=5%>';
++$count;
}
else
{
echo '<TABLE border=0 width=100%><tr width=100% bgcolor="#ffffff"><td width=5% bgcolor="#ffffff">';
++$count;
}

echo($row["id"] . '</td>');
echo('<td width=15%>' . $row["word"] . '</td>');
echo('<td width=80%>' . $row["definition"] . '</td></tr></table>');
}
}
__________________
"Advancement is answering the questions, discovery is questioning the answers."

Last edited by dataDude; 04-30-2006 at 03:22 AM.
dataDude is offline   Reply With Quote
Old 05-01-2006, 09:57 AM   #2
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
The Like operator only works with Char and Varchar types. Make sure your "word" column is not set to TEXT.
faulkner132 is offline   Reply With Quote
Old 05-01-2006, 11:26 AM   #3
Staff
Premium Member
 
mairving's Avatar
 
Join Date: Jul 1999
Location: Arlington, TN
Posts: 5,538
Quote:
Originally Posted by faulkner132
The Like operator only works with Char and Varchar types. Make sure your "word" column is not set to TEXT.
Shouldn't be the problem in this case. You can add FULLTEXT search to the field if necessary.

What I would do is:
(1.) First do a View/Source on the page. Oftentimes I have figured out the error by what was displaying or not displayed.
(2.) Remove your conditional statements and see what the results are.
__________________

Want to Make $$$$ with your Computer? No Risk! Simply press shift-4 four times in a row
mairving is offline   Reply With Quote
Old 05-01-2006, 11:37 AM   #4
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
Quote:
Originally Posted by mairving
You can add FULLTEXT search to the field if necessary.
Good call... forgot about that keyword.
Just because you don't use it, doesn't mean it is not there.
faulkner132 is offline   Reply With Quote
Old 05-01-2006, 12:41 PM   #5
Member (8 bit)
 
Join Date: Apr 2004
Location: Phoenix
Posts: 223
Since your replies, I've created a new table called 'entries2', copied over the data from entries, deleted 'entries', then recreated 'entries' with the updated VARCHAR data type (I did have them originally as TEXT) and finally copied over the data from 'entries2'. Still not working. I put in another line in PHP that says "echo mysql_numrows($result);" and it successfully provides a correct number (it was always successful in doing so, even before the table creation/delete), but it still won't output the contents. I had also tried the full text option, but I received an error saying "The used table type doesn't support FULLTEXT indexes." What's that about? It doesn't work with VARCHAR or something?

Another thing to note is that I went to the cmd console and applied the same command the mysql prompt and it produced the desired results. Is there some sort of incompatibility between PHP and MySQL? Did any of you try recreating the situation? If so, were you successful?

I've been successful with pushing the "SELECT * FROM"-type and "INSERT INTO" and "DELETE FROM" commands in PHP, just not "WHERE word LIKE '%$var%'" comparisons. I'm confused.
dataDude is offline   Reply With Quote
Old 05-01-2006, 01:05 PM   #6
Staff
Premium Member
 
mairving's Avatar
 
Join Date: Jul 1999
Location: Arlington, TN
Posts: 5,538
Try this:

Code:
mysql_connect("192.168.0.100", "root", "myPassword") or die(mysql_error());

mysql_select_db("vocab") or die(mysql_error());
echo '<TABLE border=1 width=100%>'\n;
$query = "SELECT * FROM entries WHERE word LIKE '%$var%';";
$result = mysql_db_query("vocab",$query);

while ($r = mysql_fetch_array($result))
{

echo '<tr>'\n;
echo('<td>' . $row["id"] . '</td>')\n;
echo('<td width=15%>' . $row["word"] . '</td>')\n;
echo('<td width=80%>' . $row["definition"]')\n;
echo '</tr>'\n;
}
echo '</table>'\n;
See if this yields any results. I haven't checked the syntax of it though. Also I usually prefer only showing a row for each result rather than a whole table. It just makes it much more simple. I also added a line break for easier readability of the source.

Last edited by mairving; 05-01-2006 at 01:08 PM.
mairving is offline   Reply With Quote
Old 05-01-2006, 01:56 PM   #7
Member (8 bit)
 
Join Date: Apr 2004
Location: Phoenix
Posts: 223
I found the answer and I'm smacking my forehead about it. It's so obvious! If you'll notice I said:

while ($r = mysql_fetch_array($result))

but when outputting the data I said $row["id/word/definition"]. LOL. I changed the 'r' to 'row' just for kicks and when it worked it dawned on me. Sorry to put you all through that. At least I learned some new stuff. Thanks all for the help!
dataDude is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 05:08 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2