CrownVictoriaCop Posted September 27, 2009 Share Posted September 27, 2009 Hello again guys, I'm using this code to retrieve information from a MySQL Database. <?php $username="safetyfi_secure"; $password="HIDDEN"; $database="safetyfi_students"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM tablename"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); echo "<b><center>Database Output</center></b><br><br>"; $i=0; while ($i < $num) { $field1-name = mysql_result($result,$i,"ID"); $field2-name = mysql_result($result,$i,"ParentFirstName"); $field3-name = mysql_result($result,$i,"ParentLastName"); $field4-name = mysql_result($result,$i,"StudentName"); $field5-name = mysql_result($result,$i,"Class"); $field6-name = mysql_result($result,$i,"Address"); $field7-name = mysql_result($result,$i,"City"); $field8-name = mysql_result($result,$i,"State"); $field9-name = mysql_result($result,$i,"Zip"); $field10-name = mysql_result($result,$i,"Phone"); $field11-name = mysql_result($result,$i,"BirthMonth"); $field12-name = mysql_result($result,$i,"BirthDay"); $field13-name = mysql_result($result,$i,"BirthYear"); $field14-name = mysql_result($result,$i,"School"); $field15-name = mysql_result($result,$i,"PaymentMethod"); echo "Student ID: $field1-name<p>Enrolled in Class: $field5-name</p> <p>$field4-name $field3-name</p> <p>$field14-name</p> <p>$field6-name</p> <p>$field7-name, WI $field9-name</p> <p>$field10-name</p> <p>Parent Name: $field2-name $field3-name</p> <p>Payment Method: $field15-name</p> <hr>"; $i++; } ?> When using it, I'm getting the error Parse error: syntax error, unexpected '=' in /home2/safetyfi/public_html/secure/directory.php on line 20 Can anyone tell me how I could fix this? Thanks, Anthony Quote Link to comment https://forums.phpfreaks.com/topic/175734-solved-unexpected/ Share on other sites More sharing options...
smerny Posted September 27, 2009 Share Posted September 27, 2009 try changing $field1-name to $field1_name, etc Quote Link to comment https://forums.phpfreaks.com/topic/175734-solved-unexpected/#findComment-926082 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 Fixes the error, but now no info from the database is being displayed. try changing $field1-name to $field1_name, etc Quote Link to comment https://forums.phpfreaks.com/topic/175734-solved-unexpected/#findComment-926083 Share on other sites More sharing options...
smerny Posted September 27, 2009 Share Posted September 27, 2009 you changed the variable names for the output as well, correct? what are you seeing exactly? Quote Link to comment https://forums.phpfreaks.com/topic/175734-solved-unexpected/#findComment-926084 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 The Words "Database Output". I don't think I did. I changed some things in the script in hopes of making it working. <?php $username="safetyfi_secure"; $password="Hidden"; $database="safetyfi_students"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM Students"; $result=mysql_query($query); mysql_close(); echo "<b><center>Database Output</center></b><br><br>"; $i=0; while ($i < $num) { $field1_name = mysql_result($result,$i,"ID"); $field2_name = mysql_result($result,$i,"ParentFirstName"); $field3_name = mysql_result($result,$i,"ParentLastName"); $field4_name = mysql_result($result,$i,"StudentName"); $field5_name = mysql_result($result,$i,"Class"); $field6_name = mysql_result($result,$i,"Address"); $field7_name = mysql_result($result,$i,"City"); $field8_name = mysql_result($result,$i,"State"); $field9_name = mysql_result($result,$i,"Zip"); $field10_name = mysql_result($result,$i,"Phone"); $field11_name = mysql_result($result,$i,"BirthMonth"); $field12_name = mysql_result($result,$i,"BirthDay"); $field13_name = mysql_result($result,$i,"BirthYear"); $field14_name = mysql_result($result,$i,"School"); $field15_name = mysql_result($result,$i,"PaymentMethod"); echo "Student ID: $field1_name<br>Enrolled in Class: $field5_name <br>$field4_name $field3_name <br>$field14_name <br>$field6_name <br>$field7_name, WI $field9_name <br>$field10_name <br>Parent Name: $field2_name $field3_name <br>Payment Method: $field15_name <hr>"; $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/175734-solved-unexpected/#findComment-926086 Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 mysql_numrows should be mysql_num_rows Quote Link to comment https://forums.phpfreaks.com/topic/175734-solved-unexpected/#findComment-926087 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 mysql_numrows should be mysql_num_rows I got rid of that part, simply because it displays the number of rows. Quote Link to comment https://forums.phpfreaks.com/topic/175734-solved-unexpected/#findComment-926088 Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 well that part was also running your loop $num=mysql_numrows($result); [...] while ($i now you don't have a $num to be greater than $i Also..there is a tag for displaying code Quote Link to comment https://forums.phpfreaks.com/topic/175734-solved-unexpected/#findComment-926090 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/safetyfi/public_html/secure/directory.php on line 11 That's the error that comes up now. well that part was also running your loop $num=mysql_numrows($result); [...] while ($i < $num) { now you don't have a $num to be greater than $i Also..there is a tag for displaying code Quote Link to comment https://forums.phpfreaks.com/topic/175734-solved-unexpected/#findComment-926094 Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 When you get that error it means you need to do this $result=mysql_query($query) or die(mysql_error()); it will tell you why $result is not a valid mysql result resource Quote Link to comment https://forums.phpfreaks.com/topic/175734-solved-unexpected/#findComment-926095 Share on other sites More sharing options...
CrownVictoriaCop Posted September 27, 2009 Author Share Posted September 27, 2009 When you get that error it means you need to do this $result=mysql_query($query) or die(mysql_error()); it will tell you why $result is not a valid mysql result resource Found out the error and fixed it. Thank you guys again for helping a n00b! Quote Link to comment https://forums.phpfreaks.com/topic/175734-solved-unexpected/#findComment-926100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.