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

时间:2022-09-22 16:03:34

This question already has an answer here:

这个问题在这里已有答案:

I'm having trouble remotly accessing a mysql database, for an android application in c#. Here is my C# script.

对于c#中的android应用程序,我无法远程访问mysql数据库。这是我的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);

    }

And my PHP script:

我的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);
}
 ?>

When I go to http://hitachipickmeup.dx.am/SQLQuery.php, it says "connected successfully".

当我去http://hitachipickmeup.dx.am/SQLQuery.php时,它说“连接成功”。

Phpmyadmin(database empty):http://i.stack.imgur.com/WOu6l.png

1 个解决方案

#1


1  

Replace the last if statement with this:

用这个替换最后一个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
    ]);
}

As you can see I changed it to PDO and I prepared the statement.

正如您所看到的,我将其更改为PDO并准备了声明。

As I also said in a comment you can't mix database API's, either use PDO or MySQLi.

正如我在评论中所说,你不能混用数据库API,要么使用PDO,要么使用MySQLi。

#1


1  

Replace the last if statement with this:

用这个替换最后一个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
    ]);
}

As you can see I changed it to PDO and I prepared the statement.

正如您所看到的,我将其更改为PDO并准备了声明。

As I also said in a comment you can't mix database API's, either use PDO or MySQLi.

正如我在评论中所说,你不能混用数据库API,要么使用PDO,要么使用MySQLi。