Jump to content

Recommended Posts

Hello,

 

When I execute this script what happens is that every field in the table is repeated twice, where am I messing up??

 

$questions_datasource = mysql_query("SELECT * FROM table");

while($question = mysql_fetch_array($questions_datasource))

{

echo "<tr>";

foreach($question as $data)

//for($i = 0; $i < sizeof($question); $i++)

{

echo "<td>{$data}</td>";

}

echo "</tr>";

 

}

If I replace foreach with: for($i = 0; $i < sizeof($question); $i++)

it works fine.

 

But in another script I tried foreach and it did the same, but I replace with for like this:

 

$edit_question = mysql_query("SELECT * FROM intro WHERE id = {$_GET['ID']}");

while($question = mysql_fetch_array($edit_question))

{

for($i = 0; $i < sizeof($question); $i++)

{

$field_name = mysql_field_name($edit_question, $i);

echo "<tr><td>$field_name</td><td>$data</td><td><input type=\"textarea\" /></td></tr>";

}

 

}

What happened is it printed all the array contents in rows, but when it reached the final row it didn't finish it printed a double more textareas. What the hell..

 

Thanks in advance!

sizeof AKA  count returns the number of items, in an array, the array starts from 0, so you need to remove one from the count, see below

 

<?php
            $edit_question = mysql_query("SELECT * FROM intro WHERE id = {$_GET['ID']}");
            while($question = mysql_fetch_array($edit_question))
            {
               for($i = 0; $i < (sizeof($question)-1); $i++)
               {
                  $field_name = mysql_field_name($edit_question, $i);
                  echo "<tr><td>$field_name</td><td>$data</td><td><input type=\"textarea\" /></td></tr>";
               }
               
            }
?>

mysql_fetch_array does both associative and indexd so 30 is correct

 

try this

$edit_question = mysql_query("SELECT * FROM intro WHERE id = {$_GET['ID']}");
while($question = mysql_fetch_assoc($edit_question))
{
	foreach($question as $K => $V)
	{
		$field_name = mysql_field_name($edit_question, $i);
		echo "<tr><td>$K</td><td>$V</td><td><input type=\"textarea\" /></td></tr>";
	}
}

 

 

I have just been playing with it as it was printing the data twice for me too until i changed this line

 

while($question = mysql_fetch_array($questions_datasource))

 

to

 

while($question = mysql_fetch_assoc($questions_datasource))

 

might be worth a go

hehe, while I was trying to find something I tried mysql_fetch_row, instead of array, and it worked fine! it printed 15

 

Thanks for the assoc! it works too! but it's associative so that's a bit of a problem(not with foreach though)

 

i did:

 

echo "<pre>";

print_r($question);

echo "</pre>";

 

this is what is printed:

 

Array

(

    [0] => 1

    [id] => 1

    [1] => www.google.com

    [question] => www.google.com

    [2] => www.zzz.com

    [answer] => www.zzz.com

    [3] => recursion

    [notes] => recursion

    [4] => recursion

    [tags] => recursion

    [5] => 2000

    [year] => 2000

    [6] => saher

    [people] => saher

    [7] => 5

    [rating] => 5

    [8] => 5

    [rators_num] => 5

    [9] => al

    [moed] => al

    [10] => 1

    [question_num] => 1

    [11] => 1

    [question_part] => 1

    [12] => 2

    [times_visited] => 2

    [13] => 1

    [recommend] => 1

    [14] => the pizpiz

    [question_name] => the pizpiz

)

 

so yea you are right. But is this what is supposed to happen, how weird I never ran into this problem before. Well what do you use as a php programmer? mysql_fetch_assoc?

 

Thanks a lot for your replies

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.