简体   繁体   English

SQL表中的JSON获得双重结果

[英]JSON from SQL Table get double result

first of all i have to tell you that it is my first step on php and JSON. 首先,我必须告诉您,这是我开发php和JSON的第一步。

I decided to use JSON to get value from a customer SQL Table. 我决定使用JSON从客户SQL表中获取价值。

I get my results using this script : 我使用以下脚本获得结果:

mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");


mysql_query('SET CHARACTER SET utf8');

$fet=mysql_query('select * from  vehicule');

$json = array();

while($r=mysql_fetch_array($fet)){
    $json[] = $r;
}

header('Content-Type: application/json');


echo $json_data=json_encode($json);

Everything is ok, exept that my JSON results looks like : 一切正常,例如我的JSON结果如下所示:

        0 = 462;
        1 = "Hyundai ix20 crdi 115 panoramic sunsation";
        10 = 1346450400;
        11 = "462-Hyundai-ix20-crdi-115-panoramic-sunsation";
        12 = 462;
...

       id = 462;
        kilometrage = 14400;
        marque = 4;
        modele = 137;
        motorisation = 2;
        ordre = 462;
        prix = 17500;
        puissance = 6;
        titre = "Hyundai ix20 crdi 115 panoramic sunsation";
        url = "462-Hyundai-ix20-crdi-115-panoramic-sunsation";

... ...

I have result of the table in 2 versions : one with 0:value, 1:value, 2... and the other one using the table key, how can i print only the second one ? 我有2种版本的表格结果:一种使用0:value,1:value,2 ...,另一种使用table键,我如何只打印第二种?

By the way can someone give me link so i can know by what i have to replace mysql which is think out of date ? 顺便说一句,有人可以给我链接,这样我就可以知道我必须更换什么认为过时的mysql吗? (i'm a beginner few hours using PHP) (我是一个使用PHP的初学者,需要几个小时)

Thank you very much ! 非常感谢你 !

You have two different issues happening here. 您在这里发生了两个不同的问题。 One is outright causing the issue you are seeing, and the other is a bad practice mistake that will leave you wide open for trouble in the long run. 一种是直接导致您看到的问题,另一种是错误的做法错误,从长远来看,它会使您大开麻烦。

The first issue is the one you're asking about. 第一个问题是您要问的问题。 The mysql_fetch_array function (see the Docs here) expects a minimum of one input (the result input) that you are providing. mysql_fetch_array函数(请参见此处的文档 )期望至少提供一个输入(结果输入)。 It also has a second, optional input. 它还具有第二个可选输入。 That optional input defaults to MYSQL_BOTH, which returns an associative array with the results available both through keys (column names) and their indexes. 该可选输入默认为MYSQL_BOTH,它将返回一个关联数组,其结果可通过键(列名)及其索引使用。 Which is to say, that if you select the column 'id', you get it's value in both $array[0] and $array['id']. 这就是说,如果您选择列“ id”,则会在$ array [0]和$ array ['id']中获得其值。 It's duplicated, and thus the JSON process carries over the duplication. 它是重复的,因此JSON流程会进行重复。 You need to provide a second value to the function, either MYSQL_ASSOC to get $array['id'] or MYSQL_NUM to get $array[0]. 您需要为该函数提供第二个值,即MYSQL_ASSOC以获取$ array ['id']或MYSQL_NUM以获取$ array [0]。

Your second issue is the choice of functions. 第二个问题是功能的选择。 You're using the 'raw' mysql functions. 您正在使用“原始” mysql函数。 These have been depreciated, which is a technical term that means 'these functions are no longer supported, but we've left them in to give you time to fix legacy code'. 这些已贬值,这是一个技术术语,表示“不再支持这些功能,但我们留给了它们以便给您时间来修复旧代码”。 For legacy, read 'old'. 对于旧版,请阅读“旧版”。 Those functions will be going away soon, and you need to upgrade to a better option -- either the mysqli functions, or the PDO class. 这些功能将很快消失,您需要升级到更好的选项-mysqli功能或PDO类。 I strongly recommend the PDO class, as once you learn it it's easy to learn and has the advantage of being more portable. 我强烈推荐PDO课程,因为一旦您学习它,它就很容易学习,并且具有更便携的优势。 Whichever set you go with, you need to learn to use prepared statements as both a performance and security issue. 无论采用哪种设置,都需要学习将准备好的语句用作性能和安全性方面的问题。 Right at the moment, you're working with 'raw' statements which have a history of being very easy to interfere with via what's called an 'injection attack'. 目前,您正在使用“原始”语句,该语句具有很容易通过所谓的“注入攻击”进行干扰的历史。 You can see a fictionalized example of such an attack here , and there are plenty of articles online about it. 您可以在此处看到虚构的此类攻击示例,并且在线上有很多文章。 These attacks can be incredibly complex and difficult to fight, so using prepared statements (which handle it for you), is strongly recommended. 这些攻击可能非常复杂且难以抵抗,因此强烈建议使用准备好的语句(为您处理)。 In the specific example you're using here, you don't need to worry about it because you aren't including any user inputs, but it's an important habit to get into. 在此处使用的特定示例中,您不必担心它,因为您不包括任何用户输入,但这是一种重要的习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM