如何操作/更改从mysql表

时间:2017-08-29 03:45:45

标签: php mysql arrays loops while-loop

我从我的桌子中选择所有      

  function convert(){

  $mysqli = new MySQLi('$server', '$database', '$password', '$username');   

  $Sql = "SELECT * FROM test1";
  $result = array();
  $res = mysqli_query($mysqli, $Sql);

  while($row = mysqli_fetch_array($res, MYSQLI_BOTH)){
  $result[] = $row; 
  }  

此时我需要更改数组值并组合2列。我能够在第一组下面使用KEY#而不是列名来完善这项工作。

   for($i=0; $i< count ($result) ; $i++)
   if ($result[$i]['4'] == 'n'){
      $result[$i]['4'] = "ntu_";
    }
   else{
      $result[$i]['4'] =  "std_";
    }

  for($i=0; $i<count ($result) ; $i++)            
  if ($result[$i]['2'] == 'f'){
      $result[$i]['2'] = "female";
    }
  else{
      $result[$i]['2'] = "male";
    }

 for($i=0; $i<count ($result) ; $i++)
 if ($result[$i]['2'] == 'female' && $result[$i]['4'] == 'ntu_' ){
         $result[$i]['2'] = "ntu_female";
    }   
  elseif ($result[$i]['2'] == 'male' && $result[$i]['4'] == 'ntu_' ){
        $result[$i]['2'] = "ntu_male";
    }
  elseif ($result[$i]['2'] == 'female' && $result[$i]['4'] == 'std_' ){
        $result[$i]['2'] = "std_female";
    }
  elseif ($result[$i]['2'] == 'male' && $result[$i]['4'] == 'std_' ){
        $result[$i]['2'] = "std_male"; 
    }

这是第二集IM NOT ABLE使其工作 - 与上面相同的代码虽然需要不同的数组KEYS - 它用于不同的列。这是我使用的方法不可靠的地方。它没有组合这两个值,如果/ else条件没有正确转换。请参阅下面的print_r($ array)。

  for($i=0; $i< count ($result) ; $i++)
  if ($result[$i]['7'] == 'n'){
      $result[$i]['7'] = "ntu_";
    }
  else{
      $result[$i]['7'] =  "std_";
    }

 for($i=0; $i<count ($result) ; $i++)           
  if ($result[$i]['5'] == 'f'){
      $result[$i]['5'] = "female";
    }
  else {
      $result[$i]['5'] = "male";
    }

  for($i=0; $i<count ($result) ; $i++)
  if ($result[$i]['5'] == 'female' && $result[$i]['7'] == 'ntu_' ){
         $result[$i]['5'] = "ntu_female";
    }   
  elseif ($result[$i]['5'] == 'male' && $result[$i]['7'] == 'ntu_' ){
        $result[$i]['5'] = "ntu_male";
    }
  elseif ($result[$i]['5'] == 'female' && $result[$i]['7'] == 'std_' ){
        $result[$i]['5'] = "std_female";
    }
  elseif ($result[$i]['5'] == 'male' && $result[$i]['7'] == 'std_' ){
        $result[$i]['5'] = "std_male"; 
    }             

   //echo '<pre>';
  //print_r($result);
  //echo '</pre>';

  $_SESSION['Sql'] = $result;

  echo '<pre>';
  print_r($_SESSION['Sql']);
  echo '</pre>';

以下是数组值

  Array
  (
      [0] => Array
          (
              [0] => 1
              [id] => 1
              [1] => Kent Mercer
              [name] => Kent Mercer
              [2] => std_male
              [gender] => m
              [3] => 53
              [age] => 53
              [4] => std_
              [tobacco] => n
              [5] => std_male
              [s_gender] =>  f
              [6] => n
              [s_tobacco] => n
              [7] => std_
          )

      [1] => Array
          (
              [0] => 2
              [id] => 2
              [1] => Larry Bird
              [name] => Larry Bird
              [2] => std_male
              [gender] => m
              [3] => 42
              [age] => 42
              [4] => std_
              [tobacco] => y
              [5] => std_male
              [s_gender] => f
              [6] => n
              [s_tobacco] => n
              [7] => std_
        )

      [2] => Array
          (
              [0] => 3
              [id] => 3
              [1] => Linda Carter
              [name] => Linda Carter
              [2] => ntu_female
              [gender] => f
              [3] => 63
              [age] => 63
              [4] => ntu_
              [tobacco] => n
              [5] => std_male
              [s_gender] => m
              [6] => n
              [s_tobacco] => n
              [7] => std_
          )

   )

下面是数据库表结果 - 请注意,agender2给出1个值且不正确 - 看起来像是从agender处理但是没有性别。

  id name             agender   age  agender2
  1 Kent Mercer     ntu_male    53  ntu_
  2 Larry Bird      std_male    42  std_
  3 Linda Carter    ntu_female  63  ntu_

此行下方的所有代码都非常完美。

   if(!($stmt=$mysqli->prepare('INSERT INTO `converted` (`id`,`name`,`agender`,`age`,`agender2`) VALUES (?,?,?,?,?)'))){      
   echo "<p>Prepare failed: ", mysqli_error($mysqli),"</p>";   
   }elseif(!$stmt->bind_param("issis",$id, $name, $gender, $age, $s_gender)){
   echo "<p>Binding failed: (",$stmt->errno,") ",$stmt->error,"</p>";     
   }else{    
  foreach($result as $i=>$row2){
  list($id,$name,$gender,$age,$s_gender)=$row2;  
  if(!$stmt->execute()){ // if the execute call fails
  echo "<p>Execute failed: (",$stmt->errno,") ",$stmt->error,"</p>";  
  }else{
  echo "<p>Success on index $i </p>";  
    }
    }
    }
    }

   ?>

请帮助。必须有更好的方法来做到这一点

这是完成工作的解决方案;来自@Used_By_Already的评论 基本上使用CASE表达式来更新表中的列。

我仍然想知道如何更改数组元素值并插入新的表格,如果有人能引导我朝着正确的方向THX !!

   $gen = "UPDATE test1 SET gender = CASE 
   WHEN gender = 'f' THEN 'female'
   WHEN gender = 'm' THEN 'male'
           ELSE 'male'  
       END";
  if ($mysqli->query($gen) === TRUE) {
  echo "Record updated successfully";
  } else {
  echo "Error updating record: " . $mysqli->error;
  }

  $tob = "UPDATE test1 SET tobacco = CASE 
   WHEN tobacco = 'n' THEN 'ntu_'
   WHEN tobacco = 'y' THEN 'std_'
           ELSE 'std_'  
       END";     
  if ($mysqli->query($tob) === TRUE) {
  echo "Record updated successfully";
  } else {
echo "Error updating record: " . $mysqli->error;
 }                     

  $combine = "UPDATE test1 SET agender = CASE 
   WHEN tobacco = 'ntu_' AND gender = 'female' THEN 'ntu_female'
   WHEN tobacco = 'std_' AND gender = 'female' THEN 'std_female'
   WHEN tobacco = 'ntu_' AND gender = 'male' THEN 'ntu_male'
   WHEN tobacco = 'std_' AND gender = 'male' THEN 'std_male'
           ELSE 'std_male'  
       END";   
  if ($mysqli->query($combine) === TRUE) {
  echo "Record updated successfully";
  } else {
  echo "Error updating record: " . $mysqli->error;
 }

0 个答案:

没有答案