Question

I am attempting to set up a reply notification application using php. When a site visitor fills out the reply form on the website and hash-tags other participants in the discussion (#Mike, #Wale ), the application extracts the hash-tags and uses the preg_match_all() function to process and ultimately extract the usernames from the hash-tags and then, this is stored in an array.

Based on the values in the username array, the application is supposed to iterate through the users table in the database, extracting email addresses that match the username and thereafter each user is sent a reply notification.

The part of the application that should execute the select query is throwing up an error, which i have highlighted below: Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\wamp\www\lost_and_found\send_reply_notification.php on line 33.

Take a look at my code below:

 <?php
 ///////////////////extract matches into array//////////////////////////
 $data = "#MIKE The party will start at 10:30 pm and #John1975 run untill 12:30 am. Please #Emeka, could you inform #Joseph?";
 preg_match_all('/#[a-z0-9]+/i', $data, $match, PREG_PATTERN_ORDER);
$usernames=$match[0] ;
 /////////////convert array into a string using implode///////////////////
$newlist = implode( ",", $usernames );

$count = count($newlist);
/////////////////store username into array///////////
for ($i = 0; $i < $count; $i++) {

 preg_match_all('/#[a-z0-9]+/i', $newlist, $match, PREG_PATTERN_ORDER);
 $full_name=$match[0];}

////////////////////////Extract usernames/email pair from database////////////////
for ($i = 0; $i < $count; $i++)
    //dabase connection script
    $sql = 'SELECT * FROM users where username='. $full_name[$i];
    // submit the query and capture the result
    $result = $conn->query($sql) or die(mysqli_error());
    if ($result) 
        {
            //send mail query here.
        }   
?> 

It appears the WHERE clause in the SELECT query is not accepting the '$full_name[$i]' as a value input.

How may I resolve this, so as to iterate through the users table using the values in the array in the WHERE clause?

Thanks.

Was it helpful?

Solution

Add quotes for name in the query like

$sql = 'SELECT * FROM users where username="'. $full_name[$i].'"';

and also don't forgot to put the code inside { } in the for loop.

for ($i = 0; $i < $count; $i++)
{
    //dabase connection script
    $sql = 'SELECT * FROM users where username="'. $full_name[$i].'"';
    // submit the query and capture the result
    $result = $conn->query($sql) or die(mysqli_error());
    if ($result) 
        {
            //send mail query here.
        } 
}

OTHER TIPS

no need to loop just implode the array and use WHERE in query

$sql = 'SELECT * FROM users where username IN ("'. implode(",",$full_name).'"';

$full_name = array();
for ($i = 0; $i < $count; $i++) {

     preg_match_all('/[a-z]+/i', $newlist, $match, PREG_PATTERN_ORDER);
     $full_name=$match[0];
 }

 $count = count($full_name);

you should define $full_name outside the loop and recreate $count value again

also change query to

$sql = 'SELECT * FROM users where username="'. $full_name[$i].'"';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top