Page 1 of 1

[SOLVED] strpos() not finding string

Posted: Mon Jul 31, 2006 4:33 am
by hairyjim
I have the below code:

Code: Select all

if (strpos($data[CallType],"VOLOCITY")) { $sql_insert_reporting ="INSERT INTO hs_request_reportingtags (xRequest,xReportingTag) VALUES ($data[No],30)";
	$ins_result_reporting = mysql_query( $sql_insert_reporting, $dbconn );
	if (!$ins_result_reporting)
	{echo mysql_error();} }
This is basically searching a string to see if VOLOCITY is in the string if it does then insert a record in a DB.

So when my script runs $data[CallType] contains VOLOCITY INSTALL.

When it gets to the IF statement it equates to false and no record is inserted but I know for the one test record I have it should equate to true and insert a record.

If I change the value of CallType so there is a space before VOLOCITY the IF statement runs? Am I completly missing how STRPOS works and should I be using somehting else?

This is quite urgent, this minor problem is holding up a big data import I am trying to get done before some staff get in.

Please help.

Regards
Jim

Posted: Mon Jul 31, 2006 4:54 am
by MarK (CZ)
RTM.
Manual wrote: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.

Code: Select all

if (strpos($data[CallType],"VOLOCITY") !== FALSE) {
  $sql_insert_reporting ="INSERT INTO hs_request_reportingtags (xRequest,xReportingTag) ".
                         "VALUES ($data[No],30)";
  $ins_result_reporting = mysql_query( $sql_insert_reporting, $dbconn );
  if (!$ins_result_reporting) {
    echo mysql_error();
  }
}

Posted: Mon Jul 31, 2006 4:56 am
by ronverdonk

Code: Select all

Are you sure it is 
$data[CallType]  
and not 
$data['CallType'] ?

Posted: Mon Jul 31, 2006 4:59 am
by JayBird
The problem is, the first occurece of the string starts at index 0...which also equates to FALSE so you if statement will not execute.

This should do it

Code: Select all

if (strpos($data[CallType],"VOLOCITY" !== FALSE)) {

Posted: Mon Jul 31, 2006 5:19 am
by hairyjim
Cheers for the replies.

I RTM but it seems I did not engage brain when putting in the code.

Thanks Pimptastic you code sorted me out.

Jim