Trouble Passing Parameters to php script?

Following the examples of several projects here I created a simple php script called "arduino.php as follows

<html>
<head>
<title>Test Arduino PHP Server</title>
</head>
<body>

<?php

 echo "<h1>temp1 = " . $_GET["temp1"] . "</h1>"; 
 ?>
</body>
</html>

I can easily pass values directly to the page from the url. eg “http://mywebsite/arduino.php?temp1=5” however I have not been able to pass parameters from the Arduino Client. Learning more than just coding from SurferTim, I have followed a step by step process.

  1. successfully connected and retrieved “Google.com” HTTP/1.1 200 OK
    2)successfully connected to my server and retrieved “index.html” HTTP/1.1 200 OK
    3)successfully connected to my server and retrieved “arduino.php” HTTP/1.1 200 OK

I am using one of the variations of code that are similar to temperature/data codes used by others. Here is the relevant section.

void loop()
{
  thisMillis = millis();

  if(thisMillis - lastMillis > delayMillis)
  {
    lastMillis = thisMillis;

    // Modify next line to load different page
    // or pass values to server
    sprintf(pageAdd,"/arduino.php?temp1=%d",totalCount);

 
    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    totalCount++;
    Serial.println(totalCount,DEC);
  }    
}

byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,thisPort))
  {
    Serial.println(F("connected"));

    sprintf(outBuf,"GET %s HTTP/1.0",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",serverName);
    client.println(outBuf);
    client.println(F("Connection: close\r\n"));
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

  return 1;
}

And finally here is the monitor output.

connecting...connected
HTTP/1.1 200 OK
Date: Tue, 18 Feb 2014 21:21:45 GMT
Server: Apache
Vary: Accept-Encoding
Connection: close
Content-Type: text/html


	

<html>
<head>
<title>Test Server Page</title>
</head>
<body>

<h1>temp1 = 1</h1></body>
</html>

disconnecting.
Pass 2

Looking at the monitor output, it appears that the parameter is being passed but when I load the arduino webpage in my browser, all I see is "Temp1 = ". Can anyone just what to try next?

It is receiving the variable. You are sending the variable temp1 with totalCount as the value. The variable totalCount starts at zero and increments after each call to the getPage() function. The first send should be temp1=0, the second should be temp1=1, and so on.

The serial monitor output shows the second iteration where temp1 = 1.

Test Server Page

temp1 = 1

disconnecting.
Pass 2

Thanks ST. Yes the monitor output shows the value being sent, but I thought if I then entered mywebpage.com - This website is for sale! - mywebpage Resources and Information. I would be able to see the value as in Temp1 = 0, then Temp1=1, and so on. All I ever get to see on the website is Temp1=

That is not the way it works. You must store that value somewhere. I use a MySQL database to store data from php pages.

Oh No. Just when I thought I was getting the hang of this. Thank you for setting me straight. Well the good part about all this is so far I'm OK. I just have to get my dat into mysql, which I now have available on my new 'paid for' server. I have seen several good scripts here that accomplish writing to mysql and I will work in that direction, but could I ask your opinion on the new mysql library function that connects directly to mysql.

I recommend trying connecting to your MySQL database to start. After that, it is a MySQL challenge. There are several good websites that have MySQL code examples you can modify to suit your needs.

This php code connects to a MySQL database named “test”. That is all it does. Get it working first.

<?php
	 $dbcnx = mysql_connect("localhost","user", "password");

	 if (!$dbcnx)
	 {
	    echo( "<P>Unable to connect to the database server at this time.</P></body></html>" );
	    exit();
	}

	if (!mysql_select_db("test") )
	{
		echo( "<P>Unable to locate the test database at this time.</P></body></html>" );
		exit();
	}

        echo( "<P>Connected to the database.</P></body></html>" );

	mysql_close($dbcnx);
?>

That is exactly where I will start. Thanks for your generous help.

Tim beat me to it, but, here is a very simple class I made for a small site years ago. It wraps the basic mysqli interface, you can use this if you like, or at least see how the functions can be used.

EDIT: you should use the newer mysqli_x interface, rather than mysql_x

The PHP documentation is very good. PHP.net, MySQLi: http://au1.php.net/manual/en/book.mysqli.php

<?php
//Written by Christopher Andrews
//Released under the MIT license

  class DBAccess{

    private $connected; //True if connected.
    public  $database;  //The database connection object for mysqli.
    private $result;  //The result of an SQL query.
    
    
    /* __construct Function. */
    
    public function __construct( $connect_now = false )
        {
          $this->connected = false;
          $this->result = 0;
          
          if( $connect_now ) $this->Connect();
          return;
        }

    function __destruct() { $this->Close(); }
    public function __get($name) { return $this->$name; }  
    public function __set($name, $value) { $this->$name = $value; }  
        
        
    /* Close Function. */    
    
    public function Close()
        {
          if( $this->connected ){
            
            if( $this->result != 0 ){
            
              @$this->result->close();
              $this->result = 0;
            }            
            $this->database->close();
            $this->connected = false;
          }
          return;
        }
        
        
    public function Escape( $str ){  return ( empty( $str ) || !$this->connected ) ? $str : $this->database->real_escape_string( $str ); }
        
        
    /* Connect Function. */    
    
    public function Connect( $db_Address = 'localhost', $db_User = 'your_db_user', $db_Pass = 'your_db_password', $db_Name = 'your_db_name' )
        {
          //Close any previous connections.
          $this->Close();
          
          //Attempt a new connection
          $this->database = new mysqli( $db_Address, $db_User, $db_Pass, $db_Name );
          $this->result  = 0;

          //Check for connection errors.
          return $this->connected = ( mysqli_connect_errno() ?  false : true );
        }
        

    public function Connected() { return $this->connected == true; }
        
    
    /*
      Execute Function.
      
      Executes an SQL statement on the database. If you are not intending to retrieve data, don't use the return value.
      false_on_empty only affects the result if manage_result is true.
    */        
        
    public function Execute( $sql_query, $manage_result = true, $false_on_empty = false )
        {
          if( $this->connected ){
            
            if( $this->result != 0 ){
            
              @$this->result->close();
              $this->result = 0;
            }
            if( $manage_result ){
              
              $query_result = $this->database->query( $sql_query );
              
              if( is_bool( $query_result ) ){
              
                return $query_result;
                
              }else{
              
                $this->result = $query_result;
                
                if( $false_on_empty && ( $this->result->num_rows == 0 ) )
                  return false;
                else
                  return $this->result;
              }
            }else{
              
              return $this->database->query( $sql_query );
            }
          }
          return false;
        }  
                
  } //End of DBAccess class implementation.

$dba_object = 0;

function &GetDB( $connect = true )
  {
	global $dba_object;
	if( !is_object( $dba_object ) ) $dba_object = new DBAccess( $connect );
	else if( !$dba_object->Connected() && $connect ) $dba_object->Connect();
	return $dba_object;
  }

?>

This file should never be placed in the public_html/www folders. It should always be above it in the root or one of its non-public subfolders.

The function at the bottom is an easy way to get a DB connection.

<?php
  #include "../DB.php"

  //Connect to database.
  $dba = GetDB();
		
  //Check if DB connection was successful.
  if( $dba->Connected() ){

    $result = $dba->Execute( "select * from Table" );
    $row = $result->fetch_row();
    print_r( $row );

    //Class looks after closing result.
  }
 //Class looks after closing DB.
?>

Love it how the forum highlights PHP, not C++

Just spent the last hour troubleshooting a database setup error, but I finally connected to the database. Thank you for pointing me at this mysqli interface. I will certainly take a good look at it. Just to help me get started though, how does this compare to Charles Bells mysql connector?

I haven't used it, however anything that uses mysql should be avoided, mysqli was put in place to help fix its many vulnerabilities.
I wrote my class to make it very easy to use a single database, and simpler to use multiple.

Using the GetDB function allows safe and quick use between modules that use the same DB, as it allows sharing, and avoids multiple connections.