Any Suggestions?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
hgrimberg01
Forum Newbie
Posts: 1
Joined: Wed Mar 03, 2010 10:09 pm

Any Suggestions?

Post by hgrimberg01 »

Hi, I am brand new to PHP and MySql. I have had a bit of experience with VB.NET and some exposure to Java but nothing as it relates to databases.

Problem: I am trying to query a field based on a previously retrieved ID from another table in the database. Given the id from the one table(where it is not the primary, but serves as a reference to the primary of another table), I am trying to retrieve the entry associated with that ID. I have tried numerous times but Php still returns parse errors. The top half of the code works, its just the retrieval mechanism for the second part that doesn't.

Code: Select all

 
 
mysql_select_db($database_NHSDatabase, $NHSDatabase);
$query_RecLetter = "SELECT * FROM recletter ORDER BY StudentID ASC";
$RecLetter = mysql_query($query_RecLetter, $NHSDatabase) or die(mysql_error());
$row_RecLetter = mysql_fetch_assoc($RecLetter);
$totalRows_rsRecLetter = mysql_num_rows($rsRecLetter);
 
mysql_select_db($database_NHSDatabase, $NHSDatabase);
$query_StudentName = sprintf("SELECT * FROM students WHERE StudentID='$row_RecLetter['StudentID']'");
$StudentName = mysql_query($query_StudentName, $NHSDatabase) or die(mysql_error());
$row_StudentName = mysql_fetch_assoc($StudentName);
$totalRows_StudentName = mysql_num_rows($StudentName);
 
 
Later in the page, I use this to (try to) display the final result

Code: Select all

<?php echo $row_StudentName['StudentName']; ?>
What am I doing wrong?
Kurby
Forum Commoner
Posts: 63
Joined: Tue Feb 23, 2010 10:51 am

Re: Any Suggestions?

Post by Kurby »

Parse errors? What are they?

Also, you can just do this in mysql using joins (http://dev.mysql.com/doc/refman/5.1/en/join.html)

SELECT students.* FROM students JOIN recletter ON students.StudentID = recletter.StudentID WHERE recletterID = 1

I don't know your column names so the ID columns may be wrong.
Post Reply