MySQL Forums
Forum List  »  PHP

am I using massive resource for sending mySQL qurey through PHP?
Posted by: Yousef Al-Hadhrami
Date: December 19, 2014 03:27PM

Hello dear mySQL users
I have been using mySQL for only ~2 weeks so I am a noob :D, please be gentle on me as this is my first post too.

what I want to ask about is that I have wrote 2 functions that will auto arrange a unique number field in a column
ofcourse global $current_page; is an array that used mysql to read whole row
and global $connection; is the connection string

<?php
function rearrange_position($modify_position_to){
global $current_page;
global $connection;
$current_page_id = $current_page['id'];
$current_page_position = $current_page['nav_position'];

if($current_page_position > $modify_position_to){
while($current_page_position > $modify_position_to){
$query = "UPDATE pages SET ";
$query .= "nav_position = '{$current_page_position}' ";
$current_page_position--;
$query .= "WHERE nav_position = '{$current_page_position}' ";
mysqli_query($connection, $query);
}
} elseif ($current_page_position < $modify_position_to){

while($current_page_position < $modify_position_to){
$query = "UPDATE pages SET ";
$query .= "nav_position = '{$current_page_position}' ";
$current_page_position++;
$query .= "WHERE nav_position = '{$current_page_position}' ";
mysqli_query($connection, $query);
}
}
}
?>
thats the function and this is the implementation
<?php
// Perform Update

$id = $current_page["id"];
$page_name = mysql_prep($_POST["page_name"]);
$nav_position = (int) $_POST["nav_position"];
$visible = (int) $_POST["visible"];

// reorder function start
rearrange_position($nav_position);
// reorder function end

$query = "UPDATE pages SET ";
$query .= "page_name = '{$page_name}', ";
$query .= "nav_position = {$nav_position}, ";
$query .= "visible = {$visible} ";
$query .= "WHERE id = {$id} ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if($result)....// you know the remaining, if query sucess
show a message , else echo query failed
?>

and I have another function for arranging that deals when inserting a new row and when deleting a row..

the question is when each time I change a row I use a new query and it's limited to how many rows in the table
I was thinking of saving all of desired rows id's to be changed then change each ones nav_position(the unique column) using an array and only type one long mySQL query

or the current one I am using is fine?
thank you, please if you are not sure what to replay, just post your function for arranging unique columns. :)

// pss is there a way to make my code appear more readable in the forum?

Options: ReplyQuote


Subject
Written By
Posted
am I using massive resource for sending mySQL qurey through PHP?
December 19, 2014 03:27PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.