Connect to MySQL Database

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

Connect to MySQL Database

Post by outjack »

I am having problems with my php code below:

Code: Select all

 
print ("something!");
 
$link = mysqli_connect("localhost","xxx","yyy","testDB");
 
if(mysqli_connect_errno())
{
    printf("Connection failed: %s\n", mysqli_connect_error());
    exit();
}
else
{
    $sql = "insert into testtbl(firstName) values('christine')";
    $result = mysqli_query($link, $sql);
    
    if($result == TRUE)
    {
        echo "a record has been inserted.";
    }
    else
    {
        printf("Could not insert record: %s\n",mysqli_error($link));
    }
    
    mysqli_close($link);
}
?>
The first line of code, "print ("something!");" is outputing to the browser which tells me php is working correctly with apache. However the rest of the code seems to be doing nothing. I am not sure if its making a connection or not. Anyone please assist.

~pickle | Please use [ syntax ] or [ code ] tags where appropriate. Your post has been modified. I also removed your database credentials.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Connect to MySQL Database

Post by Kieran Huggins »

I really like this syntax for debugging:

Code: Select all

$link = mysqli_connect("localhost","xxx","yyy","testDB") or die(mysqli_connect_error());
Eliminates that big nasty loop.

Also, not many hosts have mysqli installed, but nearly all of them have the mysql functions.

Try switching to them and see if your problem remains.
outjack
Forum Newbie
Posts: 3
Joined: Thu Jan 17, 2008 4:58 pm

Re: Connect to MySQL Database

Post by outjack »

Everah | Please use [code] tags when posting code in the forums. For posting PHP code, use [code=php]. Thanks.
Below is the code I tried and it still did not work

Code: Select all

<?php
 
print ("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);
}
?>
I added 'or die(mysqli_connect_error());' and the only output im getting is the first line: 'something!'.

Please assist anyone.

Everah | Please use [code] tags when posting code in the forums. For posting PHP code, use [code=php]. Thanks.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Connect to MySQL Database

Post by RobertGonzalez »

You are mixing mysql and mysqlI with that.

Try checking what mysql extension(s) you have using something:

Code: Select all

<?php
var_dump(get_loaded_extensions());
?>
Then check out the code samples in the manual for how to connect and query.
Post Reply