PHP + PDO - Inserting data on the table with a loop
up vote
1
down vote
favorite
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.
performance beginner php sql pdo
New contributor
add a comment |
up vote
1
down vote
favorite
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.
performance beginner php sql pdo
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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.
performance beginner php sql pdo
New contributor
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.
performance beginner php sql pdo
performance beginner php sql pdo
New contributor
New contributor
edited 19 mins ago
New contributor
asked 11 hours ago
515948453225
113
113
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
515948453225 is a new contributor. Be nice, and check out our Code of Conduct.
515948453225 is a new contributor. Be nice, and check out our Code of Conduct.
515948453225 is a new contributor. Be nice, and check out our Code of Conduct.
515948453225 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208829%2fphp-pdo-inserting-data-on-the-table-with-a-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown