簡體   English   中英

如何:將mysql_query($ show_tables)回顯到標准輸出?

[英]how to: echo mysql_query($show_tables) to standard output?

我有這個php腳本,每天都會由cronjob運行:

mysql_connect("$host", "$username", "$password")or die("Could not connect to the database: " . mysql_error()) ;
mysql_select_db("$db_name") or die("Could not select database: " . mysql_error()) ;

//create backup table with current date concatenated to the table name
$create_table = "set @c=concat('create table    rc_profile_table_backup_',date_format(now(),'%Y_%m_%d'))";
$prepare = "prepare stmt from @c";
$execute = "execute stmt";
$deallocate = "deallocate prepare stmt";

mysql_query($create_table);
mysql_query($prepare);
mysql_query($execute);
mysql_query($deallocate);

echo $sql11 = "insert rc_profile_table_backup_".mktime(date("y"),date("m"),date("d"))."select * from   rc_profile_table"."\n";

我怎樣才能實現上面的插入? 我在cronjob中運行此腳本,因此它必須選擇具有當前日期的表名稱並進行插入。 我怎么知道當前日期?

謝謝

用PHP編寫stdout

您可以使用流包裝器php://stdout或預定義的STDOUT (這是首選方式):

fwrite(STDOUT, "My text to write to stdout");

參考: http : //php.net/manual/en/wrappers.php.php

將代碼的一些邏輯移至php

您的php看起來像這樣:

$tableName = "rc_profile_table_backup_" . date("Y_m_d");
mysql_query("CREATE TABLE `{$tableName}` ..."); // insert your schema here
mysql_query("INSERT INTO `{$tableName}` SELECT * FROM `rc_profile_table`");

您的代碼的其他問題

不要使用不必要的字符串插入:

("$db_name")

可以寫成

($db_name)

准備,執行和解除分配的整個過程應使用適當的mysql類(mysqli,PDO)進行,而不應使用您自己的代碼。 他們為您完成所有這些工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM