为什么我从数据库中得到0而不是值?如果我给$_POST['dataType'] === 'start'
,我做错了什么。我从不调用多个数据库选择请求。我做错了什么?
更新:
if(isset($_POST['dataType'])){
switch ($_POST['dataType']) {
case 'start':
$data_type = 'pictures, videos, audio, documents';
break;
case 'picture':
$data_type = 'pictures';
break;
case 'video':
$data_type = 'videos';
break;
case 'audio':
$data_type = 'audio';
break;
case 'document':
$data_type = 'documents';
break;
default:
$data_type = '';
}
if (!empty($data_type)) {
$userId = mysqli_real_escape_string($connect, $_SESSION['userId']);
if ($_POST['dataType'] !== 'start')
$sql = "SELECT * FROM " . $data_type . " WHERE user_id = " . $userId;
else {
$sql_picture = "SELECT * FROM pictures WHERE user_id = " . $userId . " ORDER BY upload_time";
$sql_videos = "SELECT * FROM videos WHERE user_id = " . $userId . " ORDER BY upload_time";
$sql_audio = "SELECT * FROM audio WHERE user_id = " . $userId . " ORDER BY upload_time";
$sql_documents = "SELECT * FROM documents WHERE user_id = " . $userId . " ORDER BY upload_time";
}
// Check connection
if ($connect->connect_error) {
mysqli_close($connect);
echo 1;
}
else {
if ($_POST['dataType'] !== 'start'){
$result = $connect->query($sql);
if ($result->num_rows > 0) {
$indexOfSuggests = 0;
$data = array();
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$user_id = $row['user_id'];
$name = $row['name'];
$public = $row['public'];
$link = $row['link'];
$upload_time = $row['upload_time'];
$data[$indexOfSuggests] = array($id, $user_id, $name, $public, $link, $upload_time);
$indexOfSuggests++;
}
$result->free();
echo json_encode($data);
}
else {
echo 0; // keine ergebnisse
}
}
else {
$indexOfSuggests = 0;
$new_index;
$collectData = array();
for($i = 0; $i < 4; $i++){
$indexOfSuggests = $new_index;
if($i == 0)
$sql = $sql_picture;
else
if($i == 1)
$sql = $sql_videos;
else
if($i == 2)
$sql = $sql_audio;
else
if($i == 3)
$sql = $sql_documents;
$result = $connect->query($sql);
if ($result->num_rows > 0) {
$data = array();
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$user_id = $row['user_id'];
$name = $row['name'];
$public = $row['public'];
$link = $row['link'];
$upload_time = $row['upload_time'];
$data[$indexOfSuggests] = array($id, $user_id, $name, $public, $link, $upload_time);
array_push($collectData, $data[$indexOfSuggests]);
$indexOfSuggests++;
}
$new_index = $indexOfSuggests;
}
else {
// keine ergebnisse
}
}
echo json_encode($collectData);
}
}
}
else {
header("Location: http://google.com");
}
}
所以我得到了它但却非常不酷。所以我要做的最后一件事是按日期排序数组值。
答案 0 :(得分:0)
这是新的修复:)
<?php
if (isset($_POST['dataType'])) {
switch ($_POST['dataType']) {
case 'start':
$data_type = 'pictures,videos,audio,documents';
break;
case 'picture':
$data_type = 'pictures';
break;
case 'videos':
$data_type = 'videos';
break;
case 'audio':
$data_type = 'audio';
break;
case 'documents':
$data_type = 'documents';
break;
default:
$data_type = '';
}
function get_list ($sql,&$list) {
// Create connection
$connect = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connect->connect_error) {
die(1);
} else {
$result = $connect->query($sql);
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$user_id = $row['user_id'];
$name = $row['name'];
$public = $row['public'];
$link = $row['link'];
$upload_time = $row['upload_time'];
$list[] = array($id, $user_id, $name, $public, $link, $upload_time);
}
}
}
if (!empty($data_type)) {
$list = array();
$userId = mysqli_real_escape_string($_SESSION['userId']);
if ($_POST['dataType'] !== 'start') {
$sql = "SELECT * FROM " . $data_type . " WHERE user_id = " . $userId;
get_list($sql,$list);
} else {
$data_types = explode(',',$data_type);
foreach($data_types as $type) {
$sql = "SELECT * FROM " . $type . " WHERE user_id = " . $userId;
get_list($sql,$list);
}
}
if(empty($list))
die(0);
else
echo json_encode($list);
} else {
header("Location: http://google.com");
}
}
?>