Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

calling a php function from c#

Discussion in 'Scripting' started by palomo9314, Dec 19, 2018.

  1. palomo9314

    palomo9314

    Joined:
    Dec 14, 2014
    Posts:
    8
    Im trying to acced to a database via PHP

    i have seen post that i have to call the PHP file via WWW
    im doing this:
    Code (CSharp):
    1. IEnumerator llama_php(){
    2.      
    3.  
    4.         string textodd="";
    5.      
    6.         string url="file:///C:/xampp/htdocs/webservice/conexion_juan.php?f=utf8_converter";
    7.          using (WWW www = new WWW(url))
    8.         {
    9.             yield return www;
    10.             textodd=""+www.text;
    11.             Debug.Log("descargando...");
    12.         }
    utf8_converter is the function on PHP that i need to call

    this is the php file
    Code (CSharp):
    1. <?php
    2.   class conexion{
    3.     public function conectar(){
    4.       $hostname = 'localhost';
    5.       $database = 'prueba_uno';
    6.       $username = 'root';
    7.       $password = 'password';
    8.       $mysqli = new mysqli($hostname, $username,$password, $database);
    9.       if($mysqli -> connect_errno){
    10.         die( "Fallo la conexión a MySQL: (" .$mysqli -> mysqli_connect_errno(). ") " . $mysqli -> mysqli_connect_error());
    11.            return false;
    12.        }
    13.       else{
    14.            return $mysqli;
    15.       }
    16.     }
    17.     #Este metodo devuelve recibe un arreglo y lo codifica en utf8 para no causar problemas en json
    18.     function utf8_converter($array){
    19.       array_walk_recursive($array, function(&$item, $key){
    20.           if(!mb_detect_encoding($item, 'utf-8', true)){
    21.                   $item = utf8_encode($item);
    22.           }
    23.       });
    24.  
    25.       return $array;
    26.     }
    27.   }
    28. ?>
    29.  
    but instead of give m the array, or a string with only the db array, it returns me the whole code in an string
    "<?php
    class conexion{......"

    can you help me please?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    This is a web server configuration issue, please consult with the manual of the webserver of your choice or try to get help on their forums.
     
  3. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    I believe you'll need to call the PHP file via http and not file. You generally do not want to connect directly to a database like this, but instead want to implement a web api service.
     
    Joe-Censored and Lurking-Ninja like this.
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Ouch, I didn't catch that, I need a coffee, apparently. :D
     
    Joe-Censored likes this.