0

I am trying to get a number of rows providing the user id's in $admin_ID are not present.

$admin_ID = "1, 32, 34";
foreach($con->query('SELECT user_agent FROM wp_access_log WHERE user_id NOT IN ($admin_ID)') as $row2) {  
    $user_agent2 = $row2['user_agent'];
    $browser2 = new BrowserDetection();
    $browser2->setUserAgent($user_agent2);
    $browserName2  = $browser2->getPlatform();                //string
    $browserNameString2[] = $browserName2;
}

$string = var_export(array_count_values($browserNameString2), true);

echo "<table id='datatable-buttons' class='table table-striped table-bordered dt-responsive' cellspacing='0' width='100%'>";
    echo "<tr><td><b>Browser</b></td><td><b>Visits</b></td></tr>";
    $string = str_replace("array (","",$string);
    $string = str_replace("' => ","</td><td>",$string);
    $string = str_replace(",","</td></tr>",$string);
    $string = str_replace(")","",$string);
    $string = str_replace("'","<tr><td>",$string);
$string = $string . "</table>";
echo $string;

I get the following error:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\account-stats.php on line 543

Notice: Undefined variable: browserNameString2 in C:\xampp\htdocs\account-stats.php on line 553

Warning: array_count_values() expects parameter 1 to be array, null given in C:\xampp\htdocs\account-stats.php on line 553 NULL

If I remove the code:

 WHERE user_id NOT IN ($admin_ID)

Then the code runs fine and loads all records from the database.

Can I get a pointer as to what I have done wrong here?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sandy
  • 312
  • 6
  • 20

1 Answers1

3

PHP variable interpolation works only if interpolated string is double quotes. So PHP doesn't substitutes $admin_ID value in your SQL query which is placed between sigle quotes, and you get an incorrect query. Place your query in double quotes:

"SELECT user_agent FROM wp_access_log WHERE user_id NOT IN ($admin_ID)"

SergeyLebedev
  • 3,673
  • 15
  • 29
  • Ah thanks for explaining that, your right changed to double quotes and it works perfectly now. – Sandy Jan 08 '17 at 18:58