2

As of now I have no errors in my program, but I need the primary key for one of the tables for a relation for the following Query. but instead of getting a actual number the value the query is sending back is

Resource id #4

Here is my Code: (The query that I'm having issues with is the $sql_branch, is there a function to change the result from "Resource id #4" to just 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>";
?>

Thanks!

3
  • $branch is still an array, you'll need to convert it. Commented Oct 26, 2010 at 23:08
  • Strictly speaking it's not an array, but a link to an external resource. You can't iterate over it or perform other array functions on it. au.php.net/manual/en/function.mysql-query.php Commented Oct 26, 2010 at 23:17
  • my mistake, i tried to edit but it had passed the threshold already. :P Commented Oct 26, 2010 at 23:19

1 Answer 1

5

You are not pulling results from the mysql_query. Try this:

if($branch_result = mysql_query($sql_branch, $connect)) {
  $branch = mysql_fetch_array($branch_result);
}
Sign up to request clarification or add additional context in comments.

3 Comments

No prob. I often need a fresh set of eyes over my code to see the glaringly obvious that I am too close to see.
as for in the query I'm guessing I'd put the result in as <code>'$branch[BranchNum]'</code>
@rajh2504: That's right. Since you're fetching an associative array with mysql_fetch_array() the field names become the array keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.