我在LAMP服务器上为客户端构建一个Web应用程序,用PHP编写,存储在MySQL数据库中的数据。在current.php
脚本中,我需要根据用户登录的内容和在上一页上做出的选择从两个表中获取信息。以前的查询似乎干扰了新的查询。
问题:我收到错误Undefined index: col5
,指的是下面标有(// ERROR HERE
)的行。
到目前为止已尝试:我在相关行之前添加了print_r($resultTwoRow);
,以查看实际返回的数据。这显示:
Array ( [0] => foo [col1] => foo [1] => bar [col2] => bar )
这是FIRST查询的结果/行。 为什么还在闲逛?如何从第二个查询中获取行数据?
======users_widget_links======
| id | colOne | colTwo |
|------|----------|----------|
| 123 | foo | bar |
| 666 | FOO | BAR |
==============================
=====================choices===========================
| id | col1 | col2 | col3 | col4 | col5 |
|--------|--------|--------|--------|--------|--------|
| '42' | 0 | 1 | 1 | 1 | 0 |
| '43' | 1 | 1 | 0 | 0 | 1 |
=======================================================
<?php
$host = "host";
$port = "port";
$dbname = "db";
$user = "user";
$password = "pw";
$cnxn = mysqli_connect( $host, $user, $password, $dbname );
if(!$cnxn){
echo "Error : Unable to open database\n";
}
<?php
$userID = $_SESSION['user']['user_id']; // 123 in this example
$choiceID = $_POST['choice']; // '42' in this example
/* Omitted code */
require('connection.php');
$queryOne = "SELECT users_widget_links.colOne, users_widget_links.colTwo FROM users_widget_links WHERE id = '123'";
$resultOne = mysqli_query($cnxn, $queryOne) or die(mysqli_error());
$resultOneRow = mysqli_fetch_array($resultOne); // not looping because only one result
$thingOne= $row['colOne'];
$thingTwo = $row['colTwo'];
if ($userID == '666') $thingOne = 'exception';
mysqli_close($cnxn);
require('connection.php');
$queryTwo = "SELECT `col1`, `col2`, `col3`, `col4`, `col5` FROM `tableTwo` WHERE id = '$choiceID'";
$resultTwo = mysqli_query($cnxn, $queryTwo) or die(mysqli_error());
$resultTwoRow = mysqli_fetch_array($resultTwo); // not looping because only one result
print_r($resultTwoRow);
$columnFive = $resultTwoRow['col5']; // ERROR HERE
mysqli_close($cnxn);
/* Omitted code */
?>
答案 0 :(得分:0)
您可以从第一个查询中取消设置变量。使用函数unset($ variable)