That code doesn't work..right. What it gives me is this:

Say $storyfile = local

it will give me an output file named:

local1.html

So, it is giving me a "1" as the ID. Not the next ID in the database.

So, to answer your question, it is a auto-increment field. There will only be one person adding to the database at a time. So, it doesn't matter to me how I get the ID of the story I am adding.

    hmm, ok... I am not sure why it isn't working but here's what I'd do to debug it:

    1. try your SQL statement directly on the db (use "phpmyadmin", the mysql commadn line tool in unix, or something similar)... just to get PHP out of the equation and isolate the SQL part of the problem. (first rule of debugging: simplify! get rid of anything you can to really get to the root of the problem!)

    2. read the mysql documentation tob e sure MAX() works on auto-increment fields (...I'm guessing this is the problem; try your SQL statement on a column that isn't auto_increment to test that theory)

    3. if all else fails, you may need to go ahead and do as I suggested with inserting the row, getting the ID and then updating the row with the real information.

    best
    Eric

      So, I tested this query string in PHPMyAdmin and it returned the value I am looking for.

      So, I know this string will work on an Auto_Incrementing field.

      What do you suggest now?

      If I do grab the id after it is added...I have no idea how to do that.

      Help!

        dude, you're killin' me here.

        is this really your whole function? I glanced at it so quickly earlier I can't believe I missed the obvious-- or that you missed the obvious ;-)

        function storyid() 
        { 
          $maxID = "SELECT max(id) FROM stories"; 
          $newID = $maxID + 1; 
          return $newID; 
        }
        

        OK, what's wrong with this picture, hmm?

          I am not sure..because I am just now getting to know how functions work. So, I do not fully understand there concepts and how they are put together.

          :-(

            I think you probably should go back and do some basic PHP and database tutorials-- you have good ideas and know what you're aiming for, but you need a little more coding experience to accomplish the task. if the problem in the code below doesn't leap out at you, check out these articles; you'll probably get the most mileage out of parts 3, 4 and 5 (ESPECIALLY part 4):

            http://www.devshed.com/Server_Side/PHP/PHP101

            function storyid() 
            { 
              $maxID = "SELECT max(id) FROM stories"; 
              $newID = $maxID + 1; 
              return $newID; 
            }
            

              Like pnoeric said, you really need to go back to basics, but the basic solution would be (simplied as well):

              function storyid() 
              { 
                 $query = mysql_query("select max(id) as maxid FROM stories"); 
                 return (mysql_result($query,0,"maxid"))+1;
              }
              

              Edit
              Forgot to make it increment by 1.

                Well, that finally worked.

                I have another question regarding this.

                I changed the function for use with the photos that are added with stories. I do not knw how to add this function into my code, for use in naming conventions. Here is what I have, how would I change this to work?

                exec("cp $custom3 /tmp/$custom3_name");

                $tmpimg = "/tmp/$custom3_name";
                $newfile = "/projects/sunarchive/htdocs/image/";
                $newphoto = "$newfile . photoid() . '.jpg'";

                $new_w=140;

                $new_h=191;

                system("convert -quality 80 -antialias -sample '$new_wx$new_h' '$tmpimg' '$newphoto'");

                  quote from manual regarding max() function

                  Don't use this feature if the columns you omit from the GROUP BY part aren't unique in the group! You will get unpredictable results.

                    have u ever tried:
                    function storyid()
                    {
                    $maxID = "SELECT max(id) FROM stories";
                    $newID = $maxID + 1;
                    echo $newID;
                    }

                    ??????????????????????????

                      I am actually using this function:

                      function photoid() 
                      { 
                         $query = mysql_query("select max(id) as maxid FROM stories"); 
                         return (mysql_result($query,0,"maxid"))+1;
                      }
                      

                      And the function works fine. I just need a way to put this function in to define the name of the file after it is converted. This is what I have, and it doesn't work:

                      exec("cp $custom3 /tmp/$custom3_name"); 
                      
                      $tmpimg = "/tmp/$custom3_name"; 
                      $newfile = "/projects/sunarchive/htdocs/image/"; 
                      $newphoto = "$newfile . photoid() . '.jpg'"; 
                      
                      $new_w=140; 
                      $new_h=191; 
                      
                      system("convert -quality 80 -antialias -sample '$new_wx$new_h' '$tmpimg' '$newphoto'");
                      

                        That function makes no sense which is why I posted up one that does.

                        For starters theres no mysql_query in it.

                        It doesn't matter that group by isn't being used, it appears he wants the latest ID of a story, in which case id will be an auto. inc. field thats unique.

                          The function works fine. I know it does because it works in this context:

                          function storyid()
                          {
                          $query = mysql_query("select max(id) as maxid FROM stories");
                          return (mysql_result($query,0,"maxid"))+1;
                          }
                          
                          $storyfile = "/projects/sunarchive/htdocs/temp/sports/sports";
                          
                          $fp = fopen($storyfile . storyid() . '.html','w');
                          if($fp)
                          {
                          fwrite($fp,'<html><head><link rel=stylesheet href=../../main2.css type=text/css></head><body>');
                          fwrite($fp,chr(10));
                          fwrite($fp,'<TABLE WIDTH="440" border="0" cellspacing="0" cellpadding="10">');
                          
                          etc
                          etc
                          etc
                          

                          All I am trying to do is provide a name for the photo using the photoid() function in the name. As I have in this html file creation.

                            Write a Reply...