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
https://forums.phpfreaks.com/topic/221171-localhost-connection/
Share on other sites

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.