我的代码没有´如果数据库中有超过2个对象,则无法工作

vdgimpew  于 2021-06-15  发布在  Mysql
关注(0)|答案(0)|浏览(211)

我´我是react.js的新手,从数据库中提取对象时需要一些帮助。我有一个api从mysql数据库中获取数据,当我有两个以上的对象时 sportid='26' 在数据库里我得到了这个。
“未处理的拒绝(syntaxerror):json输入意外结束”。
如果是一个或两个物体,它工作得很完美。
有人能告诉我为什么吗?

import React, { Component } from 'react';
import Popup from './Popup';

class Riding extends Component {

    constructor(props) {
        super(props);
        this.state = {
          data: null,
          clicked: false,
          open: false,
        };
        this.togglePopup = this.togglePopup.bind(this);
    }

    togglePopup() {
        this.setState(state => ({
            clicked: true
        }));
    }

    componentDidMount() {

        let data = fetch('http://localhost/reusesport/src/api/riding.php')
            .then((res) => {
                res.json().then((res) => {
                    console.log(res);
                    this.setState({data:res})
                })
            })

    }

    render() {
      return (
        <div>
            <h1 className="Advertisment_title">Ridsport</h1>
            <div className="Advertisment">
              {
                    this.state.data ?
                    this.state.data.map((item) =>
                    <div className="AdvertismentBox"  onClick={this.togglePopup}>
                     {this.state.clicked ? <Popup id={item.id} title={item.title} text={item.text} date={item.date} price={item.price} municipality={item.municipality} county={item.county}/> : null}
                    <h3 className="AdvertismentBox_title">{item.title}</h3>
                    <img src={item.picture} alt={item.title}/>
                    <h3 className="AdvertismentBox_price">{item.price}</h3>
                    <h3 className="AdvertismentBox_date">{item.date}</h3>
                    <h3 className="AdvertismentBox_county">{item.county}</h3>
                    </div>
                )
                    :
                    <h3>Vänta - datat hämtas</h3>
                }
            </div>    
        </div>
    );
  }
}

export default Riding;

我的api:

<?php
header('Access-Control-Allow-Origin: *');
header("Content-Type:application/json");

include("connect.php");

$sql = "SELECT * FROM `advertisment` WHERE sportid='26'";

$result = $connect ->query($sql);
$return_arr = array();

if($result = mysqli_query($connect, $sql))  {
    while($row = mysqli_fetch_assoc($result)) {
        $row_array['id'] = $row['id'];
        $row_array['title'] = $row['title'];
        $row_array['picture'] = $row['picture'];
        $row_array['price'] = $row['price'];
        $row_array['date'] = $row['date'];
        $row_array['county'] = $row['county'];
            array_push($return_arr, $row_array);
    }
}    
mysqli_close($connect);
echo json_encode($return_arr);
?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题