Jump to content

Need help on connecting to mysql database from php.


boyyo

Recommended Posts

Im trying to connect to a database from php. Heres the code:

<?php

  $dbc = mysqli_connect('192.168.0.122', 'boyyo', 'KiaNNa11', 'aliendatabase')

    or die('Error connecting to MySQL server.');

 

 

  $query = "INSERT INTO aliens_abduction (first_name, last_name, " .

    "when_it_happened, how_long, how_many, alien_description, " .

"what_they_did, fang_spotted, other, email) " .

"VALUES ('Sally', 'Jones', '3 days ago', '1 day', 'four', " .

"green with six tentacles', 'We just talked and played with a dog', " .

"'yes', 'I may have seen your dog. Contact me.', " .

"'[email protected]')";

 

$result = mysqli_query($dbc, $query)

or die('Error querying database.');

?>

 

I think i have a theory that my MySQL server location is wrong but i dont know. I use HostGator to do this and im using Phpmyadmin. But everytime i type in a form that i created it says Error querying database. Can someone tell me whats wrong with this code. Oh by the way im using head first into PHP and MySQL

First a tip.  You will save yourself a lot of time and trouble in realizing that you do not need to concatenate just because you want to have a newline in your string.  This is the same as what you have.

 

$query = "INSERT INTO aliens_abduction (first_name, last_name,  
    when_it_happened, how_long, how_many, alien_description,  
   what_they_did, fang_spotted, other, email) 
   VALUES 
    ('Sally', 'Jones', '3 days ago', '1 day', 'four', 
green with six tentacles', 'We just talked and played with a dog', 
'yes', 'I may have seen your dog. Contact me.', 
'[email protected]')";

 

 

If you could not connect to the db you'd be getting the first error, so there's a problem in your query.  Try returning the actual error message.

 

   $result = mysqli_query($dbc, $query)
   or die(mysqli_error($dbc));

gizmola meant using the function mysqli_error to find out why your query is failing, Checkout the snippet gizmola posted

Try returning the actual error message.

 

   $result = mysqli_query($dbc, $query)
   or die(mysqli_error($dbc));

Notice the use of the mysqli_error function.

 

What is the error message?

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.