0

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.

9
  • What happened recently that the code stopped working? Did you move your code to new host? Are mysql and myslqi PHP modules installed? Commented Aug 3, 2017 at 18:33
  • Nope, I didn't move. I have contacted with my hosting company, what they have said is that, this is happening for updating php version. Commented Aug 3, 2017 at 18:35
  • Did they update the PHP? Or you chose a newer version of PHP in your control panel? If it's the latter, you might need to enable these modules as well. Commented Aug 3, 2017 at 18:36
  • I have checked my control panel, and it is selected the latest version (php 7.0) automatically. Even I can't change it manually. So.... Commented Aug 3, 2017 at 18:38
  • You should check this with your hosting company. I guess the problem occurs because mysql and mysql extensions are not enabled on your hosting service. Commented Aug 3, 2017 at 18:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.