0

我想使用 MySQL 和 php 在 windows phone 中创建登录过程。

下面是我的访问数据库代码,它不起作用。

XAML 代码

    private void btnLogin_Click(System.Object sender, System.Windows.RoutedEventArgs e)
    {
        try
        {
            string url = "http://localhost/check_login/check.php";
            //string url = "http://localhost/sam.php";
            Uri uri = new Uri(url, UriKind.Absolute);

            StringBuilder postData = new StringBuilder();
            postData.AppendFormat("{0}={1}", "sUsername", HttpUtility.UrlEncode(this.txtUsername.Text));
            postData.AppendFormat("&{0}={1}", "sPassword", HttpUtility.UrlEncode(this.txtPassword.Password.ToString()));

            WebClient client = default(WebClient);
            client = new WebClient();
            client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            client.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();

            client.UploadStringCompleted += client_UploadStringCompleted;
            client.UploadProgressChanged += client_UploadProgressChanged;

            client.UploadStringAsync(uri, "POST", postData.ToString());
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        prog = new ProgressIndicator();
        prog.IsIndeterminate = true;
        prog.IsVisible = true;
        prog.Text = "Loading....";
        SystemTray.SetProgressIndicator(this, prog);

    }

PHP代码

    <?php
$objConnect = mysql_connect("localhost","root","");
$objDB = mysql_select_db("login_db");

//$_POST["sUsername"] = "weerachai"; // for Sample
//$_POST["sPassword"] = "weerachai@1";  // for Sample

$strUsername = $_POST["sUsername"];
$strPassword = $_POST["sPassword"];
$strSQL = "SELECT * FROM member WHERE 1 
    AND Username = '".$strUsername."'  
    AND Password = '".$strPassword."'  
    ";

$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
$intNumRows = mysql_num_rows($objQuery);
if($intNumRows==0)
{
    echo "0|0|Incorrect Username and Password";
}
else
{
    echo "1|".$objResult["MemberID"]."|"."Valid User121";
}

/**
return 
x|y|z
    x // (0=Failed , 1=Complete)
    y // MemberID
    z // Error Message
*/

mysql_close($objConnect);

    ?>

我不知道添加任何其他内容以在模拟器中运行该文件。

请帮我。提前致谢。

4

1 回答 1

0

问题可能是您试图从模拟器连接到“localhost”。

WP8 模拟器是一个单独的虚拟机。因此,它认为 localhost 就是它自己。如果您在开发机器上托管 PHP 脚本,您应该通过主机名或 IP 地址从模拟器中引用它。

于 2013-06-12T15:39:18.197 回答