doofy Posted November 26, 2011 Share Posted November 26, 2011 The following query or while loop is only increasing the ArticleID variable every 3rd time the script is run, I've narrowed it down to the following code snippet. Can you spot a problem with this, I'm in my first week of PHP and MySQL and I can't see any problem with it. Any help would be mighty appreciated by this idiot Code snippet: --- $result = mysql_query("SELECT ArticleID FROM test_top ORDER BY ArticleID ASC LIMIT 1") or die(mysql_error()); while($row = mysql_fetch_array($result)) { $ArticleID=$row['ArticleID']; } $ArticleID=intval($ArticleID); $ArticleID++; --- Quote Link to comment https://forums.phpfreaks.com/topic/251846-newb-simple-mysql-query/ Share on other sites More sharing options...
Pikachu2000 Posted November 26, 2011 Share Posted November 26, 2011 What is the purpose of that script? Context needed. Quote Link to comment https://forums.phpfreaks.com/topic/251846-newb-simple-mysql-query/#findComment-1291366 Share on other sites More sharing options...
doofy Posted November 26, 2011 Author Share Posted November 26, 2011 I'm trying to post a variable over url to identify an article id. I've got a template that will pull the data from the database based on this ArticleID. This section is at the admin's "Article Submission" point, so I want to see what the last ArticleID was from the test_top table and increase it by one. Thanks man, Chris Quote Link to comment https://forums.phpfreaks.com/topic/251846-newb-simple-mysql-query/#findComment-1291367 Share on other sites More sharing options...
Pikachu2000 Posted November 26, 2011 Share Posted November 26, 2011 If that's going to be used as an primary key index in an INSERT query, that's not a good way to handle it. Is that what you're doing? Quote Link to comment https://forums.phpfreaks.com/topic/251846-newb-simple-mysql-query/#findComment-1291370 Share on other sites More sharing options...
doofy Posted November 26, 2011 Author Share Posted November 26, 2011 I'm using an auto-incrementing ID as my primary key. Quote Link to comment https://forums.phpfreaks.com/topic/251846-newb-simple-mysql-query/#findComment-1291372 Share on other sites More sharing options...
Pikachu2000 Posted November 26, 2011 Share Posted November 26, 2011 That should also be the article id then. The way you're trying to make it work leaves it open to the possibility of duplicate article ids. Quote Link to comment https://forums.phpfreaks.com/topic/251846-newb-simple-mysql-query/#findComment-1291388 Share on other sites More sharing options...
doofy Posted November 26, 2011 Author Share Posted November 26, 2011 I had originally thought of doing that, however the problem is that there are 3 types of articles, this is the only one I want an specific variable to be populated for as it will be the only one going through the template. Quote Link to comment https://forums.phpfreaks.com/topic/251846-newb-simple-mysql-query/#findComment-1291400 Share on other sites More sharing options...
doofy Posted November 26, 2011 Author Share Posted November 26, 2011 Does the code look okay anyways, it's not doing what I intended, I was expecting it to see the last (highest value) of the variable, and increase it by one for preparation to the database. Quote Link to comment https://forums.phpfreaks.com/topic/251846-newb-simple-mysql-query/#findComment-1291401 Share on other sites More sharing options...
Psycho Posted November 27, 2011 Share Posted November 27, 2011 Does the code look okay anyways, it's not doing what I intended, I was expecting it to see the last (highest value) of the variable, and increase it by one for preparation to the database. There is nothing wrong with the code with regard to syntax, but as Pikachu2000 stated it really doesn't make sense for what you seem to be wanting to accomplish. However, I will state that there are issues witht hat code aside from syntax and purpose. 1. If you are using LIMIT 1 on the query why are you using a while loop. 2. Why extract the value and then add 1, just add 1 in the query. $query = "SELECT ArticleID + 1 FROM test_top ORDER BY ArticleID ASC LIMIT 1"; $result = mysql_query($query) or die(mysql_error()); $ArticleID = result($result, 0); But, really, what you are doing just doesn't make sense. Quote Link to comment https://forums.phpfreaks.com/topic/251846-newb-simple-mysql-query/#findComment-1291614 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.