ll87 Posted February 25, 2010 Report Share Posted February 25, 2010 I have the following query code. I insert a record and then do a select to retrieve it. The select is working as I can see it in my DB. Mysql_num_rows gives me a result of 1 which is correct. When i try to retrieve the data using mysql_fetch_array within a for loop, it says the values of id and title are empty which doesn't make sense. I thought it was to do with my query but seeing as the result of num_rows is correct it can't be. I'm stumped :s. $insert = mysql_query('INSERT INTO blog (title,entry) VALUES ("'.$blog_title.'","'.$blog_entry.'")') or die("Error:".mysql_error()); $new_result = mysql_query('SELECT id,title,entry FROM blog WHERE title="'.$blog_title.'" AND entry="'.$blog_entry.'"') or die("Error1:".mysql_error()); echo $new_rows = mysql_num_rows($new_result) or die(mysql_error()); for($i=0;$i<=$new_rows;$i++){ $row = mysql_fetch_array($new_result) or die(mysql_error()); $id = $row['id']; $title = $row['title']; $title = str_replace(" ","_",$title); header('Location: blog.php?title='.$title.'&id='.$id); } Quote Link to comment Share on other sites More sharing options...
BeeDev Posted February 25, 2010 Report Share Posted February 25, 2010 This is just a guess but: for($i=0;$i<=$new_rows;$i++){ shouldn't it be for($i=0;$i<$new_rows;$i++){ (notice that it's only less than and NOT less than or equals Because arrays based at 0 and mysql_num_rows will return a number based at 1 it's trying to loop an extra 1 time and mysql_fetch_array will return false on the 2nd go. Quote Link to comment Share on other sites More sharing options...
ll87 Posted February 25, 2010 Author Report Share Posted February 25, 2010 yea i initially had < but that wasnt working so i changed it. strangely enough i altered it a lot to play around, and then got rid of it all, and now its working with what i posted about. hours wasted for nothing. it's really strange. my friend told me about mysql_insert_id() and its definitely the better way to go Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.