PHP-Database connect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
outjack
Forum Newbie
Posts: 3
Joined: Thu Jan 17, 2008 4:58 pm

PHP-Database connect

Post by outjack »

I am trying to connect to a MySQL database with the following code:
I have not had any success so far. Please help me out.

*** PLEASE USE THE

Code: Select all

TAG *****[/color]

Code: Select all

<?php
 
echo ("something!");
 
$link = mysql_connect("localhost","james","james","testDB") or die(mysqli_connect_error());
 
if(mysqli_connect_errno())
{
    printf("Connection failed: %s\n", mysqli_connect_error());
    exit();
}
else
{
    $sql = "insert into testtbl(firstName) values('christine')";
    $result = mysql_query($link, $sql);
    
    if($result == TRUE)
    {
        echo "a record has been inserted.";
    }
    else
    {
        printf("Could not insert record: %s\n",mysql_error($link));
    }
    
    mysql_close($link);
}
?>
The line " echo "something!" " is displays output which tells me that PHP and apache are working fine. However, I cant seem to get through to the database please help me out.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP-Database connect

Post by Christopher »

You are using the mysql_connect() to connect and the mysqli_*() fuctions for errors. They are two different libraries.
(#10850)
Post Reply