0

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!

4
Contributors
18
Replies
19
Views
6 Years
Discussion Span
Last Post by geneh23
Featured Replies
  • the error is on the return line that is [code=php] return ( mysql_results($query, 0) == 0 ) ? false : true; [/code] should be : [code=php] $return = ( mysql_result($query, 0) == 0 ) ? false : true; return $return; [/code] Read More

  • Sorry, my mistake. What you had originally was very nearly correct. [CODE] 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); } [/CODE] The function should by [B]mysql_result[/B] and checking that the count value is [B]not[/B] equal to … Read More

  • @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. [CODE] function category_exists($name){ $name = mysql_real_escape_string($name); $query = mysql_query("SELECT COUNT(*) FROM `categories` WHERE `name` = '$name'"); return ! … Read More

  • You've not copied my code correctly. Line 33 should read: [CODE] // Notice the double == return ! (mysql_result($query,0) == 0); [/CODE] R Read More

  • 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): [CODE] function category_exists($name){ $name = mysql_real_escape_string($name); $query = mysql_query("SELECT COUNT(*) FROM `categories` WHERE `name` = '$name'"); return … Read More

1

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;

Edited by jogesh_p: n/a

0

@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"

0

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.

Edited by blocblue: n/a

0

@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"

:/

1

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.

0

@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.. :/

0

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')) ...

Edited by Smeagel13: n/a

0

@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?

1

@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.

0

@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);

Edited by diafol: fixed formatting

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

0

@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);
0

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

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

R

1

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

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

R

0

@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"

1

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.

Votes + Comments
this person know's his php!
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.