|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#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. |
|
|
|
|
|
#2 |
|
Come in Ray...
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.
|
|
|
|
|
|
#3 | |
|
Staff
Premium Member
Join Date: Jul 1999
Location: Arlington, TN
Posts: 5,538
|
Quote:
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 |
|
|
|
|
|
|
#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. |
|
|
|
|
|
#6 |
|
Staff
Premium Member
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;
Last edited by mairving; 05-01-2006 at 01:08 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! |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|