Estou seguindo um tutorial da net, para conectar no banco mysql mas nao funciona alguém ja fez isso ?
1.
CREATE TABLE `people` (
2.
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
3.
`name` VARCHAR( 100 ) NOT NULL ,
4.
`sex` BOOL NOT NULL DEFAULT '1',
5.
`birthyear` INT NOT NULL
6.
)
[code] 1.
<?php
2.
mysql_connect("host","username","password");
3.
mysql_select_db("PeopleData");
4.
-
$q=mysql_query(“SELECT * FROM people WHERE birthyear>’”.$_REQUEST[‘year’]."’");
-
while($e=mysql_fetch_assoc($q))
-
$output[]=$e; -
print(json_encode($output));
-
mysql_close();
-
?>
[/code]
[code] 1.
String result = “”;
2.
//the year data to send
3.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
4.
nameValuePairs.add(new BasicNameValuePair(“year”,“1980”));
5.
-
//http post
-
try{
-
HttpClient httpclient = new DefaultHttpClient(); -
HttpPost httppost = new HttpPost("http://example.com/getAllPeopleBornAfter.php"); -
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); -
HttpResponse response = httpclient.execute(httppost); -
HttpEntity entity = response.getEntity(); -
InputStream is = entity.getContent(); -
}catch(Exception e){
-
Log.e("log_tag", "Error in http connection "+e.toString()); -
}
-
//convert response to string
-
try{
-
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); -
StringBuilder sb = new StringBuilder(); -
String line = null; -
while ((line = reader.readLine()) != null) { -
sb.append(line + "\n"); -
} -
is.close(); -
result=sb.toString(); -
}catch(Exception e){
-
Log.e("log_tag", "Error converting result "+e.toString()); -
}
-
//parse json data
-
try{
-
JSONArray jArray = new JSONArray(result); -
for(int i=0;i<jArray.length();i++){ -
JSONObject json_data = jArray.getJSONObject(i); -
Log.i("log_tag","id: "+json_data.getInt("id")+ -
", name: "+json_data.getString("name")+ -
", sex: "+json_data.getInt("sex")+ -
", birthyear: "+json_data.getInt("birthyear") -
); -
} -
}
-
}catch(JSONException e){
-
Log.e("log_tag", "Error parsing data "+e.toString()); -
}
[/code]>