dousong1992 2015-10-12 07:32
浏览 133
已采纳

在线输入PHP函数,查询(通过PDO)MySql输出JSON

This is my first time posting, so forgive me if I'm not perfect at this yet. I'm new to web development and most of what I have already gotten is just through searching for answers. I've been doing pretty well so far, but I can't seem to find a clear answer to what I'm trying to do.

I'm trying create an online database of Teacher Assistant information that's readily accessible. It's a self-assigned project that I'm doing with a combination of MySQL (database), PHP (for querying/echoing JSON), and HTML.

This is what I have so far:

(partial) test_db.sql:

CREATE TABLE IF NOT EXISTS `TAs` ( 
`ID` int(1) NOT NULL auto_increment, 
`Name` varchar(40) NOT NULL, 
 PRIMARY KEY (`ID`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; 

INSERT INTO `TAs` (`Name`) VALUES 
 ('Firstname Lastname'), 
 ('Firstname2 Lastname2'), 
 ('Firstname3 Lastname3'), 
 ('Firstname4 Lastname4');
 ...
 ...

test_to_json.php:

<?php

function name($x) {

    $db = new PDO('mysql:host=localhost;dbname=json_test;charset=utf8', 'root', 'user');

    $sql = "select TAs.Name, Classes.Classname, Sessions.Day, Sessions.Time, Sessions.Location 
    from TAs, Classes, Sessions 
    where TAs.ID = Sessions.TAID and Classes.ID = Sessions.ClassID and TAs.name like '%$x%'";

    $array = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode($array), "
";
}

name($_POST["name"]);

?>

testing.html:

<!DOCTYPE HTML>
<html>
<body>

<form action="test_to_json.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>

</body>
</html>

First, is this even possible? Or is this an exercise in futility?

If it is, I know that my database and PHP are working to output the JSON. My problem comes with the html; when I'm trying to figure out how to use an online user input to create a search of the database.

So how do I implement something like this? Something like a HTML user input (i.e. a search) that submits to the PHP for for querying the MySQL and then returning the proper JSON for use?

A direct answer would be awesome! Resources that could teach me work too though; learning is great. Really anything that can put me on the right track is welcome.

Thanks for any help!

  • 写回答

1条回答 默认 最新

  • dongwei3172 2015-10-12 15:48
    关注

    You did not put your value inside the input.

    <form action="test_to_json.php" method="post">
    Name: <input type="text" value="" name="name"><br>
    <input type="submit">
    </form>
    

    What helps is if you download an addon ( Firebug ) for firefox. This will show you what you have posted in the "Net" tab. If it is showing your post it meens that your html works fine. Try that and see if it works. If not, let me know. And Also, its better to work with a INNER JOIN

    $sql = "SELECT 
    T.Name, C.Classname, S.Day, S.Time, S.Location 
    FROM TAs AS T 
    INNER JOIN Sessions AS S 
    ON T.ID = S.TAID 
    INNER JOIN Classes AS C 
    ON C.ID = S.ClassID 
    WHERE T.Name LIKE '%$x%'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败