![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
[PHP]<?php $user_query=$_POST['name'];
$conn=odbc_connect('mysql_con', '...', '...') //connect to database odbc_exec($conn, "USE localdb"); //specify the database $sql="SELECT * FROM products"; $reader=odbc_exec($conn, $sql); //read the table. this is working ok echo '<table>'; while(odbc_fetch_row($reader)) { $name=odbc_result($reader,"name"); if (strpos($name, $user_query)) { //THIS IS ALWAYS FALSE! $price=odbc_result($reader,"price"); $description=odbc_result($reader,"description"); $ingredients=odbc_result($reader,"ingredients"); echo'<tr><td>'.$name.'</td><td>'.$price.'</td><td>' .$description.'</td><td>'.$ingredients.'</td>'; } } ?>[/PHP] look at the code above, the strpos() is supposed to compare the $name that it read from the database with the $user_query from the $_POST. I did some debuging and printed out $name and $user_query and they are fine but still, even if the strings are a match, strpos returns FALSE. What am I missing? Any help would be appreciated. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Strpos may well return a non-zero integer, which will be evaluated as false with your approach. I recommend use of the "===" test (or its converse), which will require an actual boolean type for the evaluated comparison.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Thanks for the remark but I tryed ==true with no luck either. I changed the code so that the search is done by the DBMS. Now it is working:[PHP]<?php $user_query=$_POST['name'];
$conn=odbc_connect('mysql_con', '...', '...') //connect to database odbc_exec($conn, "USE localdb"); //specify the database $sql="SELECT * FROM products WHERE LOCATE(LCASE('".$user_query."'), LCASE(products.name))"; if (!($reader=odbc_exec($conn, $sql))) exit("Problem with SQL"); echo '<table border=2><tr><th>Name</th><th>Price</th><th>Description</th>' .'<th>Ingredients</th>'; while(odbc_fetch_row($reader)) { $name=odbc_result($reader,"name"); $price=odbc_result($reader,"price"); $description=odbc_result($reader,"description"); $ingredients=odbc_result($reader,"ingredients"); echo'<tr><td>'.$name.'</td><td>'.$price.'</td><td>' .$description.'</td><td>'.$ingredients.'</td>'; }[/PHP] |
|
|
|
|
|
#4 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"==" and "===" are not the same thing. The second form requires a match of type as well as value.
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#5 | |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
This particular issue arises from the fact that PHP is less strongly typed. The use of the === and !== operators is often called for.
I like PHP, OpenLoop. If you're coming from C/C++ to PHP, you'll find a lot of methods named the same. There are subtle and not so subtle differences, so be careful writing from habit. When you look up a familiar methods in the manual, also just scan the other methods of that same type. PHP has often added even more flexible ways to accomplish the same thing.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|