Jump to content

[SOLVED] New to PHP.. Where am I going wrong here?


ManOnScooter

Recommended Posts

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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.