Jump to content

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.', " .

"'sally@gregs-list.net')";

 

$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.', 
'sally@gregs-list.net')";

 

 

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.