Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 20th, 2005, 11:32 PM   #1
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Exclamation strpos() function not functionning!

[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.
OpenLoop is offline   Reply With Quote
Old Aug 21st, 2005, 6:29 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 21st, 2005, 9:02 AM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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]
OpenLoop is offline   Reply With Quote
Old Aug 21st, 2005, 7:21 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
"==" and "===" are not the same thing. The second form requires a match of type as well as value.

Quote:
Originally Posted by PHP Manual
Warning

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
is found here.
__________________
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
DaWei is offline   Reply With Quote
Old Aug 21st, 2005, 9:12 PM   #5
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Quote:
Originally Posted by PHP Manual
Returns the numeric position of the first occurrence of needle in the haystack string
Yup that's my problem right there. I was thinking it returns a true if found and false otherwise. So if the substring was at the beginning of the string, it will return 0 which evaluates to FALSE and that was my case.
OpenLoop is offline   Reply With Quote
Old Aug 22nd, 2005, 6:55 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC