如何在windows phone 8应用程序中调用php文件?

时间:2013-06-11 10:09:07

标签: php login windows-phone-8

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

下面是我的访问数据库代码,它不能正常工作。

XAML CODE

    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);

    ?>

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

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

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