spfoonnewb Posted February 3, 2007 Share Posted February 3, 2007 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 More sharing options...
genericnumber1 Posted February 3, 2007 Share Posted February 3, 2007 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 Link to comment https://forums.phpfreaks.com/topic/36963-solved-mysql-while-statement/#findComment-176406 Share on other sites More sharing options...
spfoonnewb Posted February 3, 2007 Author Share Posted February 3, 2007 It needs to select from the ID though, I dont want it to select everyones row.. just theirs.. and each row contains Value1, Value2, etc.. Basically each ID has X amount of skills, and I need to pull them all out. Link to comment https://forums.phpfreaks.com/topic/36963-solved-mysql-while-statement/#findComment-176411 Share on other sites More sharing options...
genericnumber1 Posted February 3, 2007 Share Posted February 3, 2007 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 Link to comment https://forums.phpfreaks.com/topic/36963-solved-mysql-while-statement/#findComment-176419 Share on other sites More sharing options...
spfoonnewb Posted February 3, 2007 Author Share Posted February 3, 2007 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 Link to comment https://forums.phpfreaks.com/topic/36963-solved-mysql-while-statement/#findComment-176427 Share on other sites More sharing options...
genericnumber1 Posted February 3, 2007 Share Posted February 3, 2007 on a side note if you are just getting one row you can retrieve that row without putting it in a while loop, eg $theOnlyRow = mysql_fetch_array($result); Link to comment https://forums.phpfreaks.com/topic/36963-solved-mysql-while-statement/#findComment-176429 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.