使用PHP将数据从SQL表传输到iOS应用程序

时间:2017-04-27 17:36:10

标签: php mysql json

我正在创建一个iOS应用程序,允许用户使用存储在SQL数据库中的凭据登录应用程序。我有一个名为“a_Employee”的表,如下所示:

a_Employee Table screenshot

要将数据库连接到我的iOS应用程序,我想用JSON编码php并从数据库中获取它。

在employees.php脚本中,我想获取a_Employee表中的所有数据,并在JSON中对其进行编码。

这是employees.php脚本:

<html>
<head> <meta charset=UTF-8> <title>Query PHP</title></head>
<body>
<?php
    print "Opened Service.php";
    // Set up the data source information
    $dsn = "";
    $username = '';
    $password = '';

    // Create connection to MySQL
    try {
            $db = new PDO($dsn, $username, $password);
            echo "Connected to ekepling";

            // Execute a query
            $results = $db->query('SELECT * a_Employee');
            //$results2 = $db->query('SELECT lname FROM a_Employee');

            $myArray = array(); // new empty array
            $tempArray = array(); //temp array
            echo "Got to after execute query";

            // Put the titles in an array
            while ($row = mysqli_fetch_row($result)) {

                    echo "<tr>";
                    foreach($row as $cell)
                    echo "<td>$cell</td>";
                    echo "</tr>\n";

                    /*
                    $myArray[] = $row["fname"];
                    $userID = $row['user_id'];
                    $userName = $row['user_name'];
                    $fname = $row['fname'];
                    $lname = $row['lname']
                    $manager_id = $row['manager_id'];
                    $title = $row['title'];
                    $email = $row['email'];
                    $project = $row['project'];
                    $businessPhone = $row['business_phone'];
                    $jobTitle = $row['job_title'];
                    $department = $row['department'];
                    $bio = $row['bio'];
                    $company = $row['company'];
                    $officeLocationID = $row['office_location_id'];
                    $teamLeadID = $row['team_lead_id'];
                    $projectID = $row['project_id'];
                    $locationID = $row['location_id'];
                    $user_pass = $row['user_pass'];
                    $tempArray = $row;
                    array_push($myArray, $tempArray);
                    */
            }

            echo json_encode($myArray);
            echo "Got to after putting titles in array";
            // Display the titles with html format information
            foreach($myArray as $title)
            {
                    echo $title;
                    echo "\n<br/>\n";
            }

            echo "Got to print array contents";

            // Close the MySQL connection
            $results = null;
            $db = null;
    }

     catch (PDOException $e) {
            // If this fails and exception isn’t caught, traceback will reveal
            //username and password
            echo "Error!: " . $e->getMessage() . "<br/>";
            die();
    }
?>
</body> </html>

省略了用户名,密码和数据库值。当我运行此脚本时,我没有显示任何内容。如何编写脚本以使SELECT * FROM a_Employee语句获取所有数据并将其编码为JSON?

0 个答案:

没有答案