Jump to content

[SOLVED] Info from database


yddib

Recommended Posts

<html>

<head>

</head>

<body>

<?php


mysql_connect("localhost", "username ", "password") or die(mysql_error());
//connects to the databse
mysql_select_db("username");


echo "open";

$result = @mysql_query("SELECT f_name FROM gradinfo where email ="[email protected]");

if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
while($row = mysql_fetch_array($result))
{
}

?>
<form name="form" action="post.php" method="post">
First Name:
<input type="text" name="f_name" value="<?php echo $row['f_name'] ?>">
<input type="submit" value="send">
<input type="reset" value="reset">
</form>

</body>

</html>

 

 

Hi i'm having a small problem. I want to get the information out of the database and into the form. I think the problem lies with <?php echo $row['f_name']?>

Can anyone help?

Link to comment
https://forums.phpfreaks.com/topic/122712-solved-info-from-database/
Share on other sites

You should be getting a syntax error from your query.

 

change it to

$result = mysql_query("SELECT f_name FROM gradinfo where email ='[email protected]'")or die(mysql_error());

 

What's the point of these lines?

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

 

Take that out and replace it with

$row = mysql_fetch_array($result);

Anytime you wish to output information from a query yo need to make sure when you call the row variable, that you do so inside the while braces "{}"

 

<html>
<head>
</head>
<body>
<?php
mysql_connect("localhost", "username ", "password") or die(mysql_error());
//connects to the databse
mysql_select_db("username");
echo "open";
$result = @mysql_query("SELECT f_name FROM gradinfo where email ="[email protected]");
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
while($row = mysql_fetch_array($result))
{
?>
<form name="form" action="post.php" method="post">
First Name:
<input type="text" name="f_name" value="<?=$row['f_name'] ?>">
<input type="submit" value="send">
<input type="reset" value="reset">
</form>
<?php
}
?>
</body>
</html>

 

or you can set the row array to a new variable array

<html>
<head>
</head>
<body>
<?php
mysql_connect("localhost", "username ", "password") or die(mysql_error());
//connects to the databse
mysql_select_db("username");
echo "open";
$result = @mysql_query("SELECT f_name FROM gradinfo where email ="[email protected]");
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}

$yourvar = array();

while($row = mysql_fetch_array($result))
{
    
    $yourvar[] = $row;

}
?>
<form name="form" action="post.php" method="post">
First Name:
<input type="text" name="f_name" value="<?=$yourvar['f_name'] ?>">
<input type="submit" value="send">
<input type="reset" value="reset">
</form>
</body>
</html>

however the overall easiest way to do a query is:

 

<?php
//$mysqli = new mysqli("SERVER","USERNAME","PASSWORD","DATABASE");

    $mysqli = new mysqli("localhost", "username ", "password","username");
         $results = $mysqli->query("SELECT f_name FROM gradinfo where email ="[email protected]);
         $resultset = $results->fetch_assoc();
?>

 

Then you simply continue on your normal merry way.  You output the same way you would as if you were using mysql procedural functions.

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.