SQL查询的PHP输出

时间:2010-10-26 23:02:18

标签: php sql mysql

到目前为止,我的程序中没有错误,但是我需要其中一个表的主键用于以下Query的关系。但是,查询发回的值不是获取实际数字,而是

Resource id #4

这是我的代码:(我遇到的问题是$ sql_branch,是否有一个函数将结果从“资源ID#4”更改为4?

$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 i.BranchNum = 1";


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

mysql_select_db('henrybooks', $connect);

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

if(mysql_query($sql_result, $connect)) {
  $result = mysql_query($sql_result, $connect);
}
else {
  echo mysql_error();
}
echo $branch."<br>";
echo $sql_branch."<br>";
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>";
?>

谢谢!

1 个答案:

答案 0 :(得分:5)

你没有从mysql_query中提取结果。试试这个:

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