Hey everyone,

I have a function that has an error reading

"Parse error: syntax error, unexpected '}' in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test5\func\blog.php on line 31"

here is the code for that function:

function get_categories($id = null){
	$categories = array();
	
	$query = mysql_query("SELECT `id`, `name` FROM `categories`");
	
	while( $row = mysql_fetch_assoc($query)) {
		$categories = $row
	}
	
	return $categories;
	
}

why is there a syntax error saying that there is an unexpected "}" when it seems as though it looks just fine?..

Recommended Answers

All 19 Replies

Change this:

$categories = $row

with:

$categories = $row;

;D

@cereal: I did that..now it says
"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test5\func\blog.php on line 29"

line 29 is here:

while( $row = mysql_fetch_assoc($query)) {

That means the query is resulting false. Check your connection code, probably you are connecting to the wrong database.

@cereal: I rechecked the database..it's connected :/

Member Avatar for diafol

Are you sure the field names and table name are correct?

@ardav: yep, just triple checked them :)

Member Avatar for diafol

the error message usually points to a mistake in the query. Copy the SQL statement and place it into phpmyadmin. See if it works. If it does, it points to your connection being bogus.

@ardav: I copied and pasted the sql statement and put it into phpmyadmin..it went through displaying the green check mark saying "Showing rows 0 - 0 ( 1 total, Query took 0.0010 sec)"

use..

if(is_resource($query)){
	while( $row = mysql_fetch_assoc($query)) {
		$categories = array('id'=>$row['id'],'name'=>$row['name']);
	}
        return $categories;
} else {
        die(mysql_error());
}

To see what happens!

@raphie: well that other error disapeared but this error showed up:

"Parse error: syntax error, unexpected T_ELSE in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test5\func\blog.php on line 34"

here is that function again and the error shows in line 11 from here instead of 34

function get_categories($id = null){
	$categories = array();
	
	$query = mysql_query("SELECT `id`, `name` FROM `categories`");
	
	while( $row = mysql_fetch_assoc($query)) {
		$categories[] = $row;
	}
	
	return $categories;
} else {
	die(mysql_error;);
}

In your copy there are some errors: 1) here is missing the if statement and 2) inside the die() you wrote mysql_error; while it should be mysql_error() I didn't say to check the connection, because that gives another error, but the database, use this:

$con = mysql_connect("localhost","user","password");
mysql_select_db('your_database') or die("There was an error connecting to the table");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

bye.

...damn...either I did this wrong or I don't know what...now it says I have an error on line 47...
"Parse error: syntax error, unexpected $end in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test5\func\blog.php on line 47"

<?php
include_once('../resources/config.php');

function add_post($title, $contents, $category) {
	
}

function edit_post($id, $title, $contents, $category){
	
}

function add_category($name){
	$name = mysql_real_escape_string($name);
	
	mysql_query("INSERT INTO `categories` SET `name` = '$name'");
}

function delete($field, $id){
	
}

function get_posts($id = null, $cat_id = null){
	
}

function get_categories($id = null){
	$categories = array();
	
	$query = mysql_query("SELECT `id`, `name` FROM `categories`");
	
	while( $row = mysql_fetch_assoc($query)) {
		$categories[] = $row;
	}
	
	return $categories;
	$con = mysql_connect("127.0.0.1","root","");
	mysql_select_db('blog') or die("There was an error connecting to the table");
	if (!$con)
	{
    die('Could not connect: ' . mysql_error());
	}

function category_exists($name){
    $name = mysql_real_escape_string($name);
    $query = mysql_query("SELECT COUNT(1) FROM `categories` WHERE `name` = '$name'");
    return ! (mysql_result($query, 0) == 0);
}

Sorry.. you need to put the code I suggest inside the function after your $query, you can only use else with an if statement.

function get_categories($id = null){
        //YOU NEED A CONNECTION CODE HERE AS MENTIONED BY cereal
        $con = mysql_connect("localhost","user","password");
        mysql_select_db('your_database')
        or die("There was an error connecting to the table");
	
        $categories = array();
	
	$query = mysql_query("SELECT `id`, `name` FROM `categories`");
	
        if(is_resource($query)){
	     while( $row = mysql_fetch_assoc($query)) {
		     $categories[] = $row;
	     }
	
	     return $categories;
       } else {
	     die(mysql_error());
       }

}

The error at line 47 is a missing } that's on your get categories, is all wrong... follow the code I post before this one.

yeah, sorry I messed it up..the error declared in mysql_error() says on the page that there was an error connecting to the table..I'm assuming this is what @ardav was talking about earlier..

Yes.. you didn't have a connection inside the function, Any connection outside a function does not affect the function itself, the function has to have it same connection.

The best thing I suggest you to create a class with the connection then call the class whenever you need it instead of coding connection all the time.

what I'm wondering is why is it not connecting to my table..it's connected to the database and it should be connected to the table because I was able to insert a "Test" category in the text field for the add_category.php page...it should work..

what if I add the word "AND" in between `id` and `name` in the query:
"SELECT `id`, `name` FROM `categories`"

would that make any difference?

there has to be a reason why the table isn't showing..unless the query is wrong..I want the query code to collect the name variable in the categories table from the blog database..I thought that's what I basically put.. hmm

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.