2015-06-01 29 views
2

我有這段代碼工作正常我的應用程序獲取數據與JSON和都很好,但是當我插入特殊字符像ñ我需要得到我不能有被告知我應該使用utf8_encode,但我不知道如何在這裏應用它。如何從一個mssql數據庫通過PHP傳遞特殊字符

<?php 
    require_once(dirname(__FILE__).'/ConnectionInfo.php'); 


    //Set up our connection 
    $connectionInfo = new ConnectionInfo(); 
    $connectionInfo->GetConnection(); 

    if (!$connectionInfo->conn) 
    { 
     //Connection failed 
     echo 'No Connection'; 
    } 

    else 
    { 
     if (isset($_POST['mod']) && isset($_POST['lec']) && isset($_POST['clase'])) 
     { 
     $mod = $_POST['mod']; 
     $lec = $_POST['lec']; 
     $clase = $_POST['clase']; 
     //Create query to retrieve all contacts 
     $query = 'SELECT TituloEjercicio,PreguntaEjercicio,Opcion1Ejercicio,Opcion2Ejercicio,Opcion3Ejercicio,Opcion4Ejercicio,EstaCorrectaEjercicio FROM ejercicios WHERE QueModulo = ? and QueLeccion = ? and Queclase = ?'; 
     $params = array($mod,$lec,$clase);  
     $stmt = sqlsrv_query($connectionInfo->conn, $query,$params); 

     if (!$stmt) 
     { 
      //Query failed 
      echo 'Query failed'; 
     } 
     else 
     { 
      $contacts = array(); //Create an array to hold all of the contacts 
      //Query successful, begin putting each contact into an array of contacts 

      while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) //While there are still contacts 
      { 
       //Create an associative array to hold the current contact 
       //the names must match exactly the property names in the contact class in our C# code. 
       $contact = array("lbl_variable_cuestionario_titulo" => $row['TituloEjercicio'], 
           "lbl_variable_pregunta" => $row['PreguntaEjercicio'], 
           "opcion1" => $row['Opcion1Ejercicio'], 
           "opcion2" => $row['Opcion2Ejercicio'], 
           "opcion3" => $row['Opcion3Ejercicio'], 
           "opcion4" => $row['Opcion4Ejercicio'], 
           "EstaCorrecta" => $row['EstaCorrectaEjercicio'] 
           );           
       //Add the contact to the contacts array 
       array_push($contacts, $contact); 
      } 

      //Echo out the contacts array in JSON format 
      echo json_encode($contacts); 
      sqlsrv_close($connectionInfo->conn); 
     } 
     } 
     sqlsrv_close($connectionInfo->conn); 
    } 
    sqlsrv_close($connectionInfo->conn); 
?> 

回答

0

如果您的問題在於將非拉丁字符推送到MySQL,那麼您可能只需將數據庫配置爲使用UTF8。有很好的tutorials online,告訴你如何做到這一點。

+0

即時通訊使用mssql沒有MySQL的任何方式來做到這一點? –

+0

就我的Google-fu而言,MSSQL不支持UTF8。似乎有辦法解決這個問題。 [查看您可以找到的內容。](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=configuring%20mssql%20utf8) –