私はサイトのキャリア部分を構築している最中であり、まだ構築していないアプリと通信するためにこのPHPを作成しました。問題は、json_encodeを実行すると、データベースの最初の行のみが表示されることです。正しい方向へのポインタをいただければ幸いです。ありがとう、ジョーダン
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
//variables
$server = 'localhost';
$user = 'root';
$password = '';
$db = 'starkdb';
// Connect to Database
$connection = mysql_connect($server, $user, $password)
or die ("Could not connect to server ... \n" . mysql_error ());
mysql_select_db($db)
or die ("Could not connect to database ... \n" . mysql_error ());
//Check if the skill has been updated. If it has, process and save it to the database
//POST update
if (isset($_POST['update']))
{
//Confirm that the 'id' value is a valid integer before getting the data
if (is_numeric($_POST['id']))
{
//Retrieve the data
$id = $_POST['id'];
$title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
$description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
//Error check both fields
if ($title == '' || $description == '')
{
$error = 'ERROR: Please fill in all Fields!';
}
else
{
//Update data to database
mysql_query("UPDATE skill SET title='$title', description='$description' WHERE id='$id'")
or die (mysql_error());
$row = mysql_fetch_array($result);
}
}
//POST create
else
{
//Get data, making sure it is valid
$title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
$description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
//Error check both fields
if ($title == '' | $description == '')
{
$error = 'ERROR: Please fill in all Fields!';
}
else
{
//Save new skill to database
mysql_query("INSERT skill SET title='$title', description='$description'")
or die(mysql_error());
$row = mysql_fetch_array($result);
}
}
}
//GET ID
else
{
//Get the id value from URL
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
//Query DB
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM skill WHERE id=$id") or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$title = $row['title'];
$description = $row['description'];
}
}
//GET list
else
{
$result = mysql_query("SELECT * FROM skill") or die(mysql_error());
$row = mysql_fetch_array($result);
}
}
//Encode data and display with JSON
echo json_encode($row);
?>