0

I want to search for name in text but I have this error:

Warning: Invalid argument supplied for foreach()

    $users = $db->query_read(" SELECT username FROM " . TABLE_PREFIX . " user "); 
    $nameuser=array();
    while ($results = $db->fetch_array($users, MYSQLI_ASSOC)) 
    {
        $nameuser[] = $results['username'];
    }
    foreach ($nameuser as $name)
    {
        if(strstr($post['message'], $name))
        {
            // create new PM
            $pmdm =& datamanager_init('PM', $vbulletin, ERRTYPE_ARRAY);
            $pmdm->set_info('is_automated', true);
            $pmdm->set('fromuserid', 1);
            $pmdm->set('fromusername', admin);
            $pmdm->set('title', 'some title');
            $pmdm->set('message', 'some text');
            $botpermissions['adminpermissions'] = 2;
            $pmdm->set_recipients($name, $botpermissions);
            $pmdm->set('dateline', TIMENOW); 
            $pmdm->save(); 
        }
    }

Edit : use stripos Instead of strstr

6
  • 4
    The argument to foreach has to be an array. $names is a string that you just created with implode. Use $nameuser. Commented Feb 20, 2015 at 16:12
  • @Barmar thanks brother , you are right .. now no error , but the code don't do any result .. can you know why ? Commented Feb 20, 2015 at 16:22
  • try echo(count($nameuser)); right before foreach line to see if any was found Commented Feb 20, 2015 at 16:51
  • @KimAlexander thanks brother i found th sloution with use stripos Instead of strstr Commented Feb 20, 2015 at 17:04
  • good luck with your project! Commented Feb 20, 2015 at 17:07

1 Answer 1

2

Why do you:

$names = implode(',', $nameuser);
foreach ($names as $name)

just :

foreach ($nameuser as $name)
Sign up to request clarification or add additional context in comments.

6 Comments

yeah... thanks brother , you are right .. now no error , but the code don't do any result .. can you know why ?
Not this one the code which is not showing any error i mean the current code which you are saying providing no output. .
@omardealo mustn't it be: `" . TABLE_PREFIX . " user` ") with ticks? is your table name include space?? or you should use " . TABLE_PREFIX . "user") if there is no space in table name
@KimAlexander , my table name don't include space , i can use query like (" SELECT username FROM user ") There is no difference or a problem in both cases
there is a difference, in your current code you use space character after TABLE_PREFIX before user. so if TABLE_PREFIX = 'wp' real query will be SELECT username FROM wp user which is not correct
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.