Undefined function

I keep getting undefined function error from these two pages:

<?php 
include_once('resources/init.php');?>
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
   
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
   
    <title> Category List </title>
</head>

<body>
    <?php
	foreach ( get_categories() as $category ) {
		echo $category['name'] . '<br/>';
	}
	?>
</body>
</html>
<?php
include_once('resources/init.php');

if ( isset($_POST['name']) ) {
	$name = trim($_POST['name']);
	
	if ( empty($name) ) {
		$error ='You must submit a category name. ';
	} else if ( category_exists($name) ) {
		$error = 'That category already exists. ';
	} else if (strlen($name) > 24 ) {
		$error = 'Category names can only be up to 24 characters.';
	}
	
	if ( ! isset($error) ) {
		add_category($name);
	}
}
?>
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    
    <title> Add a Category </title>
</head>

<body>
    <h1> Add a Category </h1>
	
	<?PHP
	if (isset($error) ) {
		echo "<p> {$error} </p>\
";
	?>
    
    <form action="" method="post">
        <div>
            <label for="name"> Name </label>
            <input type="text" name="name" value="">
        </div>
        <div>
            <input type="submit" value="Add Category">
        </div>
    </form>
</body>
</html>

both should be pulling the information from following page but I don’t think they are connected properly:

<? 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;
}

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' ) ? false : true;

}
?>

Please post the exact error you’re getting

For sanity’s sake, temporarily place a simple echo statement in init.php.


<?php 
echo 'EEK!';

If the inclusion went OK, you should see that echo’ed text in the source code of your html page.

Fatal error: Call to undefined function get_categories() in C:\xampp\htdocs\ est\blog\category_list.php on line 18

Fatal error: Call to undefined function category_exists() in C:\xampp\htdocs\ est\blog\add_category.php on line 9

it’ echoed ok

In the last snippet of code you posted your init.php file? If so your opening PHP tag has a space in it which would cause it to output as plain text instead of running as PHP code, if you remove the space the error should stop.

that did not work

You didn’t answer my initial question, i asked if the code you posted above exists in the init.php file as if it doesn’t that will also cause the error. I double checked the function names and they are both spelled the same way.

sorry about that here is the inti.php code

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

mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME);

include_once('func/blog.php');

now remove the echo test from init and put it in config.php, if that appears in the source code of your page, then move it to blog.php and test it again.

If all of that works then you have proved that all the dependent files are wired up correctly (unless you have another level of includes in them as well :wink: ).

So then move the EEK! into your suspect function.

ok cool it is not showing up when I put it in the blog.php file

At the start of the third script in the OP (which I presume is resources/init.php) it starts with <? php try removing that space (if the server has got the allowing of short tags disabled it may be causing a problem.

after fixing the space issue and a couple of other small things i get this error:

Fatal error: Call to undefined function get_post() in index.php on line 4

here is the php for that:


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

$posts = get_post();
?>


Going by the same script (3rd one in the OP) shouldn’t the line

$posts = get_post();

be

$posts = get_posts():

?

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\ est\blog\resources\func\blog.php on line 43

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ est\blog\index.php on line 25

blog:


<?php
function add_post($title, $contents, $category) {
     $title                = mysql_real_escape_string($title);
     $contents           = mysql_real_escape_string($contents);
     $category           = (int)$category;
    
     mysql_query("INSERT INTO 'post' SET
                                        'cat_id'                     = {$category},
                                        'title'                         = '{$title}',
                                        'contents'                = '{$contents}',
                                        'date_posted'      = NOW()")
                                        or die(mysql_error());
}

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($table, $id) {
     $table = mysql_real_escape_string($table);
     $id = (int) $id;
    
     mysql_query("DELETE FROM '{$table}' WHERE 'id' = {$id}");
}

function get_posts($id = null, $cat_id = null) {
     $post = array();

     $query = "SELECT 'posts' . 'id' AS 'post_id', 'categories . 'id' AS 'category_id',
                                        'title', 'contents', 'date_posted', 'categories' . 'name'
                         FROM 'post'
                         INNER JOIN 'categories' ON 'categories' . 'id' = 'post' . 'cat_id'
                         ORDER BY 'id' DESC";
                        
     $query = mysql_query($query);
    
     while ( $row = mysql_fetch_assoc($query) ) {
               $posts[] = $row;
     }
    
     return $posts;
}

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;
}

function category_exists($field, $value) {
     $field = mysql_real_escape_string($field);
     $value = mysql_real_ecape_string($value);
    
     $query = mysql_query("SELECT COUNT(1) FROM 'categories' WHERE '{$field}' = '{$value}' ");
    
     return ( mysql_result($query, 0) == '0' ) ? false : true;

}
?>

index:


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

$posts = get_posts();
?>
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  
    <style>
    ul { list-style-type: none; }
    li { display: inline; margin-right: 20px; }
    </style>
  
    <title> My Blog </title>
    
     <?php
     foreach ( $posts as $post) {
         
     }
     ?>
</head>

<body>
    <nav>
        <ul>
            <li><a href="index.php"> Index </a></li>
            <li><a href="add_post.php"> Add a Post </a></li>
            <li><a href="add_category.php"> Add a Category </a></li>
            <li><a href="category_list.php"> Category List </a></li>
        </ul>
    </nav>
  
    <h1> Devin's Awesome Blog </h1>
   
   
</body>
</html>

thanks SpacePhoenix after posting it I quickly fixed it

The first error indicates that an sql query has failed, echo the query to see if anything looks out of place or is missing.

The second error relates to the first as foreach was expecting an array but as the query that would populate the array failed it didn’t get what it expected. Fixing the cause of the first error should automatically fix that error.

I echoed out $row and $posts nothing shows up

I have to update my version of php after that I will check for more errors