0

私はこのコーディング言語をすべて学び始めたばかりで、先生は私たちに次のことをするように頼んでいます。これをフルタイムで行う人や、コーディングに時間がかかる人にとっては、とても簡単に聞こえるかもしれません。先生はいつも私たちにすべてをグーグルするように言っています、そして私はあまりにも多くのサイトを試しました、しかし私は私を助けるものを全く見つけませんでした。

MySQLデータベースから値を動的に読み取るPHPを使用して2つのJSONドキュメント(製品とカテゴリ)を作成する必要があります。各ドキュメントが呼び出されると、http://jsonlint.com/を使用して検証される完全にフォーマットされたJSONが返されます。

正直なところ、どうしたらいいのかわかりません。私はPHPを理解していませんが、今ではこのJSONが混乱を招いています。

4

1 に答える 1

1

You just need to make a MySQL request to your database:

(this is simple code and not optimized)

// Make a MySQL Connection
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Retrieve the data from the "example" table
$result = mysql_query("SELECT products, categories FROM example")
or die(mysql_error());  

// store all the records of the "example" table into $rows[]
while($ds = mysql_fetch_array( $result )) {
    $rows[] = $ds
}

with this you have an array $rows with your data:

after this you can output the data as JSON:

echo json_encode($rows);

I hope this will help you a little bit.

于 2012-06-26T07:12:40.433 に答える