我目前正在开发一个项目,需要从数据库中的 3 个不同表中获取一些数据并回显它们,以便我可以在 jQuery 中处理结果。 我正在向我的 PHP 发送一个包含 3 个变量的 GET 请求。第一个用于确定需要执行哪个“命令”,另外两个用于确定需要查询哪个表和哪一行。
这是我到目前为止的代码:
} elseif($_GET['command']=='getpage') {
$mypid = $_GET['id'];
$mytable = $_GET['table'];
$link = mysqli_connect($dbserver,$userdb,$passdb,$db_typo) or die(mysqli_error($link));
if($mytable == 'tableName1'){
$query = 'SELECT * FROM table1 WHERE uid = "'.$mypid.'"'; //I need 6 elements from this table
} elseif($mytable == 'tableName2'){
$query = 'SELECT * FROM table2 WHERE uid = "'.$mypid.'"'; //I need 7 elements from this table
} elseif($mytable =='tableName3'){
$query = 'SELECT * FROM table3 WHERE uid = "'.$mypid.'"'; //I need 8 elements from this table
} else {
echo 'no such table supported for this command';
}
$result = mysqli_query($link, $query) or die(mysqli_error($link));
$pagecontent = array();
while($row = mysqli_fetch_assoc($result)){
$pagecontent[] = array(
'id' => utf8_encode($row['uid']),
'name' => utf8_encode($row['name']),
'text1' => utf8_encode($row['text1']), //not every table has this
'text2' => utf8_encode($row['text2']),
'img' => utf8_encode($row['image']),
'parent' => utf8_encode($row['parent']), //not every table has this
'sub_parent' => utf8_encode($row['sub_parent']), //not every table has this
'deleted' => utf8_encode($row['deleted'])
);
}
echo '{"content": '.json_encode($pagecontent).'}';
}
我需要从数据库中获取 50 多个页面。因此,当我让发送 GET 请求的 jQuery 函数运行时,我最终会用
向 error.log 发送垃圾邮件PHP Notice: Undefined index: text1 in /var/www/my.php on line 171
这是我不想要的。
除了将查询和 while 循环放在 if 语句中之外,还有其他方法可以解决此“问题”吗?
最佳答案
添加检查数组键是否存在
'text1' => isset($row['text1']) ? utf8_encode($row['text1']) : '',
关于php - 如何阻止我的 PHP 使用 PHP 通知向 error.log 发送垃圾邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30077938/