جایگزین کردن یک متن در کل جدولهای دیتابیس با PHP
در این نوشته به یک اسکریپت آماده سازی کردیم تا به سادگی به دیتابیس خود وصل شوید و بدون مشخص کردن نام جدولها به طور کامل و خودکار متنی را در کل دیتابیس خود تغییر دهید.
جایگزین کردن یک متن در کل جدولهای دیتابیس با PHP
برای جایگزین کردن متن در کل جدولهای دیتابیس mysql از قطعه کد زیر استفاده کنید:
با PDO
<?php
// Database connection credentials
$host = 'localhost';
$dbname = 'your_database_name'; // Change this to your database name
$user = 'your_username'; // Change this to your username
$password = 'your_password'; // Change this to your password
try {
// Create a new PDO connection
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Get all table names in the database
$tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
// Iterate through each table
foreach ($tables as $table) {
// Get columns of the table
$columns = $pdo->query("SHOW COLUMNS FROM $table")->fetchAll(PDO::FETCH_COLUMN);
// Iterate through each column to update the values
foreach ($columns as $column) {
$updateQuery = "UPDATE `$table` SET `$column` = REPLACE(`$column`, 'متنی که میخواهید جایگزین شود', 'متنی که باید جایگزین شود') WHERE `$column` LIKE '%متنی که میخواهید جایگزین شود%'";
try {
$pdo->exec($updateQuery);
echo "Updated $table.$column successfully.\n";
} catch (PDOException $e) {
// Catch any errors (e.g., if the column data type is not suitable for REPLACE)
echo "Failed to update $table.$column: " . $e->getMessage() . "\n";
}
}
}
echo "Database update complete.\n";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
با MYSQLI
<?php
// Database connection credentials
$host = 'localhost';
$dbname = 'your_database_name'; // Change this to your database name
$user = 'your_username'; // Change this to your username
$password = 'your_password'; // Change this to your password
// Create a new mysqli connection
$mysqli = new mysqli($host, $user, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Get all table names in the database
$tablesResult = $mysqli->query("SHOW TABLES");
if ($tablesResult) {
// Iterate through each table
while ($tableRow = $tablesResult->fetch_array()) {
$table = $tableRow[0];
// Get columns of the table
$columnsResult = $mysqli->query("SHOW COLUMNS FROM `$table`");
if ($columnsResult) {
// Iterate through each column to update the values
while ($columnRow = $columnsResult->fetch_assoc()) {
$column = $columnRow['Field'];
$updateQuery = "UPDATE `$table` SET `$column` = REPLACE(`$column`, 'متنی که میخواهید جایگزین شود', 'متن جایگزین') WHERE `$column` LIKE '%متنی که میخواهید جایگزین شود%'";
if ($mysqli->query($updateQuery) === TRUE) {
echo "Updated $table.$column successfully.\n";
} else {
echo "Failed to update $table.$column: " . $mysqli->error . "\n";
}
}
} else {
echo "Failed to get columns for table $table: " . $mysqli->error . "\n";
}
}
} else {
echo "Failed to get tables: " . $mysqli->error . "\n";
}
// Close the connection
$mysqli->close();
echo "Database update complete.\n";
?>
اگر نیاز به آموزشهای PHP دارید دارید در بخش نظرات این نوشته برای ما بنویسید.
برای امتیاز به این نوشته کلیک کنید!
[کل: 1 میانگین: 5]



















این اسکریپت روی دیتابیس ریموت هم بدون مشکل جواب میده؟
خیر تفاوتی ندارد. فقط حتما قبل از انجام این کار از دیتابیس خود بکاپ تهیه کنید.
امکان لاگ گرفتن از تغییرات قبل و بعد وجود داره؟
قبل از انجام این کار از دیتابیس خود بکاپ تهیه کنید.
برای بکاپ گرفتن قبل از اجرا چه روشی پیشنهاد می کنید؟
بستگی به محیط شما دارد از بخش backup هاست خود میتوانید استفاده کنید یا اینکه از sqldump یا export دیتابیس استفاده کنید.
اگه یکی از ستونها نوعش INT باشه، این کوئری خطا نمیده؟
بله تفاوتی ندارد.
این اسکریپت روی دیتابیسهای خیلی بزرگ هم جواب میده یا کند میشه؟
تفاوتی ندارد.