[SOLVED] strpos() not finding string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

[SOLVED] strpos() not finding string

Post 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
Last edited by hairyjim on Mon Jul 31, 2006 5:20 am, edited 1 time in total.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post 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();
  }
}
Last edited by MarK (CZ) on Mon Jul 31, 2006 4:58 am, edited 2 times in total.
User avatar
ronverdonk
Forum Commoner
Posts: 34
Joined: Sat Jun 10, 2006 7:06 am
Location: Netherlands

Post by ronverdonk »

Code: Select all

Are you sure it is 
$data[CallType]  
and not 
$data['CallType'] ?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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)) {
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post 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
Post Reply