Page 1 of 1

Connect to MySQL Database

Posted: Thu Jan 17, 2008 5:02 pm
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.

Re: Connect to MySQL Database

Posted: Thu Jan 17, 2008 5:34 pm
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.

Re: Connect to MySQL Database

Posted: Fri Jan 18, 2008 4:48 pm
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.

Re: Connect to MySQL Database

Posted: Fri Jan 18, 2008 6:29 pm
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.