Search code examples
phpmysqlxcodexcode4.6

Xcode connect error


I have Xcode 4.6. I want connect mysql database with my app. So i found this tutorial

http://www.youtube.com/watch?v=ipppykYUzh4#at=104,http://www.youtube.com/watch?v=tvv1KlZ-594

I am was continue Step by Step but i do not know where is my error.

this is my xcode project. Plese look at this and tell me what is wrong. https://www.dropbox.com/s/4pj1xj4f736l42m/mysql.zip

Thanks for help.

this is php code

<?php
header('Content-type: application/json');
$DB_HostName = '127.0.0.1';
$DB_Name = 'test';
$DB_User = 'root';
$DB_Pass = ''; 

$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());

$sql = 'SELECT * FROM phpmysql';

$result = mysql_query($sql,$con) or die(mysql_error());

$num = mysql_numrows($result);

mysql_close();

$rows =array();
while ($r = mysql_fetch_assoc($result)){

    $rows[] = $r;
}

echo json_encode($rows);



?>

THIS IS MY ERROR

2013-07-30 10:27:22.335 mysql[4095:c07] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:4460
2013-07-30 10:27:22.336 mysql[4095:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(0x1c91012 0x10cee7e 0x1c90e78 0xb64665 0xc46c4 0x2c88 0xcd8fb 0xcd9cf 0xb61bb 0xc6b4b 0x632dd 0x10e26b0 0x228dfc0 0x228233c 0x228deaf 0x1022bd 0x4ab56 0x4966f 0x49589 0x487e4 0x4861e 0x493d9 0x4c2d2 0xf699c 0x43574 0x4376f 0x43905 0x4c917 0x1096c 0x1194b 0x22cb5 0x23beb 0x15698 0x1becdf9 0x1becad0 0x1c06bf5 0x1c06962 0x1c37bb6 0x1c36f44 0x1c36e1b 0x1117a 0x12ffc 0x243d 0x2365)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

Solution

  • Ok, the problem here is that your PHP script returns json_encode($rows) at first, then a var_dump($rows). So, clearly, it's not a JSON that is returned, while your Objective-C code expects a JSON.

    Try adding a header('Content-type: application/json'); in the beginning of your file, and remove the var_dump at the end.

    EDIT : this is the new PHP script

    <?php
    
    header('Content-type: application/json'); // Specify that the result of your script is a JSON
    
    $DB_HostName = '127.0.0.1';
    $DB_Name = 'test';
    $DB_User = 'root';
    $DB_Pass = ''; 
    
    $con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
    mysql_select_db($DB_Name,$con) or die(mysql_error());
    
    $sql = 'SELECT * FROM phpmysql';
    
    $result = mysql_query($sql,$con) or die(mysql_error());
    
    $num = mysql_numrows($result);
    
    mysql_close();
    
    $rows =array();
    while ($r = mysql_fetch_assoc($result)){
    
        $rows[] = $r;
    }
    
    echo json_encode($rows);
    ?>
    

    EDIT 2 : Here is a similar question to yours, see the answer.