douzhi3776 2012-02-22 10:17
浏览 100
已采纳

如何:echo mysql_query($ show_tables)到标准输出?

i have this php script that gets run by a cronjob everyday:

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"."
";

how can i achieve the insert above? I run this script in a cronjob so it must pick up the the table name with the current date and do the insert. how am i going to know the current date?

thanks

  • 写回答

1条回答 默认 最新

  • dongwu4834 2012-02-22 10:47
    关注

    Writing to stdout in PHP

    You can use the stream wrapper php://stdout or the predefined STDOUT (which is the prefered way):

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

    Reference: http://php.net/manual/en/wrappers.php.php

    Moving some logic of your code to php

    Your php would look something like this:

    $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`");
    

    Other issues with your code

    Don't use unnecessary string inserts:

    ("$db_name")
    

    can be written as

    ($db_name)
    

    The whole process of preparing, executing and deallocating should be made with the appropriate mysql classes (mysqli, PDO) and not in your own code. They do all this work for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大