Jump to content

Localhost Connection


Corbokhan

Recommended Posts

I'm looking to connect to a MySQL localhost on my mac. I've been working for a few hours at this and I still can't figure it out.

 

Below is my code. I've tried a number of different ways to connect to the database including using new mysqli, mysql_connect and PEAR MDB2. Any suggestions as to what I can do? Thanks in advance.

 

<?php

$first=$_POST['first'];
$last=$_POST['last'];

if (!$first || !$last) {
echo 'Error: Enter the required data.';
exit;
}

if (!get_magic_quotes_gpc()) {
    $first = addslashes($first);
    $last = addslashes($last);
}

$hostname = 'localhost:8889';
$username = 'root';
$password = 'root';
$dbname = 'phonebook';

@ $db = new mysqli($hostname,$username.$password,$dbname);

if (mysqli_connect_errno()) {
echo 'Errer: Could not connect to database';
exit;
}
echo '.....Connected Successfuly';

$query = "insert into contact values 
	 ('null','".$first."','".$last."')";
$result = $db->query($query);

if ($result) {
echo $db->affected_rows." contact inserted into the database";
echo 'Contact Entered:  '.$first.' '.$last.'';
}
else {
echo 'Error: Contact not added.';
exit;
}

$db->close();

?>

 

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.