Jump to content

Problem with MAX()


seaweed

Recommended Posts

I have a table that uses an auto-increment field, and requires a second auto-increment, which isn't allowed in MySQL. So I tried to query the db for the 2nd variable, grab the highest value and add 1, but it will not work with a $variable in the query string. Any idea why? And before you ask, I need the second auto-increment because the data from this table is being split into two display sections, each needs to be user-sorted with a toggle. Also, $tableselect is passed a value, from the $_POST method.

 

This works:

 

 // 
$maxTable = $_SESSION['username'] . "_projects";

$maxResult = mysql_query("SELECT MAX(project_id) FROM $maxTable")
     or die(mysql_error());

     while($row = mysql_fetch_array( $maxResult )) {
          $maxdata = $row['MAX(project_id)'];
  $maxdata1 = $maxdata + 1;
}

 

This does not work:

 

 // 
$maxTable = $_SESSION['username'] . "_projects";
$maxField = $tableselect . "_id";

$maxResult = mysql_query("SELECT MAX($maxField) FROM $maxTable")
     or die(mysql_error());

     while($row = mysql_fetch_array( $maxResult )) {
          $maxdata = $row['MAX($maxField)'];
  $maxdata1 = $maxdata + 1;
}

Link to comment
Share on other sites

You shouldn't be relying on any auto incrementing field to sort data, auto incrementing fields are designed to keep your data's integrity. maybe if you explain what your trying to do in more detail we could offer a better suggestion or at minimum post and point out the relevant code. i don't see any correlation to your problem in the code you have posted.

Link to comment
Share on other sites

Why wouldn't you sort by an auto-incrementing field? Applications like phpBB and WordPress do it all the time with postID etc. phpBB sorts their memberlist by the auto-incrementing field 'userid' by default.

 

My question is, why does SELECT MAX(project_id) work and SELECT MAX($maxField) not work, when they technically hold the same value?

 

 

Link to comment
Share on other sites

You're saying that a MySQL query cannot except a variable as an argument? That's wrong.

 

Never said that bro

 

But i have to admit i did'int look your code, only your question ad thought you where trying to use a $ var as a current row value ... never mind

Link to comment
Share on other sites

There's various different $maxField options coming in from the previous page via $_POST, so I shove it in a variable and then I want to use that to query all of the rows with that $maxField value, get all of their unique ID's, find the highest value with MAX() and ++ its butt. It's like this:

 

It's a ticketing system, like for a help desk, and there are many different kinds of tickets, four to be exact. I have the page that displays all of the tickets broken into four tabbed sections, because I have four employees, each with their own expertise. So there's a UNIX set of tickets, Mac set, Winblows set and a handheld set. The problem is, if I have all of these tickets in one table, and each ticket has a unique ID, say like '35', and I auto-increment it, these numbers are shared across all four sections. This means the Unix set could have ticker number 35, and the Mac section will never have a 35. I would like each of them to have their own auto-incremented number, so the Unix set could have a ticket #5 an the Windows set have their own Ticket #5... Maybe the auto-increment fields are uid, mid, wid, hid etc... BUT MySQL only allows one auto-increment per row.

 

Man, I hope this makes sense!

 

My opther solution is to just spread the unique ID's across all four sections, but it's my 2nd choice.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.