1

I'm a beginner at php, so please be gentle.. :-) I've looked for answers, but time and time again I get the same error. I can't find the error and it's drivin' me nuts!

$query = "SELECT position FROM pages WHERE subject_id = '$subject_id' ORDER BY position DESC LIMIT 1";
$bla = mysql_query($query, $connection);
$position = $bla[0];
$position += 1;

Ok, the above code is at the top of my page. From what I understand, this should do the following: get the column called 'position' from the table 'pages' where the 'subject_id' matches the subject_id I passed, and sort this and pick 1 value (which would be the highest). So I would now have 1 column with 1 value. I then grab that value, put it in 'position' and increment that with 1.

But the error I get back is: "Column count doesn't match value count at row 1" Again, I'm a beginner (I'm used to another language), so this is probably something small and stupid that I forgot/did wrong.. can anyone help me out?

martynthewolf
  • 1,718
  • 2
  • 11
  • 22
Malachi
  • 977
  • 6
  • 16
  • 31
  • 1
    It may not help answer your question, but you should stop using `mysql_*` functions. They're being deprecated. Instead use [PDO](http://php.net/manual/en/book.pdo.php) (supported as of PHP 5.1) or [mysqli](http://php.net/manual/en/book.mysqli.php) (supported as of PHP 4.1). If you're not sure which one to use, [read this SO article](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – Matt Aug 10 '12 at 15:58
  • What's the *entire* error message? – Matt Aug 10 '12 at 15:59
  • 1
    There's nothing wrong with this query. You're doing an `insert` somewhere else that's screwing things up. – Matt Aug 10 '12 at 16:00

2 Answers2

5

mysql_query() returns a statement handle, not the data you want. You have to fetch a row first:

$bla = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_row($bla);
$position = $row[0] + 1;
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    That's true, but it wouldn't give the *Column count doesn't match value count* error. – Matt Aug 10 '12 at 16:01
  • @Marc B: Thank you, this did the trick. Well, not at first though.. I realized that I didn't do an INSERT statement correctly after the code I had an error in. When I fixed that (something I had overlooked), the whole thing worked like a charm! In the other language that I've used so far, I can simply pick a certain field from the table in 1 statement, so I'm trying to get used to this "mysql_" stuff.... – Malachi Aug 10 '12 at 19:28
  • @Matt: The code that Mark gave me worked, but you were right as well. That error came from something else: I did an INSERT, gave the fields (3) and the values (4). I then realized that I missed a field there... So thank you for having me checking another place as well! – Malachi Aug 10 '12 at 19:30
-1

You could just change your query to:

$query = "UPDATE pages SET position=position+1 WHERE subject_id = '$subject_id' ORDER BY position DESC LIMIT 1";

then simply run

mysql_query($query, $connection);

Obviously, if you have two subject_ids with the same highest value, only one will update - but the way you have your script set up now would do the same anyway.

a1phanumeric
  • 814
  • 2
  • 12
  • 29