Max() not working right...

Discussion in 'PHP' started by Greenmethod, Sep 18, 2007.

  1. #1
    	$highest_po_result = mysql_query("SELECT po_num FROM `inventory`");
    	$high_po = mysql_fetch_array($highest_po_result);
    	echo 'max: ' . max($high_po) . '<br>';
    	echo 'num rows: ' . mysql_num_rows($highest_po_result) . '<br>';
    	if (0 == mysql_num_rows($highest_po_result)){
    		echo 'newpo';
    		$new_po = 1;
    	}
    	else {
    		$new_po = max($high_po);
    		$new_po = $new_po+1;
    	}
    PHP:
    I have the exact code written on another page and it works fine, but this just gives me max is 1 and num_rows is the actual number of rows every time.

    I don't know what else I can do to troubleshoot.. any ideas?
     
    Greenmethod, Sep 18, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    What exactly are you trying to accomplish?

    You know that you're working with just one single row from the database, right?



    Are you trying to fetch the highest "po_num" from this table? Maybe you sould consider something like this:
    
    SELECT po_num FROM `inventory` ORDER BY po_num DESC LIMIT 1
    
    Code (sql):
     
    nico_swd, Sep 18, 2007 IP
  3. omgitsfletch

    omgitsfletch Well-Known Member

    Messages:
    1,222
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    145
    #3
    And if you still need the number of items:

    SELECT COUNT(*) FROM `inventory`
    HTML:
     
    omgitsfletch, Sep 18, 2007 IP