Jump to content

[SOLVED] Mysql While Statement


spfoonnewb

Recommended Posts

My database is setup like this:

 

ID, Value1, Value2, Value3, Value4, Value5, Value6, etc.

 

I need this PHP script to pull out each value, it currently only pulls out one.. whats wrong?

 

<?php

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$result = mysql_query("SELECT * FROM `mydb` WHERE id = '".$_SESSION["id"]."'") or die(mysql_error());  

while($row = mysql_fetch_array( $result )) {

$i = "1";

echo "".$i++.". <textarea name=\"".$row["Value$i++"]."\"></textarea><br /><br />";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/36963-solved-mysql-while-statement/
Share on other sites

You're only telling it to pull out one

 

WHERE id = '".$_SESSION["id"]."'

 

basically... pull out the row where id = $_SESSION['id'] -- there's only one row with that ID so it only returns one

 

<?php

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$result = mysql_query("SELECT * FROM `mydb`") or die(mysql_error());  

$i = "1"; // moved out because it would reset $i each loop if it was in the while loop

while($row = mysql_fetch_array( $result )) {

echo "".$i++.". <textarea name=\"".$row['id']."\"></textarea><br /><br />";

}

?>

 

try that

ah, I misunderstood...

 

<?php

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$result = mysql_query("SELECT * FROM `mydb` WHERE id = '".$_SESSION["id"]."'") or die(mysql_error());  

$row = mysql_fetch_array( $result );

$i = "1";
$done = false;

while ($done == false) {
      if(isset($row['value' . $i])){
            echo $i . '<textarea name="' . $row['value' . $i] . '"></textarea><br /><br />';
            ++$i;
      } else {
            $done = true;
      }
}
?>

 

there's probably a better way of doing it, but you get the idea.. ps this wasn't tested so you may need to fix some syntax

Thanks, this worked;

 

//New
$i = "1";
$done = false;

while($row = mysql_fetch_array( $result )) {
while ($done == false) {
      if(isset($row["value$i"])){
            echo $i . "<textarea name=\"value$i\"></textarea><br /><br />";
            ++$i;
      } else {
            $done = true;
      }
}
}
//End

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.