HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
File: C:/xampp/htdocs/myapi/getSqlDataDeCo3.php
<?php
// Assuming your PHP script interacts with a database or performs some other logic to generate data
// For this example, let's assume it generates a simple JSON response

// Sample data for demonstration purposes
$getCoS=$_GET['getCo'];
$po1="";
$po2="";
$po3="";
$po4="";
$po5="";

//echo "getCo:".$getCo."_".$getPo;
$servername = "127.0.0.1";
$username = "root";
$password = "fredsql";
$DBNAME = "tvicheck-st";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";


if( !mysqli_select_db($conn, $DBNAME)) {
  die ("無法選擇資料庫");
}
mysqli_query( $conn, "SET NAMES 'utf8'");



		$querys = "select po from spotreport where (company='$getCoS') ORDER BY id DESC ";
		//echo $querys;
		$results =  mysqli_query($conn, $querys);
		$num = mysqli_num_rows( $results);
		//echo "<br>num:".$num;
		
	// po1..po5 always present so older apps keep working; po6+ appear when they exist
	$data = array('po1' => "", 'po2' => "", 'po3' => "", 'po4' => "", 'po5' => "");
	if ($num > 0)
	{
		$i=1;
while($row = mysqli_fetch_object($results))
	{
		$data["po".$i]=$row->po;
		$i=$i+1;
	}
	
	}
//echo "<br> po:".$po1."_".$po2."_".$po3;



// Encode as JSON; JSON_FORCE_OBJECT keeps it {..} even when the company has no jobs
$jsonData = json_encode($data, JSON_FORCE_OBJECT);

// Set the response header to indicate that the content is JSON
header('Content-Type: application/json');

// Output the JSON data
echo $jsonData;
?>