Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function/While loop error

    Hi, again, guys. I'm trying get some insight as to where I'm going wrong. I'm trying to create a function (it might not be necesarry, but I'm learning), that spits out 2 columns of my table chronocross, and spit out each row, until the all the rows are counted for. I think I'm doing this right, and might be missing something, since I keep getting the following error:
    Parse error: syntax error, unexpected T_FUNCTION in c:\Server\apache group\Apache2\Apache\htdocs\chrono cross\characters.php
    Code:
    	#connect to database using variables, select database, and query for results
    	$link = mysql_connect (DB_connection,DB_user,DB_password) OR die ('Error connecting to MySQL: '.mysqli_connect_error());
    	mysql_select_db ("chronocross");
    	$query = "SELECT * FROM characters";
    	
    	#query result(s)
    	$result = mysql_query($query)
    	
    	function character_display()//function to display amount of rows
    	{
    		$num = mysql_num_rows($result);
    		
    		if ($num < 0)//no rows
    		{
    			echo "<style=\"font:Arial;color:#990000;\">";
    		}
    		else
    		{
    			#while loop to go through amount of rows
    			while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
    			{
    				echo'<tr><td>'.$row['image'].'</td></tr>
    				<tr><td>'.$row['name'].'</td></tr>';
    			}
    		}
    	}
    	mysql_close();
    Any pointers and help is greatly appreciated. Many thanks in advance!

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You are missing a semicolon before the line that starts with "function".
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    New Coder
    Join Date
    Mar 2006
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That just gave me a new error
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\Server\apache group\Apache2\Apache\htdocs\chrono cross\characters.php

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\Server\apache group\Apache2\Apache\htdocs\chrono cross\characters.php

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You have an error in the mysql_query() function itself, but you aren't bothering to check to see if it worked before you try to use it in other functions (mysql_num_rows() and mysql_fetch_array()), so you are seeing that error "not a valid resource" on those two functions.

    Try checking the mysql_query() to see if it worked.
    PHP Code:
    $result mysql_query($query);
    if (!
    $result) {
        print 
    "AHHHHHH!!!!!!!!!!!!!!  QUERY ERROR QUERY ERROR!!!!!<br>\n";
        print 
    "SHUT EVERYTHING DOWN!!!! AAHHHHHHHHHHHH!!!<br>\n";
        print 
    "query: $query<br>\n";
        print 
    mysql_error();
        die();


  • #5
    New Coder
    Join Date
    Mar 2006
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If I echo the $result, I get a Resource ID #3, and if I don't echo it, I just get a blank page. Sooooo.....I'm confused, is my query not working?

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    $result isn't something you can echo. Try putting in the code in my previous post and see what kind of output you get.

  • #7
    New Coder
    Join Date
    Mar 2006
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, ok...sorry, did'nt know that. I did that, took out the echo, and all I get is a blank screen. No output what-so-ever

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    $result isn't something you can echo. Try putting in the code in my previous post and see what kind of output you get.
    Did you do this?

  • #9
    New Coder
    Join Date
    Mar 2006
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    S&#237;, to the letter
    Code:
    				#connect to database using variables, select database, and query for results
    				$link = mysql_connect (DB_connection,DB_user,DB_password) OR die ('Error connecting to MySQL: '.mysqli_connect_error());
    				mysql_select_db ("chronocross");
    				$query = "SELECT * FROM characters";
    				
    				#query result(s)
    				$result = mysql_query($query);
    				if (!$result)
    				{
    					print "AHHHHHH!!!!!!!!!!!!!!  QUERY ERROR QUERY ERROR!!!!!<br>\n";
    					print "SHUT EVERYTHING DOWN!!!! AAHHHHHHHHHHHH!!!<br>\n";
    					print "query: $query<br>\n";
    					print mysql_error();
    					die();
    				}  
    				
    				/*function character_display()//function to display amount of rows
    				{
    					$num = mysql_num_rows($result);
    					
    					if ($num < 0)//no rows
    					{
    						echo "<style=\"font:Arial;color:#990000;\">";
    					}
    					else
    					{
    						#while loop to go through amount of rows
    						while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
    						{
    							echo'<tr><td>'.$row['image'].'</td></tr>
    							<tr><td>'.$row['name'].'</td></tr>';
    						}
    					}
    				}*/
    				mysql_close();
    Last edited by crono.Serge; 08-22-2006 at 01:23 AM.

  • #10
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You'll need to post your latest code then. You're not giving enough info to help you with.

  • #11
    New Coder
    Join Date
    Mar 2006
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    				#connect to database using variables, select database, and query for results
    				$link = mysql_connect (DB_connection,DB_user,DB_password) OR die ('Error connecting to MySQL: '.mysqli_connect_error());
    				mysql_select_db ("chronocross");
    				$query = "SELECT * FROM characters";
    				
    				#query result(s)
    				$result = mysql_query($query);
    				if (!$result)
    				{
    					print "AHHHHHH!!!!!!!!!!!!!!  QUERY ERROR QUERY ERROR!!!!!<br>\n";
    					print "SHUT EVERYTHING DOWN!!!! AAHHHHHHHHHHHH!!!<br>\n";
    					print "query: $query<br>\n";
    					print mysql_error();
    					die();
    				}  
    				
    				/*function character_display()//function to display amount of rows
    				{
    					$num = mysql_num_rows($result);
    					
    					if ($num < 0)//no rows
    					{
    						echo "<style=\"font:Arial;color:#990000;\">";
    					}
    					else
    					{
    						#while loop to go through amount of rows
    						while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
    						{
    							echo'<tr><td>'.$row['image'].'</td></tr>
    							<tr><td>'.$row['name'].'</td></tr>';
    						}
    					}
    				}*/
    				mysql_close();

  • #12
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I can't find anything wrong; try uncommenting the bottom part and see if it works now.

  • #13
    New Coder
    Join Date
    Mar 2006
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I keep getting the same error. What is wrong with my code???
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Character page</title>
    </head>
    <?php				
    	#set variables to connect to database
    	define ('DB_connection');
    	define ('DB_user');
    	define ('DB_password');
    	
    	#connect to database using variables, select database, and query for results
    	$link = mysql_connect (DB_connection,DB_user,DB_password) OR die ('Error connecting to MySQL: '.mysqli_connect_error());
    	mysql_select_db ("chronocross");
    	$query = "SELECT * FROM characters";
    	
    	#query result(s)
    	$result = mysql_query($query);
    	if (!$result)
    	{
    		print "AHHHHHH!!!!!!!!!!!!!!  QUERY ERROR QUERY ERROR!!!!!<br>\n";
    		print "SHUT EVERYTHING DOWN!!!! AAHHHHHHHHHHHH!!!<br>\n";
    		print "query: $query<br>\n";
    		print mysql_error();
    		die();
    	}  
    	
    	function character_display()//function to display amount of rows
    	{
    		$num = mysql_num_rows($result);
    		
    		if ($num < 0)//no rows
    		{
    			echo "<style=\"font:Arial;color:#990000;\">";
    		}
    		else
    		{
    			#while loop to go through amount of rows
    			while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
    			{
    				echo'<tr><td>'.$row['image'].'</td></tr>
    				<tr><td>'.$row['name'].'</td></tr>';
    			}
    		}
    	}
    	mysql_close();
    ?>
    <body>
    <table align="center">
    	<tr>
    	<!--?php character_display();?-->
    		<td>
    			<table>
    				<?php character_display();?>
    			</table>
    		</td>
    	</tr>
    </table>
    </body>
    </html>

  • #14
    New Coder
    Join Date
    Mar 2006
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wait..nevermind, I got it...thanks Fumigator!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •