Jump to content

First time creating a database


namhu

Recommended Posts

Hello guys, i hope im in the right place...

 

Im trying to link a html form to a database with php for the first time and i need some help.

 

I created the database and the i created a html form that as an action to the next php page:

 

/*

<?PHP

$username="****";

$password="****";

$database="fixusweb_Contacts";

 

$first=$_POST["first"];

$last=$_POST["last"];

$phone=$_POST["phone"];

$mobile=$_POST["mobile"];

$email=$_POST["email"];

$fax=$_POST["fax"];

$web=$_POST["web"];

 

mysql_connect(localhost,$username,$password);

mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM Contacts";

$result=mysql_query($query);

$num=mysql_num_rows($result);

 

mysql_close();

 

echo "<b><center>Database Output</center></b><br><br>";

 

$i=0;

while ($i < $num) {

 

$first=mysql_result($result,$i,"first");

$last=mysql_result($result,$i,"last");

$phone=mysql_result($result,$i,"phone");

$mobile=mysql_result($result,$i,"mobile");

$fax=mysql_result($result,$i,"fax");

$email=mysql_result($result,$i,"email");

$web=mysql_result($result,$i,"web");

 

echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";

 

$i++;

}

 

?>

*/

 

the problem is that there is no output whatsoever...and the db stays the same....so i think this script its not doing nothing!

 

Can someone help me with this problem please?

 

PS: soz my english..

 

Thanks

Link to comment
Share on other sites

Rather than using mysql_result it better to do this:

$num = mysql_num_rows($result);

mysql_close();

echo "<center>Database Output <i>($num result(s) returned)</i></center><br />\n\n";

while ($row = mysql_fetch_assoc($result))
{
    $first = $row['first'];
    $last = $row['last'];
    $phone = $row['phone'];
    $mobile = $row['mobile'];
    $fax = $row['fax'];
    $email = $row['email'];
    $web = $row['web'];

    echo "Name: $first $last<br />
Phone: $phone<br />
Mobile: $mobile<br />
Fax: $fax<br />
E-mail: $email<br />
Web: $web<br />";

}

mysql_fetch_assoc returns an array of each row in the result set. Much faster, cleaner an easier to understand.

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.