Hey everyone! :)

I have a line of code that I am not sure about and I get an error message that says,
"Fatal error: Call to undefined function mysql_results() in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test5\func\blog.php on line 33"

when the code is from here in this script:

blog.php

<?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){
	
}

function category_exists($name){
	$name = mysql_real_escape_string($name);
	
	$query = mysql_query("SELECT COUNT(1) FROM `categories` WHERE `name` = `$name`");
	
	return ( mysql_results($query, 0) == 0 ) ? false : true;
}

the code that is in question is here:

return ( mysql_results($query, 0) == 0 ) ? false : true;

any help would be greatly appreciated! :) Thanks!

the error is on the return line that is

return ( mysql_results($query, 0) == 0 ) ? false : true;

should be :

$return = ( mysql_result($query, 0) == 0 ) ? false : true;
return $return;

@jogesh p: I did that and this error showed up:
"Fatal error: Call to undefined function mysql_results() in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test5\func\blog.php on line 33"

As @jogesh pointed out, there is no such function as mysql_results. Drop the s - mysql_result.

Also, mysql_result only returns false on error, otherwise it returns a MySQL result object, even if no rows are found. You would therefore need to use the mysql_num_rows function to determine whether no results were found.

function category_exists($name){
    $name = mysql_real_escape_string($name);
    $query = mysql_query("SELECT COUNT(*) FROM `categories` WHERE `name` = '$name'");
    $result = mysql_result($query);
    return (bool)mysql_num_rows($result);
}

R.

@blocblue: when I changed the code to what you have, it comes up with these errors:

"Warning: mysql_result() expects at least 2 parameters, 1 given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test5\func\blog.php on line 33"

"Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test5\func\blog.php on line 34"

:/

Sorry, my mistake. What you had originally was very nearly correct.

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

The function should by mysql_result and checking that the count value is not equal to zero would determine whether the category exists.

R.

@blocblue: now it says "Fatal error: Can't use function return value in write context in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test5\func\blog.php on line 33"

..I'm getting really tired of these errors.. :/

blocblue's showing off :P it would be simpler to use ...

return mysql_result($query, 0);

... providing the caller can handle FALSE if it's returned.

I'm hoping you've used if(category_exists('blahblah')) ...

@Smeagel: Sorry for the lack of coding knowledge..do you mean like this?

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

basically, I want this function to do as follows: "if 0 rows were returned with category name, return false. otherwise return true"

does that help?

@geneh23 - Please repost your code, indicating which line is line 33, because I have tested the function that I wrote and it works as expected for me, without a fatal error.

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

R.

@blocblue: here is the entire code for blog.php

<?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){

}

function category_exists($name){
    $name = mysql_real_escape_string($name);

    $query = mysql_query("SELECT COUNT(*) FROM `categories` WHERE `name` = `$name`");

    return ! (mysql_result($query,0) = 0);
}

here is where there error is on line 33:

return ! (mysql_result($query,0) = 0);
<?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){
	
}

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

all of code^^^

@blocblue here is all of the code in blog.php

<?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){
	
}

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

and here is where the issue is on line 33

return ! (mysql_result($query,0) = 0);

sorry for the repost..but there it is :)

You've not copied my code correctly. Line 33 should read:

// Notice the double ==
return ! (mysql_result($query,0) == 0);

R

You've not copied my code correctly. Line 33 should read:

// Notice the double ==
return ! (mysql_result($query,0) == 0);

R

@blocblue: I'm sorry, I misread what you put..I added the extra "=" however, now an error shows this:
"Warning: mysql_result() 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 33"

You not copied the previous line correctly either. You have backticks around the name value. They should be single quotation marks instead. Replace your entire category_exists function with the following (copy and paste):

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

R.

commented: this person know's his php! +3

@blocblue: You are right! and there were a couple of other small issues that I noticed. For the mysql queries..in blog.php ...there were " ' " instead of " ` " and I actually needed to add a one to SELECT COUNT () because It's checking to see if there is a record of something in the database and it can't check for something that isn't in there..so therefore it would return false..but you're code helped a lot. As always! Thanks for your help! I'll do the up arrow thing on every post..the same goes for everyone else who attempted to help me :) SOLVED!!

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.