0

My php script to insert data into a db is not working. It isn't giving any error messages either, so I do not know what is wrong. What is wrong?

My table:

Number  Name        Type            Null    Default      
1       Timestamp   timestamp       No      CURRENT_TIMESTAMP   
2       BTC         float           Yes     NULL
3       USD         float           Yes     NULL

My script:

<?php
$json_url = "https://crypto-trade.com/api/1/ticker/dvc_btc";
$json_data = file_get_contents($json_url);
$json_feed = json_decode($json_data);
$DVCdata = $json_feed->data;
$DVCask = $DVCdata->min_ask;
$json_url1 = "https://api.bitcoinaverage.com/ticker/USD";
$json_data1 = file_get_contents($json_url1);
$json_feed1 = json_decode($json_data1);
$BTCask = $json_feed1->ask;
$DVC_USD = $BTCask * $DVCask;
$DVCround = round($DVC_USD, 8);
$connection = mysqli_connect("mysql.serversfree.com",user,pass,database); 

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($connection,"INSERT INTO database (BTC, USD)
VALUES ('$DVCask', '$DVCround')");

mysqli_close($connection);    
?>
5
  • 2
    Have you checked to see if mysqli_query returns anything? Have you looked to see if mysqli_error() returns anything? Commented Dec 10, 2013 at 16:56
  • I'm hoping your mysqli_connect() parameters are just dummy variables for the sake of masking the real values? As the syntax is obviously wrong. Commented Dec 10, 2013 at 16:58
  • 2
    FLOAT? Are you sure?!!? Commented Dec 10, 2013 at 16:59
  • Yes, they are in the form of w.xyz Commented Dec 10, 2013 at 17:52
  • @JohnConde How is the syntax incorrect? I'm new to this so sorry Commented Dec 10, 2013 at 17:53

1 Answer 1

2

database is a reserve MySQL keyword. You will need to use backticks on the table name in order to use database as a table name.

mysqli_query($connection,"INSERT INTO `database` (BTC, USD)
VALUES ('$DVCask', '$DVCround')");

More info here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.