Jump to content

Shortening all these queries?


MySQL_Narb

Recommended Posts

Is there a more efficient way of doing this?

 

$database->processQuery("UPDATE `users` SET `username` = ? WHERE `username` = ? LIMIT 1", array($_POST['new'], $_POST['old']), false);
$database->processQuery("UPDATE `posts` SET `username` = ? WHERE `username` = ? LIMIT 1", array($_POST['new'], $_POST['old']), false);
$database->processQuery("UPDATE `threads` SET `username` = ? WHERE `username` = ? LIMIT 1", array($_POST['new'], $_POST['old']), false);
$database->processQuery("UPDATE `messages` SET `creator` = ? WHERE `creator` = ? LIMIT 1", array($_POST['new'], $_POST['old']), false);
$database->processQuery("UPDATE `replies` SET `username` = ? WHERE `username` = ? LIMIT 1", array($_POST['new'], $_POST['old']), false);

Link to comment
https://forums.phpfreaks.com/topic/264860-shortening-all-these-queries/
Share on other sites

In a second thought...

 

what exactly the column 'username' (and creator) represent in each table?... is that a literal like 'johndoe'?  ... if that is the case, seems to me that you have a design problem with no normalized tables.

 

In a normalized model you should store in all those tables just a FK (foreign key) pointing to a record in other table holding that key and the username, therefore if the username need to be changed will be neccesary to update just one table... in your case, the users table should be the only one holding the username and the others the PK of the users table.

 

You users table should look like:

id        INT    (PK)

username VARCHAR

... others fields

 

and per example your table posts

id    INT  (PK)

userid  INT (FK to the users PK)

... others fields 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.