为什么我不能用c#和php访问mysql数据库?

时间:2016-04-12 21:00:37

标签: c# php mysql database phpmyadmin

我无法远程访问mysql数据库,对于c#中的android应用程序。 这是我的C#脚本。

void mSignUpButton_Click(object sender, EventArgs e)
    {
        WebClient client = new WebClient();
        Uri uri = new Uri("http://hitachipickmeup.dx.am/SQLQuery.php");
        NameValueCollection parameters = new NameValueCollection();
        parameters.Add("Name", textName.Text);
        parameters.Add("Email", txtEmail.Text);
        parameters.Add("Password", txtPassword.Text);
        client.UploadValuesAsync(uri, parameters);

    }

我的PHP脚本:

 <?php
 $servername = "fdb13.awardspace.net";
 $username = "2100274_pickmeup";
 $password = "*************";
 try 
 {
     $conn = new PDO("mysql:host=$servername;dbname=2100274_pickmeup",      $username, $password);
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     echo "Connected successfully";
}
catch(PDOException $e)
{
   echo "Connection failed: " . $e->getMessage();
}


if (isset($_POST['Name']) && isset($_POST['Email']) &&  isset($_POST['Password']))
{
   //Get the POST variables
   $mName = $_POST['Name'];
   $mEmail = $_POST['Email'];
   $mPassword = $_POST['Password'];

   //Insert new contact into database
   $query = 'INSERT INTO Contact_info (Contact_Name, Contact_Email, Contact_Password) VALUES ("$mName", "$mEmail","$mPassword")';
   $result = mysqli_query($query);
}
 ?>

当我转到http://hitachipickmeup.dx.am/SQLQuery.php时,它表示&#34;已成功连接&#34;。

Phpmyadmin(数据库为空):http://i.stack.imgur.com/WOu6l.png

1 个答案:

答案 0 :(得分:1)

将最后一个if语句替换为:

if (isset($_POST['Name']) && isset($_POST['Email']) &&  isset($_POST['Password'])){
    //Get the POST variables
    $mName = $_POST['Name'];
    $mEmail = $_POST['Email'];
    $mPassword = $_POST['Password'];

    //Insert new contact into database
    $query = $conn->query('INSERT INTO Contact_info (Contact_Name, Contact_Email, Contact_Password) VALUES (:name, :email, :password)');
    $query->execute([
        ':name' => $mName,
        ':email' => $mEmail,
        ':password' => $mPassword
    ]);
}

正如您所看到的,我将其更改为PDOprepared声明。

正如我在评论中所说,您无法混合数据库API,可以使用PDOMySQLi