ManOnScooter Posted October 25, 2007 Share Posted October 25, 2007 I want to show the result of the database query into the textbox(its a unique result). I am able to show it in the label but not in the text box- any suggestions?? <html> <body> <?php $con = mysql_connect("localhost","root","administrator"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT details FROM test WHERE first_name = 'scooter'"); echo "<table border='1'> <tr> <th>Details</th> </tr>"; echo "</table>"; while($row = mysql_fetch_array($result)) { echo $row['details']; } mysql_close($con); ?> <input type="text" name="q" size="16" value="<?php echo $row["details"]?>"/> </body> </html> Thanks Scooter Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/ Share on other sites More sharing options...
KevinM1 Posted October 25, 2007 Share Posted October 25, 2007 Looks like you forgot a semi colon for the value in our input tag. Try: <input type="text" name="q" size="16" value="<?php echo $row["details"];?>"/> Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377929 Share on other sites More sharing options...
premiso Posted October 25, 2007 Share Posted October 25, 2007 <?php $con = mysql_connect("localhost","root","administrator"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT details FROM test WHERE first_name = 'scooter'"); echo "<table border='1'> <tr> <th>Details</th> </tr>"; echo "</table>"; while($row = mysql_fetch_array($result)) { echo $row['details']; echo '<input type="text" name="q" size="16" value="'. $row["details"] .'"/>'; } ?> </body> </html> mysql_close is not needed. You needed to put the input inside the loop. Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377930 Share on other sites More sharing options...
GingerRobot Posted October 25, 2007 Share Posted October 25, 2007 Try: <html> <body> <?php $con = mysql_connect("localhost","root","administrator"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT details FROM test WHERE first_name = 'scooter'"); echo "<table border='1'> <tr> <th>Details</th> </tr>"; echo "</table>"; $row = mysql_fetch_array($result); ?> <input type="text" name="q" size="16" value="<?php echo htmlentities($row["details"]);?>"/> </body> </html> You don't need the while loop if you're only showing one result, so i took that out. And, not knowing whats in your details field, i've used the htmentities() function - otherwise you might have issues if the field contains a double quote, since you are enclosing the value of the field in double quotes. Edit: Beaten to it but posting anyway; we really don't need a loop here Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377931 Share on other sites More sharing options...
ManOnScooter Posted October 25, 2007 Author Share Posted October 25, 2007 Thanks, It worked... Ok need a favour, got any online tutorials for doing this kinda stuff? is there a way I can do the above witout using the while loop?? Thanks again... Scooter Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377932 Share on other sites More sharing options...
premiso Posted October 25, 2007 Share Posted October 25, 2007 In short, no. The while loop has to be in there or a for loop or a for each loop. But one way or the other to get the data out of MySQL you have to loop through it. While is generally the best solution. For a tutorial, google PHP MySQL tutorial and a ton should pull up. I believe this site has one, plus many other sites. Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377934 Share on other sites More sharing options...
GingerRobot Posted October 25, 2007 Share Posted October 25, 2007 Tutorials: http://www.phpfreaks.com/tutorials.php You'll find all sorts there And yes, you can do it without a loop. See my last post. Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377936 Share on other sites More sharing options...
ManOnScooter Posted October 25, 2007 Author Share Posted October 25, 2007 Thanks Ginger, if i want to do this witout the while loop.. is it possible?? <?php $con = mysql_connect("localhost","root","administrator"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT details FROM test WHERE first_name = 'scooter'"); while($row = mysql_fetch_array($result)) { echo $row['details']; } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377938 Share on other sites More sharing options...
ManOnScooter Posted October 25, 2007 Author Share Posted October 25, 2007 Ginger, If you were refering to it being done this way.. <?php $con = mysql_connect("localhost","root","administrator"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT details FROM test WHERE first_name = 'scooter'"); echo htmlentities($row["details"]); mysql_close($con); ?> It isnt giving me any value, but the while loop is giving... did i go wrong anywhere? Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377941 Share on other sites More sharing options...
GingerRobot Posted October 25, 2007 Share Posted October 25, 2007 Yeah, you missed out the call to the mysql_fetch_assoc() function. We still need that, we just don't need it in a loop: <?php $con = mysql_connect("localhost","root","administrator"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT details FROM test WHERE first_name = 'scooter'"); $row = mysql_fetch_assoc($result); echo htmlentities($row["details"]); mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377946 Share on other sites More sharing options...
KevinM1 Posted October 25, 2007 Share Posted October 25, 2007 Thanks, It worked... Ok need a favour, got any online tutorials for doing this kinda stuff? is there a way I can do the above witout using the while loop?? Thanks again... Scooter You don't need the while-loop if you're sure that you only want to get one row back from the database. My version of this would be: <?php $dbCon = mysql_connect("localhost", "root", "administrator") OR die("Could not connect:" . mysql_error(()); mysql_select_db("test", $dbCon) OR die("Could not select correct table!"); $query = "SELECT details FROM test WHERE first_name = 'scooter'"); $result = mysql_query($query); if($result){ $row = mysql_fetch_assoc($result); $details = htmlentities($row['details']); } else{ $details = "No details found"; } ?> <html> <body> <table> <tr> <th>Details:</th> </tr> <tr> <td><input type="text" name="q" size="16" value="<?php echo $details;?>" /></td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377947 Share on other sites More sharing options...
ManOnScooter Posted October 25, 2007 Author Share Posted October 25, 2007 Thanks guys, Thanks ginger & night!! Guess its am doing fine now- should move to the next chapter and more queries.. Thanks again !! Scooter Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377950 Share on other sites More sharing options...
PHP_PhREEEk Posted October 25, 2007 Share Posted October 25, 2007 Well, first off, let's talk about code format. You need to pick a format and be consistent with it. Here you are interspersing PHP and HTML without much rhyme or reasoning. This makes a script very error-prone and then difficult to debug once erros start cropping up. Secondly, if you're only expecting one result from the MySQL query, you do not need to loop over it. Maybe you are doing the loop because future development might call for more than one user's details would be called up? Anyway, assuming the details will always be for one user, let's drop the loop (even if future development might call for the loop, let's design without it first, then once we are successful, we can build the loop later). Also, please use the BB codes for CODE... <?php // Let's get the data here, then do the HTML later... $con = mysql_connect("localhost","root","administrator"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT details FROM test WHERE first_name = 'scooter' "); // test to ensure the query was accepted and we got one row of data if (!$result || mysql_num_rows($result) != 1) { die('There were no results - MySQL says:<br />' . mysql_error()); } else { // Our result was valid, assign it to a variable list($details) = mysql_fetch_row($result); } // We obviously have good data now, so let's let the HTML output begin ?> <html> <body> <table border="1"> <tr> <th>Details</th> </tr> </table> <input type="text" name="q" size="16" value="<?php echo $details?>"/> </body> </html> This code has been thoroughly tested. PS - Late reply, others have answered, but there's always several ways of approaching something. I'll leave the code here in case there's something useful for you. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/74761-solved-new-to-php-where-am-i-going-wrong-here/#findComment-377967 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.