First of all i made a simple code to insert data on my table, with the information from this code i made a loop to insert data on another table for a notification system.
I'm new to PHP and i think that this query that is being executed into a loop is really poor in performance, example: if i have like 100K users, this query will be executed 100K times... But the query will be executed 100K times at same time or is it going to run one after the other? I don't think so, because it's a loop, right? But i still think that my loop is poor in performance, i want to know how much and how can i improve it.
Anyways, what you guys think about my code?
More details is on the comments into the code bellow:
$slug = '';
if(isset($_POST["create"])){
$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_POST["itemName"])));
$query = "SELECT `slug_url` FROM `table_tudo` WHERE `slug_url` LIKE ?";
$statement = $conn->prepare($query);
$statement->execute(["$slug%"]);
$total_row = $statement->rowCount();
$result = $statement->fetchAll();
$data = $statement->fetchAll(PDO::FETCH_COLUMN);
if(in_array($slug, $data)){
$count = 0;
while( in_array( ($slug . '-' . ++$count ), $data) );
$slug = $slug . '-' . $count;
}
$insert_data = array(
':title' => $_POST['itemName'],
':epTitle' => $_POST['epTitle'],
':itemName' => $_POST['itemName'],
':data' => $_POST['data'],
':datePublished' => $_POST['datePublished'],
':dateModified' => $_POST['datePublished'],
':descricao' => $_POST['itemName'].' - Tipo '.$_POST['epNum'].' - '.$_POST['descricao'],
':epCapa' => $_POST['epCapa'],
':alt' => $_POST['itemName'].' - Tipo '.$_POST['epNum'].' - '.$_POST['epTitle'],
':audio' => $_POST['audio'],
':qualidade' => $_POST['qualidade'],
':epNum' => $_POST['epNum'],
':tipo' => 'ep',
':keywords' => $_POST['keywords'],
':slugForitemPage' => $slug,
':player_SD' => $_POST['player_SD'],
':player_HD' => $_POST['player_HD'],
':slug_url' => "https://example.com/index.php?page=".$slug.'&ep='.$_POST['epNum'],
':slug_link' => $_POST['slug_link'],
':entry_type' => 'item'
);
$query = "INSERT INTO table_tudo (title, epTitle, itemName, data, dataFL, datePublished, dateModified, descricao, epCapa, alt, audio, qualidade, epNum, tipo, keywords, slugForitemPage, player_SD, player_HD, slug_url, slug_link, entry_type) VALUES (:title, :epTitle, :itemName, :data, NOW(), :datePublished, :dateModified, :descricao, :epCapa, :alt, :audio, :qualidade, :epNum, :tipo, :keywords, :slugForitemPage, :player_SD, :player_HD, :slug_url, :slug_link, :entry_type)";
$statement = $conn->prepare($query);
$statement->execute($insert_data);
// this is used on the INSERT query bellow
$epNum = $_POST['epNum'];
$itemName = $_POST['itemName'];
$status = 'unread';
//
// here i made a query to get all user_id where itemName is = to $_POST['itemName'];, this information
// is on a table called `seguiritem`, i will know it by the $_POST['itemName'], that is get when i execute the INSERT query above
$checkUserFLW = $conn->prepare("SELECT `user_id`, `itemName` FROM seguiritem WHERE itemName = :itemName");
$checkUserFLW->bindParam(':itemName', $itemName, PDO::PARAM_STR);
$checkUserFLW->execute();
$resultRow = $checkUserFLW->rowCount();
// if the name of the item is on the table, this will execute the code inside this condition
if($resultRow = 1){
// i have a table called `noti`, on this table i store the (user_id, epNum, itemName, status), this is
// a table that i retrive data to a notification system(i think it's not relevant to my question, but
// if you think so, place a comment bellow), on the above query i selected all the `user_id` that is
// on the table `siguiritem`, with this information i made a loop
foreach($checkUserFLW as $ckUsFLW){
//for each user_id that have the column `itemName` equal to
// $_POST['itemName'] on the table `seguiritem`, will execute this
// query to insert on the table `noti` the `user_id`, `itemName`,
// `epNum`(this is like a identifier), and `status` that will
// be = `unread`.
$user_id = $ckUsFLW['user_id'];
$queryISNOTI = $conn->prepare("INSERT INTO noti (user_id, epNum, itemName, status) VALUES (:user_id, :epNum, :itemName, :status)");
$queryISNOTI->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$queryISNOTI->bindParam(':epNum', $epNum, PDO::PARAM_INT);
$queryISNOTI->bindParam(':itemName', $itemName, PDO::PARAM_STR);
$queryISNOTI->bindParam(':status', $status, PDO::PARAM_STR);
$queryISNOTI->execute();
}
}
}
I'm not sure if i explained it well, please any doubts just ask me.
if($resultRow = 1){
condition is either wrong and useless even if fixed. you can take it out \$\endgroup\$