Jump to content

[SOLVED] Need fresh eyes


5kyy8lu3

Recommended Posts

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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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

 

query.jpg

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 :)

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.