Jump to content

Mysql_fetch_array problem


ll87

Recommended Posts

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);
                   }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...