PHP查询输出SQP

时间:2010-10-27 00:55:33

标签: php sql

我没有实际的php错误,但当我查询这个时,我得到输出:

$sql_result = "SELECT AuthorFirst, AuthorLast, OnHand, Title
              FROM Inventory i, Wrote w, Author a, Book b
              WHERE i.BookCode = b.BookCode AND i.BookCode = w.BookCode
              AND a.AuthorNum = w.AuthorNum AND 2 = i.BranchNum";

当我将2更改为最后一行中的变量时,我没有输出。这是我的完整代码:

<?php

$sql_branch = "SELECT BranchNum
              FROM Branch
              WHERE BranchName = '$_POST[branch]'";

$sql_result = "SELECT AuthorFirst, AuthorLast, OnHand, Title
              FROM Inventory i, Wrote w, Author a, Book b
              WHERE i.BookCode = b.BookCode AND i.BookCode = w.BookCode
              AND a.AuthorNum = w.AuthorNum AND '$branch[BranchNum]' = i.BranchNum";


$connect = mysql_connect('students', 'xxxxx', 'xxxxx') or exit(mysql_error());

mysql_select_db('henrybooks', $connect);

if($branch_result = mysql_query($sql_branch, $connect)) {
  $branch = mysql_fetch_array($branch_result);
}
else {
  echo mysql_error();
}

if(mysql_query($sql_result, $connect)) {
  $result = mysql_query($sql_result, $connect);
}
else {
  echo mysql_error();
}

echo $branch[BranchNum];

echo "<table>
        <tr>
           <td>Author</td>
           <td>Title</td>
           <td>Number Available</td>
        </tr>";
while( $row = mysql_fetch_array($result)) {
  echo "<tr>";
  echo "<td>".$row['AuthorFirst'].$row['AuthorLast']."</td>";
  echo "<td>".$row['Title']."</td>";
  echo "<td>".$row['OnHand']."</td>";
  echo "</tr>";
}

echo "</table>";
?>

我觉得我并不完全理解mysql_fetch_array()函数,而且我对变量名做错了,但我不确定。

谢谢!

当我回显$ sql_result时,它显示如下:

SELECT AuthorFirst, AuthorLast, OnHand, Title 
FROM Inventory i, Wrote w, Author a, Book b 
WHERE i.BookCode = b.BookCode AND i.BookCode = w.BookCode AND a.AuthorNum = w.AuthorNum AND '' = i.BranchNum

由于某种原因,变量在这里是无效的......但是当我回显变量本身时它有一个值。

1 个答案:

答案 0 :(得分:0)

查看代码,看起来$ sql_result正在$ branch之前设置。在执行mysql_fetch_array($ branch_result)后尝试定义$ sql_result。将它移动到你有“echo $ branch [BranchNum];”的相同位置看看会发生什么。