[第九週] PHP 與 MySQL 的互動:新增資料


  • sprintf( $format , $args1 , $args2 , $args3...) 把格式化的字符串寫寫入一個變量中。
    $format 是原始格式化轉換字串,後方接著一連串的 $args 是要插入 $format 字串的字符參數,每個字符參數將依序插入到原始格式化轉換字串($format)中的百分號(%)符號處,並完成格式化,例如 $args1 插入到 $format 的第 1 個 % 位置、$args2 插入到第 2 個 % 位置、$args3 插入到第 3 個 % 位置 ... 以此類推
    // $format: insert into users(id, username) values(%d, '%s')
    // $args1: 13,插入 %d 處
    // $args2: $username, 插入 %s 處
    // 輸出:insert into users(id, username) values(13, 'Tom')
    $username = 'Tom';
    $sql = sprintf(
      "insert into users(id, username) values(%d, '%s')",
      13,
      $username
    );
    echo $sql;
    






你可能感興趣的文章

Option API#監聽:Watch

Option API#監聽:Watch

Day 67

Day 67

Python Web Flask 實戰開發教學 - SQLAlchemy 與 ORM

Python Web Flask 實戰開發教學 - SQLAlchemy 與 ORM






留言討論