douzhanshen0657 2015-01-05 17:35
浏览 33
已采纳

用PHP查询MySQL

I am trying to query a MySQL database with PHP and return the results as JSON. I'm new to PHP and web development so I'm not sure what I'm doing wrong. I've set up the database using MAMP. My parameters are being printed but I'm not getting the JSON. I've gotten this far with the help of a tutorial.

EDIT: I just went into phpMyAdmin to make sure it was working and when I click on Server:localhost:8889, a window pops up that says Error in processing request. Error code 404.

I'm thinking this is the problem, I'm just not sure why it isn't working. I may reinstall MAMP.

    <?php

$user = 'root';
$password = 'root';
$db = 'TestDB';
$host = '127.0.0.1';
$port = '8889';

$first_name = filter_input(INPUT_GET, 'first_name');
$last_name = filter_input(INPUT_GET, 'last_name');
$membership_number = filter_input(INPUT_GET, 'membership_number');

echo $first_name;
echo $last_name;
echo $membership_number;

// Create connection
    // $con = mysqli_connect("localhost", "root", "root", "TestDB");
    // $con = mysqli_connect("localhost", "root", "root", "TestDB", "8889", $socket);
    $link = mysqli_init();
    $con = mysqli_real_connect($link, $host, $user, $password, $db, $port);

    // Check connection
    if(mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql = "SELECT * FROM NAME WHERE FIRST_NAME = \'$first_name\' and LAST_NAME = \'$last_name\' and MEMBERSHIP_NUMBER = \'$membership_number\'";

    $result = mysqli_query($con, $sql);

    if(!$result) {
        die('Query failed: ' . mysqli_error());
    }

    // Check for results
    // if ($result = mysqli_query($con, $sql)) {
    if($result) {
        // If there are results, create results array and a temporary one to hold the data
        $resultArray = array();
        $tempArray = array();

        // Loop through each row in the result set
        // while($row = $result->fetch_object()) {
        while($row = mysqli_fetch_object($result)) {
            // Add each row to the results array
            $tempArray = $row;
            array_push($resultArray, $tempArray);
        }

        echo $tempArray;
        echo $resultArray;
        echo $result;

        echo json_encode($resultArray);
    }

    // Close connections
    mysqli_close($con);

?>
  • 写回答

2条回答 默认 最新

  • drkxgs9358 2015-01-05 18:15
    关注

    You need to change you $sql variable to remove the escapes on the single quotes. They register as part of the string because you are using double-quotes to wrap it. Basically, you're telling the database to run the query "SELECT * FROM NAME WHERE FIRST_NAME = \'John\' and LAST_NAME = \'Smith\' and MEMBERSHIP_NUMBER = \'VRX78435\'". This will error if you run it directly because the escape characters are not escaping.

    $sql = "SELECT * FROM NAME WHERE FIRST_NAME = '$first_name' and LAST_NAME = '$last_name' and MEMBERSHIP_NUMBER = '$membership_number'";
    

    That should fix it for you.

    There may also be an issue with your connection to the server. mysqli_query() uses the results of mysqli_connect() to run the query. mysqli_real_connect() only returns a boolean value, so it is invalid for this particular use (at least it failed to work on my server).

    This would be a simple matter of replacing the $con and then you can drop the $link variable.

    $con = mysqli_connect($host, $user, $password, $db, $port);
    

    These changes, and assuming the $first_name, $last_name, and $membership_number are all valid, allowed your script to run for me, so I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)