Use of undefined constant mysql_error

Hi, I am receiving this error and I’m tearing my hair out because I cannot seem to fix it. It isn’t even part of the final system I just want to test this functions ability to DELETE categories if they already exist but its just throwing up problem after problem that I can’t seem to get to the source of.

For the sake of this post I’ve removed my echo statements. I’m deliberately passing numbers to control the effects but its just bizarre. Posting 5 as I am now, it WAS just throwing up a “Use of undefined constant mysql_error”. I removed the error reporting to get beyond that part but there is still an error there it doesn’t like.

Up until the data is entered however, post_ID shows up as 5 so the information at least is being correctly given but mySQL doesn’t like it.

Then I get to my delete function which compares an array of the current IDs from the database against the new ones. It is meant to remove the difference but $deleteCat has no content after using array_diff. I’ve tried a count and echoing it and it gives off ‘0’ and “Array”. In the database they are stored as INT so I’m assuming they’re the same type but it’s finding no differences so it won’t delete anything.

I’m honestly stumped. Even if I go back to where this function is called, which passes as $post_ID “$post_ID = (int) mysql_insert_id();” and change that to 5 (my test post) it still throws up an error. I’ve tried $post_ID = (int) 5;

I’ve also echoed the results from the database and it is retrieving the CURRENT ids correctly.

I really just don’t know what the issue is, it is probably (hopefully) something simple but it just doesn’t want to work for me. I’ve been at this for about 3 hours. Anyone see what the issue is?

function setRelationships($post_ID, $post_categories) {
	
		
	$post_categories = array (1, 3, 8, 9);
	$post_ID = 5;
	if ( !is_array($post_categories) ) {
		$post_categories = array($post_categories);
	}
	$result = mysql_query("SELECT category_id FROM relationships WHERE post_id = $post_ID");	
		if($result) {
			while($row = mysql_fetch_array($result)){
			$currentCategories = array($row['category_id']);
				
			}
			
		} else { 
				$currentCategories = array(); 
				}

	foreach ((array) $post_categories as $term) {
		if (!strlen(trim($term)) )
			continue;
			mysql_query("INSERT INTO relationships (post_id, category_id) VALUES ($post_ID, $term)");
		
	}
	updateCategoryCount($post_categories); //UPDATE COUNT OF CATEGORIES
	
	if($currentCategories) {
		$deleteCat = array_diff($currentCategories, $post_categories);
				if($deleteCat) {
				
					$deleteIN = "'" . implode("','", $deleteCat)."'";
					 mysql_query("DELETE FROM relationships WHERE post_id=$post_ID AND category_id IN ($deleteIN)");	
			
				}
	
	}
}

Nothing jumps out at me. When debugging arrays (or objects) use var_dump($myArray) instead of echo. You’ll see what’s actually in it then. So you can see what $deleteCat is, and the final DELETE query you are sending.

As for the earlier undefined constant error, were you perhaps calling mysql_error instead of mysql_error() ?

I was doing or die(mysql_error)

Which now that you point it out, I believe is wrong -_-

I hate PHP, some things work like a charm, other things you’re on hour 5 trying to get it to work. I’ve sorted out one issue but still cannot see why deleteCat won’t work.

Thanks for the var_dump advice, I’ll give that a go, see what happens.

The () makes it a function call. So die( mysql_error() ) is calling the mysql_error function (which returns a string) and ending the script with that message.

PHP assumes something without $ or () is a constant (i.e. not a variable or function), hence your error.


define('mysql_error', 'Now mysql_error is a constant, and this is its value');

Ok this is the code I’m using to retrieve the current category listing.

$result = mysql_query("SELECT category_id FROM relationships WHERE post_id = $post_ID");	
	$row = mysql_fetch_array($result);
		if(!$row) {
			$currentCategories = array();
		} else {
			while($row = mysql_fetch_array($result)){
				$currentCategories = array($row['category_id']);
			}
			
			var_dump($currentCategories);
			
		}

And this is what Vardump is returning.

{ [0]=> string(1) “9” }

Which definitely isn’t right. Well the 9 is. Plus doing it this way I’m pretty sure will remove the first element of the array by doing mysql_fetch_array twice. I’m stumped on how to go about it.

The function itself does work however, I set the array to digits and it deleted the right records, but any suggestion on how best to retrieve the rows properly? I’m declaring an empty array of $currentCategories if no result is returned because otherwise the later check throws an error but if it helps I can just declare it elsewhere.


$result = mysql_query("SELECT category_id FROM relationships WHERE post_id = $post_ID");   

  // why is the first row being removed? - this does not seem right...
    $row = mysql_fetch_array($result);
        if(!$row) {
            $currentCategories = array();
        } else {
            while($row = mysql_fetch_array($result)){
                $currentCategories[] /* notice what was added */ = $row;
            }
           
            var_dump($currentCategories);
           
        }

Thanks Oddz, that works almost perfectly.

Gives me this:

{ [0]=> array(2) { [0]=> string(1) “3” [“category_id”]=> string(1) “3” } [1]=> array(2) { [0]=> string(1) “8” [“category_id”]=> string(1) “8” } [2]=> array(2) { [0]=> string(1) “9” [“category_id”]=> string(1) “9” } }

But it isn’t including the first result.
The numbers of the record this is retrieving are 1, 3, 8 and 9 but if I’m reading that vardump correctly, it’s returning only 3, 8 and 9.

If I echo $row[0] directly after assigning it to mysql_fetch_array it displays the ‘1’ so the ‘1’ is going missing somewhere between there and your addition.

I know I had this issue once before where I had two lines saying $row=mysql_fetch_array. Would it be better if I just drop the IF part and the first $row= and declare $currentCategories an array elsewhere?

Ok I think I’ve finally solved it.

I took oddz code and just added in ‘category_id’
$currentCategories = $row[‘category_id’];

At the moment at least it is doing what I want, adding new categories, deleting the difference between old and new. Thanks for the help guys.


$result = mysql_query("SELECT category_id FROM relationships WHERE post_id = $post_ID");   

  // why is the first row being removed? - this does not seem right...
    $row = mysql_fetch_array($result);
        if(!$row) {
            $currentCategories = array();
        } else {
            while($row = mysql_fetch_assoc($result)){
                $currentCategories[] /* notice what was added */ = $row;
            }
           
            var_dump($currentCategories);
           
        } 

Not sure. I just know I had a similar issue in the past, I assumed that the first row gets stored to the first $row declaration and then the WHILE $row assigns $result again but with the first $row already having been assigned.

Honestly though I have no definite clue.

This is what I’m using right now and it seems to be working fine.

	$result = mysql_query("SELECT category_id FROM relationships WHERE post_id = $post_ID");	
			while($row = mysql_fetch_array($result)){
				$currentCategories[] = $row['category_id'];
			}