-1

I got the tutorial from this link

https://icreateproject.info/2014/12/14/arduino-save-data-to-database/

The tutorial is about how to save data from Arduino to xampp database over the local network.

I follow everything until step 3. This is the PHP code:

<?php

// Prepare variables for database connection

$dbusername = "arduino";  // enter database username, I used "arduino" in step 2.2
$dbpassword = "arduinotest";  // enter database password, I used "arduinotest" in step 2.2
$server = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"

// Connect to your database

$dbconnect = mysql_pconnect($server, $dbusername, $dbpassword);
$dbselect = mysql_select_db("test",$dbconnect);

// Prepare the SQL statement

$sql = "INSERT INTO test.sensor (value) VALUES ('".$_GET["value"]."')";    

// Execute SQL statement

mysql_query($sql);

?>

I tried to run this command in the link as mentioned in the tutorial

http://localhost/write_data.php?value=100

This is the error I get

Fatal error: Uncaught Error: Call to undefined function mysql_pconnect() in C:\xampp\htdocs\write_data.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\write_data.php on line 11

Nico Haase
  • 11,420
  • 35
  • 43
  • 69
Zubelius
  • 27
  • 1
  • 3
  • 8
  • 1
    mysql_pconnect is deprecated as mysql_connect , try to use mysqli or PDO instead – Joaquin Javi Feb 25 '19 at 15:51
  • 1
    _“This is the error I get”_ - next time, please _research_ any such error messages you get first. This has been discussed numerous times already. And maybe try to find tutorials that aren’t half a decade old already … – 04FS Feb 25 '19 at 15:54
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Nico Haase Feb 25 '19 at 16:09
  • @JoaquinJavi i tried to use your method but get another error **Warning: mysqli_connect(): (HY000/1045): Access denied for user 'arduino'@'localhost' (using password: YES) in C:\xampp\htdocs\write_data.php on line 11 Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\write_data.php on line 12 Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\write_data.php on line 20** – Zubelius Feb 25 '19 at 17:16
  • @04FS sorry but i did search but did not find the solution i wanted – Zubelius Feb 25 '19 at 17:17

2 Answers2

0

The mysql library that you are using is deprecated in php5.5 http://php.net/manual/en/function.mysql-connect.php

Here's an updated version with mysqli_connect


    $dbusername = "arduino";   
    $dbpassword = "arduinotest";  
    $server = "localhost";
    $database = 'test';

   $mysqli = mysqli_connect($server, $dbusername, $dbpassword,$database);

   // please validate the $_GET['value']
   $sql = "INSERT INTO test.sensor (value) VALUES ('".$_GET["value"]."')";

   mysqli_query($mysqli, $sql);


new mysqli http://php.net/manual/en/function.mysqli-connect.php

Vidal
  • 2,605
  • 2
  • 16
  • 32
  • 1
    _“you have an error on the mysql_connect () you have a p.”_ - this is not a typo; mysql_pconnect and mysql_connect are two different functions, with different purposes. – 04FS Feb 25 '19 at 15:53
  • yep, good point but he is getting an "Call to undefined function" error. – Vidal Feb 25 '19 at 15:55
  • Yes, because the mysql extension has been removed. But that has little to do with mysql_pconnect vs mysql_connect, as your first sentence implies - _both_ functions existed in that extension. – 04FS Feb 25 '19 at 15:57
  • I updated it, to clear the confusion and help this guy, xamp now ships with php 7, so he have to updated the code. – Vidal Feb 25 '19 at 16:05
  • Hello @Vidal i followed ur codes and get another error **Warning: mysqli_connect(): (HY000/1045): Access denied for user 'arduino'@'localhost' (using password: YES) in C:\xampp\htdocs\write_data.php on line 10 Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\write_data.php on line 15** – Zubelius Feb 25 '19 at 17:18
  • Base on php error, the user does not have access to the database. Did you created the user account and gave access? – Vidal Feb 25 '19 at 17:39
  • @Vidal yes i did. I granted all the access to user arduino – Zubelius Feb 26 '19 at 01:25
0

Somehow i managed to connect and changed the value by typing and auto insert using this code

<?php

header("Refresh:5");

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

else {
echo "hello";
}

$value = $_GET['value'];

$sql = "INSERT INTO test.sensor (value) VALUES ($value)";

$sql = "INSERT INTO test.sensor (value)
VALUES ('255')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>

And without this code, which is on line 28, i will get error so i have to put this code as well

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

Thank you everyone for helping really appreciate it.

Zubelius
  • 27
  • 1
  • 3
  • 8