質問するログイン新規登録

質問編集履歴

2

修正

2017/04/13 08:52

投稿

coko1
coko1

スコア276

title CHANGED
File without changes
body CHANGED
@@ -1,60 +1,7 @@
1
1
  現在、mysqliを使用してデータの登録、更新の処理を行っているのですが、int型のカラムにnullを登録しようとしても失敗してしまいます。整数での登録、更新は以下のソースで問題なく行えているのですが、どのようにしたらnullを設定できるのでしょうか?
2
2
 
3
- ```php
4
- function update($sql, $params, &$count=0){
5
- $mysqli = get_connection();
6
- if(is_null($mysqli)){
7
- return false;
8
- }
9
- if(!is_array($params))$params = array();
10
-
11
- mysqli_autocommit($mysqli, TRUE);
12
-
13
- if(count($params)>0){
14
-
15
- //パラメータが1つ以上存在する場合
16
- $stmt = mysqli_stmt_init($mysqli);
17
- if(!mysqli_stmt_prepare($stmt, $sql)){
18
- //SQLステートメント実行準備失敗
19
- return false;
20
- }
21
- $stmt_params = array($stmt, '');
22
- foreach($params as $idx => $value){
23
- if(is_int($value))$stmt_params[1] .= 'i';
24
- else if(is_float($value))$stmt_params[1] .= 'd';
25
- else if(is_string($value))$stmt_params[1] .= 's';
26
- $stmt_params[] = &$params[$idx];
27
- }
28
- call_user_func_array( "mysqli_stmt_bind_param" ,$stmt_params );
29
- if(!mysqli_stmt_execute($stmt)){
30
- //SQLステートメント実行失敗
31
- return false;
32
- }
33
-
34
- //$result = mysqli_stmt_get_result($stmt);
35
- }else{
36
- //上記以外の場合
37
- //$result = mysqli_query($mysqli, $sql, MYSQLI_STORE_RESULT );
38
- mysqli_query($mysqli, $sql, MYSQLI_STORE_RESULT );
39
- }
40
- /*
41
- //トランザクションをコミット
42
- if (!mysqli_commit($mysqli)) {
43
- //コミット失敗
44
- return false;
45
- }*/
46
3
 
47
- //更新件数取得
48
- $count = mysqli_affected_rows($mysqli);
49
-
50
- mysqli_free_result($result);
51
- mysqli_close($mysqli);
52
-
53
- return true;
54
- }
55
4
  ```
56
-
57
- ```
58
5
  //$params内容
59
6
  array(
60
7
  null //test_no1 整数の場合問題なく動く

1

修正

2017/04/13 08:52

投稿

coko1
coko1

スコア276

title CHANGED
File without changes
body CHANGED
@@ -55,7 +55,7 @@
55
55
  ```
56
56
 
57
57
  ```
58
- //パラメータ
58
+ //$params内容
59
59
  array(
60
60
  null //test_no1 整数の場合問題なく動く
61
61
  ,null //test_no2