...

View Full Version : Function/While loop error



crono.Serge
08-22-2006, 12:22 AM
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



#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!

marek_mar
08-22-2006, 12:26 AM
You are missing a semicolon before the line that starts with "function".

crono.Serge
08-22-2006, 12:31 AM
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

Fumigator
08-22-2006, 12:50 AM
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.


$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();
}

crono.Serge
08-22-2006, 01:07 AM
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?

Fumigator
08-22-2006, 01:11 AM
$result isn't something you can echo. Try putting in the code in my previous post and see what kind of output you get.

crono.Serge
08-22-2006, 01:15 AM
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

Fumigator
08-22-2006, 01:16 AM
$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?

crono.Serge
08-22-2006, 01:19 AM
S&#237;, to the letter


#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();

Fumigator
08-22-2006, 01:22 AM
You'll need to post your latest code then. You're not giving enough info to help you with.

crono.Serge
08-22-2006, 01:31 AM
#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();

Fumigator
08-22-2006, 05:00 AM
I can't find anything wrong; try uncommenting the bottom part and see if it works now.

crono.Serge
08-22-2006, 10:51 PM
I keep getting the same error. What is wrong with my code??? :confused:


<!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>

crono.Serge
08-22-2006, 11:01 PM
Wait..nevermind, I got it...thanks Fumigator!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum