5kyy8lu3 Posted December 17, 2008 Share Posted December 17, 2008 Hey. I added an "id" column to my table to make it easier to do sorting/etc. I can't get my code to work and I came to find out that I can't get my query to work for the life of me. My table contains three columns. filename, description, and id. Here's my "test" code, all I'm trying to do at this point is pull the highest value in the "id" column and print that value to the screen. <?php include("../l_i_f2.php"); $cxn = mysqli_connect($host, $user,$passwd,$dbname) or die ("Unable to establish a connection with the MySQL Server."); $query = 'SELECT MAX(id) FROM bleudeciel16_data'; $result = mysqli_query($cxn, $query) or die(mysqli_error($cxn)); $row = mysqli_fetch_assoc($result); echo $row['id']; ?> i run this and it prints nothing to the screen, but when I goto my sql query sender page and run "SELECT * FROM bleudeciel16_data" there's definitely a column named 'id', and it has a value in EVERY SINGLE row. what am I doing wrong? today's been a bad day for me for coding lol i can't seem to get anything to work. Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/ Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 Try this: echo $row['MAX(id)']; Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717787 Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 <?php include("../l_i_f2.php"); $cxn = mysqli_connect($host, $user,$passwd,$dbname) or die ("Unable to establish a connection with the MySQL Server."); $query = 'SELECT MAX(id) as id FROM bleudeciel16_data'; $result = mysqli_query($cxn, $query) or die(mysqli_error($cxn)); while ($row = mysqli_fetch_assoc($result)) { echo $row['id']; } You need a while loop to go through each record. Also use the "AS" keyword to make the column name actually ID, the max would have thrown it off. Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717788 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 Either way should work. Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717789 Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 Either way should work. Yep, the as is a personal perference, I hate referencing items like MAX(col) etc, too much extra effort =) Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717791 Share on other sites More sharing options...
5kyy8lu3 Posted December 17, 2008 Author Share Posted December 17, 2008 Either way should work. Yep, the as is a personal perference, I hate referencing items like MAX(col) etc, too much extra effort =) i tried echo $row['MAX(id)']; and i got 9 as an output, and 21 is what it REALLY the max id in my table, any ideas? lol Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717794 Share on other sites More sharing options...
Adam Posted December 17, 2008 Share Posted December 17, 2008 Going to say, you don't need to loop through the results... As you're only selecting the MAX(id) it will only return a single field of data. A Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717797 Share on other sites More sharing options...
5kyy8lu3 Posted December 17, 2008 Author Share Posted December 17, 2008 Going to say, you don't need to loop through the results... As you're only selecting the MAX(id) it will only return a single field of data. A well i tried both methods, it's still returning the value of 9, and the max value in the id column is actually 21, this is driving me insane lol Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717798 Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 Going to say, you don't need to loop through the results... As you're only selecting the MAX(id) it will only return a single field of data. A well i tried both methods, it's still returning the value of 9, and the max value in the id column is actually 21, this is driving me insane lol Post your table data please. Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717800 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 Weird... 'id' is auto-increment, correct? Try this, out of curiosity: $query = 'SELECT id FROM bleudeciel16_data ORDER BY id DESC LIMIT 1'; $result = mysqli_query($cxn, $query) or die(mysqli_error($cxn)); $row = mysqli_fetch_assoc($result); echo $row['id']; Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717802 Share on other sites More sharing options...
5kyy8lu3 Posted December 17, 2008 Author Share Posted December 17, 2008 Weird... 'id' is auto-increment, correct? Try this, out of curiosity: $query = 'SELECT id FROM bleudeciel16_data ORDER BY id DESC LIMIT 1'; $result = mysqli_query($cxn, $query) or die(mysqli_error($cxn)); $row = mysqli_fetch_assoc($result); echo $row['id']; ugh this is so weird, i tried that and still got 9 lol here's proof of the column existing and having higher values than just 9 Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717812 Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 Eh nm, just saw you were using mysqli. Why not loop through and print all the IDs and see what prints out/why it would stop at 9... Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717815 Share on other sites More sharing options...
5kyy8lu3 Posted December 17, 2008 Author Share Posted December 17, 2008 Eh nm, just saw you were using mysqli. Why not loop through and print all the IDs and see what prints out/why it would stop at 9... i just did and everything is exactly the way it is in that screenshot, i can take a screenie of this too if you really need lol UPDATE: OMG lol I bet I know what's wrong............... my id column isn't set as primary key since i just added it, think that's it? Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717819 Share on other sites More sharing options...
Adam Posted December 17, 2008 Share Posted December 17, 2008 That shouldn't really effect the MAX() result, what data type is 'id'? A Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717827 Share on other sites More sharing options...
5kyy8lu3 Posted December 17, 2008 Author Share Posted December 17, 2008 That shouldn't really effect the MAX() result, what data type is 'id'? A VARCHAR(25) i think, lemme go check Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717832 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 'id' should be auto-increment... Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717837 Share on other sites More sharing options...
Adam Posted December 17, 2008 Share Posted December 17, 2008 Change the data type to integer as well - I imagine it's treating it as a string, causing unexpected results from MAX() .. A Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717841 Share on other sites More sharing options...
Lamez Posted December 17, 2008 Share Posted December 17, 2008 your id type should be int Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717852 Share on other sites More sharing options...
5kyy8lu3 Posted December 17, 2008 Author Share Posted December 17, 2008 Change the data type to integer as well - I imagine it's treating it as a string, causing unexpected results from MAX() .. A that did it, such a simple thing throwing me off like that, i really appreciate everyone who gave their input, i'm gonna quit for the day before i hurt myself! haha Quote Link to comment https://forums.phpfreaks.com/topic/137379-solved-need-fresh-eyes/#findComment-717859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.