I have a table in my database, from where I fetch data and show them in a UITableView
. But recently it is not working. In error log it says,
PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /home/
After digging a little I found that It happen probably because of using mysql_connection
. Which is out dated. So I try to wrap my mysql
code with mysqli
. But it not working. Always it returns nill
. These are my code :
Old mysql
which was working perfectly :
<?php
/* ----------------------------- Code for Dabase ----------------------------- */
// Database Properties
$dbhost = 'localhost';
$dbuser = '*****';
$dbpass = '*****';
$db = '*****';
// Connect Database
$conn = mysql_connect($dbhost,$dbuser,$dbpass) or die (mysql_error());
$dbconnect = mysql_select_db($db, $conn) or die(mysql_error());
/* ----------------------------- Code for Dabase ----------------------------- */
// Check to see if we can connet to the server
if(!conn)
{
die("Database server connection faild!");
}
else
{
if(!dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM country";
$resultset = mysql_query($query, $conn);
$records = array();
// Loop through all our records and add them to our array
while($r = mysql_fetch_assoc($resultset))
{
$records[]= $r;
}
// Output the data as JSON
echo json_encode($records);
}
}
?>
Same thing, I give a try using mysqli
:
<?php
//error_reporting (0);
// Database Properties
$dbhost = 'localhost';
$dbuser = '*****';
$dbpass = '*****';
$dbname = '*****';
$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$record = array();
if ($result = $mysqli->query("SELECT * FROM country")) {
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$record[] = $row;
echo $row;
}
echo json_encode($record);
}
$result->close();
$db->close();
?>
But it is not working. Below is my fetching code in obj c:
+(CountryModel *)retriveCountryModelForIsoCountryCode:(NSString *)isoCountryCode
{
CountryModel *countryModel = nil;
NSURL *url = [NSURL URLWithString:getDataURL];
//NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
if (error) {
NSLog(@"Error %@", error);
}
// Set up json array
NSMutableArray *jsonArray = [[NSMutableArray alloc] init];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
for (int i=0; i < jsonArray.count; i++)
{
// Create CityInfo object
NSString *country_id = [[jsonArray objectAtIndex:i] objectForKey:@"country_id"];
NSString *country_name = [[jsonArray objectAtIndex:i] objectForKey:@"country_name"];
NSString *country_code = [[jsonArray objectAtIndex:i] objectForKey:@"country_code"];
NSString *country_flag_url = [[jsonArray objectAtIndex:i] objectForKey:@"country_flag_url"];
if ([country_id isEqualToString:isoCountryCode]) {
countryModel = [[CountryModel alloc] initWithCountryID:country_id andCountryName:country_name andCountryCode:country_code andCountryFlagURL:country_flag_url];
}
}
return countryModel;
}
With new wrapper, jsonArray
always getting nill
. I think, the problem is when I am trying to create a jsonArray
in mysqli
. Is there any problem? If you understand my problem, please answer me. A lot of thanks in advance.
Have a nice day.