Reputation: 31
i have the code im trying to upload data read from two sensors to in arduino to the WAMP server but when running the localhost\project,i get that Warning.THE php file are located in the projects directory.The Connect.php is used when accessing the database.The Connect.php code is the one used mainly for connection here it is,the add.php proccess the POST from arduino.index.php displays the sensor values in a table:
Below is Connect.php
****<?php
function Connection(){
$server="server";
$user="user";
$pass="pass";
$db="database";
$connection = mysqli_connect($server, $user, $pass);
//$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'user', 'pass');
if (!$connection) {
die('MySQL ERROR: ' . mysql_error());
}
mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );
return $connection;
}
?>****
Below is add.php:
**<?php
include("connect.php");
$link=Connection();
$temp1=$_POST["temp1"];
$hum1=$_POST["hum1"];
$query = "INSERT INTO `tempLog` (`temperature`, `humidity`)
VALUES ('".$temp1."','".$hum1."')";
mysql_query($query,$link);
mysql_close($link);
header("Location: index.php");
?>**
lastly index.php:
**<?php
include("connect.php");
$link=Connection();
$result=mysql_query("SELECT * FROM `tempLog` ORDER BY `timeStamp` DESC",$link);
?>
<html>
<head>
<title>Sensor Data</title>
</head>
<body>
<h1>Temperature / moisture sensor readings</h1>
<table border="1" cellspacing="1" cellpadding="1">
<tr>
<td> Timestamp </td>
<td> Temperature 1 </td>
<td> Moisture 1 </td>
</tr>
<?php
if($result!==FALSE){
while($row = mysql_fetch_array($result)) {
printf("<tr><td> %s </td><td> %s </td><td> %s </td></tr>",
$row["timeStamp"], $row["temperature"], $row["humidity"]);
}
mysql_free_result($result);
mysql_close();
}
?>
</table>
</body>
</html>**
Upvotes: 3
Views: 6440
Reputation: 4967
You're mising mysqli_ and mysql_ (It's not the same thing..)
Connection returns a mysqli_
connection:
$connect = mysqli_connect($server, $user, $pass);
But you're trying to use it to select db and query via mysql_
:
mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );
Also, using the mysqli_
connection as a link to a mysql_
query makes no sense:
$link=Connection();
$result=mysql_query("SELECT * FROM `tempLog` ORDER BY `timeStamp` DESC",$link);
Please just take a look at the mysqli docs and scrape the mysql_
code. -- Or even better, use PDO!
Upvotes: 1
Reputation: 94642
Try either using the mysqli_select_db with the correct params like this
$connection = mysqli_connect($server, $user, $pass);
if (!$connection) {
die('MySQL ERROR: ' . mysql_error());
}
mysql_select_db($connection,$db) or die( 'MySQL ERROR: '. mysql_error() );
return $connection;
Or use the mysqli_connect()
to also select the database by adding it as the 4th parameter, then you can forget the mysql_select_db()
completely.
$connection = mysqli_connect($server, $user, $pass, $db);
if (!$connection) {
die('MySQL ERROR: ' . mysql_error());
}
Upvotes: 0