Share My Creation A little app to hopefully learn from ?

Hi everyone,

As a way of saying thanks to everyone who helped me on learning B4A, I have uploaded a little app that hopefully people can learn from. I have heavily commented the app to make it easier to follow.

I have personally found the best way for me to program is to create a new module for each layout I have, then use global variable to pass the info between them, so you will see the app attached has 3 layouts and 3 modules plus the excellent DBUtils Module.

I have not used the desinger to create the screens, they are all done programmaticly..

Basically what the app does is retrieve the last 15 messages from our web server and show them in a listview on the main screen. By clicking on the listview message you will be taken to the screen showing the message in full.

On the main screen is a button to send a message of your own. Just tap this button and fill in the firstname,lastname and message, then just press the confirm button. If you have missed any boxes, then you will be told so and no sending will be done !

When you send your message, it will be stored on our server and then redistributed to any other users.

The app uses a SQLlite database on the android device to store your messages, together with an http get request to our server to get and send messages.

On the server a simple PHP page is used to connect to the MySQL database and insert the data or return the data.

The data is returned to the android device in a JSON format.

There is loads that could be added, like a service to get the new messages every xx mins etc, better formatting, groups etc...

Any questions, just ask and enjoy :sign0089:

Neil

ps. if I see it on the Market, beware !:sign0010:
 

Attachments

  • exg_send2me.zip
    52.3 KB · Views: 1,461
Last edited:

gobblegob

Member
Licensed User
Longtime User
Neil thanks heaps for this example. Do you think that you could share your php file that retrieves from your mysql and returns as json?
that would be a massive help for me.


cheers Waz
 

dealsmonkey

Active Member
Licensed User
Longtime User
Neil thanks heaps for this example. Do you think that you could share your php file that retrieves from your mysql and returns as json?
that would be a massive help for me.


cheers Waz

No problem,

I'll post it up later today.

Neil
 

gobblegob

Member
Licensed User
Longtime User
That would be awesome mate.

cheers Waz
 

dealsmonkey

Active Member
Licensed User
Longtime User
That would be awesome mate.

cheers Waz


Here is the PHP Code.


B4X:
<?php
 

$FirstName = $_GET['firstname'];               //     Assign the firstname variable that is passed by the Android Device
$LastName =$_GET['lastname'];                //     Assign the lastname variable that is passed by the Android Device
$Message = $_GET['message'];                 //     Assign themessage variable that is passed by the Android Device

$dbconfig = array(                                          //Set up the database connection. Replace with your own values.
    'host' => 'localhost',
    'user' => 'username',
    'pass' => 'password',
    'name' => 'database'
    );

$db = db_mysql::getInstance(); 

mysql_select_db('database') or die('Could not select database');

if ($FirstName == "fetch"){                                                                                         //If the Android Device is getting the results.
    
    $query = 'SELECT * FROM messages';                                                                  //Messages is the table name
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    $num=mysql_affected_rows();
    $objJSON=new mysql2json();                                                                                  //Convert the SQL results into JSON
    print(trim($objJSON->getJSON($result,$num)));
      
}
else
{                                                                                                                                           //The Android device is adding new data
    $db->query("                                                                                                            
    INSERT INTO messages (Firstname, Lastname, Message)
    VALUES ('$FirstName','$LastName','$Message')
") or $db->raise_error('Failed adding new user'); // Will use the message we give it + the SQL
    
}



//Class to convert MYSQL results to JSON. This is opensource from the net..

class mysql2json{

 function getJSON($resultSet,$affectedRecords){
 $numberRows=0;
 $arrfieldName=array();
 $i=0;
 $json="";
    //print("Test");
     while ($i < mysql_num_fields($resultSet))  {
         $meta = mysql_fetch_field($resultSet, $i);
        if (!$meta) {
        }else{
        $arrfieldName[$i]=$meta->name;
        }
        $i++;
     }
     $i=0;
      $json="{\n\"data\": [\n";
    while($row=mysql_fetch_array($resultSet, MYSQL_NUM)) {
        $i++;
        //print("Ind ".$i."-$affectedRecords<br>");
        $json.="{\n";
        for($r=0;$r < count($arrfieldName);$r++) {
            $json.="\"$arrfieldName[$r]\" :    \"$row[$r]\"";
            if($r < count($arrfieldName)-1){
                $json.=",\n";
            }else{
                $json.="\n";
            }
        }


         if($i!=$affectedRecords){
             $json.="\n},\n";
         }else{
             $json.="\n}\n";
         }



    }
    $json.="]\n};";

    return $json;
 }


}



/**
* Class to interact with a mysql database
*/
class db_mysql
{
    /**
    * Class instance
    *
    * @var object
    */
    private static $instance;

    /**
    * Connection to MySQL.
    *
    * @var string
    */
    protected $link;

    /**
    * Holds the most recent connection.
    *
    * @var string
    */
    protected $recent_link = null;

    /**
    * Holds the contents of the most recent SQL query.
    *
    * @var string
    */
    protected $sql = '';

    /**
    * Holds the number of queries executed.
    *
    * @var integer
    */
    public $query_count = 0;

    /**
    * The text of the most recent database error message.
    *
    * @var string
    */
    protected $error = '';

    /**
    * The error number of the most recent database error message.
    *
    * @var integer
    */
    protected $errno = '';

    /**
    * Do we currently have a lock in place?
    *
    * @var boolean
    */
    protected $is_locked = false;

    /**
    * Show errors? If set to true, the error message/sql is displayed.
    *
    * @var boolean
    */
    public $show_errors = false;

    /**
    * Database host
    *
    * @var string
    */
    protected static $db_host;

    /**
    * Database username
    *
    * @var string
    */
    protected static $db_user;

    /**
    * Database password
    *
    * @var string
    */
    protected static $db_pass;

    /**
    * Database name.
    *
    * @var string
    */
    protected static $db_name;

    /**
    * Constructor. Initializes a database connection and selects our database.
    *
    * private, cannot be accessed directly outside of this class
    *
    * @param  string   $db_host  Database host
    * @param  string   $db_user  Database username
    * @param  string   $db_pass  Database password
    * @param  string   $db_name  Database name
    * @return boolean            Connection resource, if database connection is established.
    */
    private function __construct()
    {
        self::set_params();

        $this->link = @mysql_connect(self::$db_host, self::$db_user, self::$db_pass);

        if (is_resource($this->link) AND @mysql_select_db(self::$db_name, $this->link))
        {
            $this->recent_link =& $this->link;
            return $this->link;
        }
        else
        {
            // If we couldn't connect or select the db...
            $this->raise_error('db_mysql::__construct() - Could not select and/or connect to database: ' . self::$db_name);
        }
    }

    /**
    * Creates an instance of the class.
    *
    * @param  void
    * @return object
    */
    public static function getInstance()
    {
        if (!self::$instance)
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
    * Sets connection/database parameters.
    *
    * @param  void
    * @return void
    */
    protected static function set_params()
    {
        global $dbconfig;

        self::$db_host = $dbconfig['host'];
        self::$db_user = $dbconfig['user'];
        self::$db_pass = $dbconfig['pass'];
        self::$db_name = $dbconfig['name'];
    }

    /**
    * Executes a sql query. If optional $only_first is set to true, it will
    * return the first row of the result as an array.
    *
    * @param  string  Query to run
    * @param  bool    Return only the first row, as an array?
    * @return mixed
    */
    public function query($sql, $only_first = false)
    {
        $this->recent_link =& $this->link;
        $this->sql =& $sql;
        $result = @mysql_query($sql, $this->link);

        $this->query_count++;

        if ($only_first)
        {
            $return = $this->fetch_array($result);
            $this->free_result($result);
            return $return;
        }
        return $result;
    }

    /**
    * Fetches a row from a query result and returns the values from that row as an array.
    *
    * @param  string  The query result we are dealing with.
    * @return array
    */
    public function fetch_array($result)
    {
        return @mysql_fetch_assoc($result);
    }

    /**
    * Will fetch all records from the database, and will optionally return the
    * value of a single field from all records.
    *
    * @param  string  $sql    SQL Query string
    * @param  string  $field  Field/column
    * @return array           Will return array of all db records.
    */
    public function fetch_all($sql, $field = '')
    {
        $return = array();

        if (($result = $this->query($sql)))
        {
            while ($row = $this->fetch_array($result))
            {
                $return[] = ($field) ? $row[$field] : $row;
            }
            $this->free_result($result);
        }
        return $return;
    }

    /**
    * Returns the number of rows in a result set.
    *
    * @param  string  The query result we are dealing with.
    * @return integer
    */
    public function num_rows($result)
    {
        return @mysql_num_rows($result);
    }

    /**
    * Retuns the number of rows affected by the most recent query
    *
    * @return integer
    */
    public function affected_rows()
    {
        return @mysql_affected_rows($this->recent_link);
    }

    /**
    * Returns the number of queries executed.
    *
    * @param  none
    * @return integer
    */
    public function num_queries()
    {
        return $this->query_count;
    }

    /**
    * Lock database tables
    *
    * @param   array  Array of table => lock type
    * @return  void
    */
    public function lock($tables)
    {
        if (is_array($tables) AND count($tables))
        {
            $sql = '';

            foreach ($tables AS $name => $type)
            {
                $sql .= (!empty($sql) ? ', ' : '') . "$name $type";
            }

            $this->query("LOCK TABLES $sql");
            $this->is_locked = true;
        }
    }

    /**
    * Unlock tables
    */
    public function unlock()
    {
        if ($this->is_locked)
        {
            $this->query("UNLOCK TABLES");
            $this->is_locked = false;
        }
    }

    /**
    * Returns the ID of the most recently inserted item in an auto_increment field
    *
    * @return  integer
    */
    public function insert_id()
    {
        return @mysql_insert_id($this->link);
    }

    /**
    * Escapes a value to make it safe for using in queries.
    *
    * @param  string  Value to be escaped
    * @param  bool    Do we need to escape this string for a LIKE statement?
    * @return string
    */
    public function prepare($value, $do_like = false)
    {
        $value = stripslashes($value);

        if ($do_like)
        {
            $value = str_replace(array('%', '_'), array('\%', '\_'), $value);
        }
        return mysql_real_escape_string($value, $this->link);
    }

    /**
    * Frees memory associated with a query result.
    *
    * @param  string   The query result we are dealing with.
    * @return boolean
    */
    public function free_result($result)
    {
        return @mysql_free_result($result);
    }

    /**
    * Turns database error reporting on
    */
    public function show_errors()
    {
        $this->show_errors = true;
    }

    /**
    * Turns database error reporting off
    */
    public function hide_errors()
    {
        $this->show_errors = false;
    }

    /**
    * Closes our connection to MySQL.
    *
    * @param  none
    * @return boolean
    */
    public function close()
    {
        $this->sql = '';
        return @mysql_close($this->link);
    }

    /**
    * Returns the MySQL error message.
    *
    * @param  none
    * @return string
    */
    public function error()
    {
        $this->error = (is_null($this->recent_link)) ? '' : mysql_error($this->recent_link);
        return $this->error;
    }

    /**
    * Returns the MySQL error number.
    *
    * @param  none
    * @return string
    */
    function errno()
    {
        $this->errno = (is_null($this->recent_link)) ? 0 : mysql_errno($this->recent_link);
        return $this->errno;
    }

    /**
    * Gets the url/path of where we are when a MySQL error occurs.
    *
    * @access private
    * @param  none
    * @return string
    */
    protected function get_error_path()
    {
        if ($_SERVER['REQUEST_URI'])
        {
            $errorpath = $_SERVER['REQUEST_URI'];
        }
        else
        {
            if ($_SERVER['PATH_INFO'])
            {
                $errorpath = $_SERVER['PATH_INFO'];
            }
            else
            {
                $errorpath = $_SERVER['PHP_SELF'];
            }

            if ($_SERVER['QUERY_STRING'])
            {
                $errorpath .= '?' . $_SERVER['QUERY_STRING'];
            }
        }

        if (($pos = strpos($errorpath, '?')) !== false)
        {
            $errorpath = urldecode(substr($errorpath, 0, $pos)) . substr($errorpath, $pos);
        }
        else
        {
            $errorpath = urldecode($errorpath);
        }
        return $_SERVER['HTTP_HOST'] . $errorpath;
    }

    /**
    * If there is a database error, the script will be stopped and an error message displayed.
    *
    * @param  string  The error message. If empty, one will be built with $this->sql.
    * @return string
    */
    public function raise_error($error_message = '')
    {
        if ($this->recent_link)
        {
            $this->error = $this->error($this->recent_link);
            $this->errno = $this->errno($this->recent_link);
        }

        if ($error_message == '')
        {
            $this->sql = "Error in SQL query:\n\n" . rtrim($this->sql) . ';';
            $error_message =& $this->sql;
        }
        else
        {
            $error_message = $error_message . ($this->sql != '' ? "\n\nSQL:" . rtrim($this->sql) . ';' : '');
        }

        $message = htmlspecialchars("$error_message\n\nMySQL Error: {$this->error}\nError #: {$this->errno}\nFilename: " . $this->get_error_path());
        $message = '<code>' . nl2br($message) . '</code>';

        if (!$this->show_errors)
        {
            $message = "<!--\n\n$message\n\n-->";
        }
        die("There seems to have been a slight problem with our database, please try again later.<br /><br />\n$message");
    }
}



?>
 

aklisiewicz

Active Member
Licensed User
Longtime User
This example looks interesting, I would be looking forward to see the server part. I'm guessing (but not sure) what is the concept of this App. could you elaborate on this ?

Arthur
:sign0104:
 

dealsmonkey

Active Member
Licensed User
Longtime User
This example looks interesting, I would be looking forward to see the server part. I'm guessing (but not sure) what is the concept of this App. could you elaborate on this ?

Arthur
:sign0104:

Hi, the server part is listed in the post above. All the app does is allow you to post data to the server, which is then read by other others of the app. it does not do much in it's own right, just an example for people to hopefully learn a little.

Neil
 

aklisiewicz

Active Member
Licensed User
Longtime User
This sounds very interesting, I need to do something similar to that, so perhaps I can get some help from you. I just started with B4A a week or so ago.

I have a local SQLite database (residing on PC, and it will have some data already when the App is installed on a device). This local dB is accessed from local (desktop program which I already have up and running). Now I need to create Android App which will allow to do some updates and the exchange information with remote MySQL as well as local SQLite. The later part initially could be handled by simply copying database file from/to PC.
The first part is more important as I needto use web services to sync SQLite with MySQL. Before doing this of course I needto build an App which will have some features similar to yours (and more advanced stuff added later).
I would like to build also some kind of menu system, and so far I haven't found any information on how to do it. I will study your exmaple and if I have question ,...will post it here.

Once again - thank you for providing this excelent sample, and thanks for your time...

Arthur
 

adex1

Member
Licensed User
Longtime User
Thanks for sharing your ideas with us.

This is my first post in this forum and I'm a newbie using b4a software. Although I have basic knowledge about VB.Net programming. How should I use this PHP code on a webpage?

Here is the PHP Code.


B4X:
<?php
 

$FirstName = $_GET['firstname'];               //     Assign the firstname variable that is passed by the Android Device
$LastName =$_GET['lastname'];                //     Assign the lastname variable that is passed by the Android Device
$Message = $_GET['message'];                 //     Assign themessage variable that is passed by the Android Device

$dbconfig = array(                                          //Set up the database connection. Replace with your own values.
    'host' => 'localhost',
    'user' => 'username',
    'pass' => 'password',
    'name' => 'database'
    );

$db = db_mysql::getInstance(); 

mysql_select_db('database') or die('Could not select database');

if ($FirstName == "fetch"){                                                                                         //If the Android Device is getting the results.
    
    $query = 'SELECT * FROM messages';                                                                  //Messages is the table name
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    $num=mysql_affected_rows();
    $objJSON=new mysql2json();                                                                                  //Convert the SQL results into JSON
    print(trim($objJSON->getJSON($result,$num)));
      
}
else
{                                                                                                                                           //The Android device is adding new data
    $db->query("                                                                                                            
    INSERT INTO messages (Firstname, Lastname, Message)
    VALUES ('$FirstName','$LastName','$Message')
") or $db->raise_error('Failed adding new user'); // Will use the message we give it + the SQL
    
}



//Class to convert MYSQL results to JSON. This is opensource from the net..

class mysql2json{

 function getJSON($resultSet,$affectedRecords){
 $numberRows=0;
 $arrfieldName=array();
 $i=0;
 $json="";
    //print("Test");
     while ($i < mysql_num_fields($resultSet))  {
         $meta = mysql_fetch_field($resultSet, $i);
        if (!$meta) {
        }else{
        $arrfieldName[$i]=$meta->name;
        }
        $i++;
     }
     $i=0;
      $json="{\n\"data\": [\n";
    while($row=mysql_fetch_array($resultSet, MYSQL_NUM)) {
        $i++;
        //print("Ind ".$i."-$affectedRecords<br>");
        $json.="{\n";
        for($r=0;$r < count($arrfieldName);$r++) {
            $json.="\"$arrfieldName[$r]\" :    \"$row[$r]\"";
            if($r < count($arrfieldName)-1){
                $json.=",\n";
            }else{
                $json.="\n";
            }
        }


         if($i!=$affectedRecords){
             $json.="\n},\n";
         }else{
             $json.="\n}\n";
         }



    }
    $json.="]\n};";

    return $json;
 }


}



/**
* Class to interact with a mysql database
*/
class db_mysql
{
    /**
    * Class instance
    *
    * @var object
    */
    private static $instance;

    /**
    * Connection to MySQL.
    *
    * @var string
    */
    protected $link;

    /**
    * Holds the most recent connection.
    *
    * @var string
    */
    protected $recent_link = null;

    /**
    * Holds the contents of the most recent SQL query.
    *
    * @var string
    */
    protected $sql = '';

    /**
    * Holds the number of queries executed.
    *
    * @var integer
    */
    public $query_count = 0;

    /**
    * The text of the most recent database error message.
    *
    * @var string
    */
    protected $error = '';

    /**
    * The error number of the most recent database error message.
    *
    * @var integer
    */
    protected $errno = '';

    /**
    * Do we currently have a lock in place?
    *
    * @var boolean
    */
    protected $is_locked = false;

    /**
    * Show errors? If set to true, the error message/sql is displayed.
    *
    * @var boolean
    */
    public $show_errors = false;

    /**
    * Database host
    *
    * @var string
    */
    protected static $db_host;

    /**
    * Database username
    *
    * @var string
    */
    protected static $db_user;

    /**
    * Database password
    *
    * @var string
    */
    protected static $db_pass;

    /**
    * Database name.
    *
    * @var string
    */
    protected static $db_name;

    /**
    * Constructor. Initializes a database connection and selects our database.
    *
    * private, cannot be accessed directly outside of this class
    *
    * @param  string   $db_host  Database host
    * @param  string   $db_user  Database username
    * @param  string   $db_pass  Database password
    * @param  string   $db_name  Database name
    * @return boolean            Connection resource, if database connection is established.
    */
    private function __construct()
    {
        self::set_params();

        $this->link = @mysql_connect(self::$db_host, self::$db_user, self::$db_pass);

        if (is_resource($this->link) AND @mysql_select_db(self::$db_name, $this->link))
        {
            $this->recent_link =& $this->link;
            return $this->link;
        }
        else
        {
            // If we couldn't connect or select the db...
            $this->raise_error('db_mysql::__construct() - Could not select and/or connect to database: ' . self::$db_name);
        }
    }

    /**
    * Creates an instance of the class.
    *
    * @param  void
    * @return object
    */
    public static function getInstance()
    {
        if (!self::$instance)
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
    * Sets connection/database parameters.
    *
    * @param  void
    * @return void
    */
    protected static function set_params()
    {
        global $dbconfig;

        self::$db_host = $dbconfig['host'];
        self::$db_user = $dbconfig['user'];
        self::$db_pass = $dbconfig['pass'];
        self::$db_name = $dbconfig['name'];
    }

    /**
    * Executes a sql query. If optional $only_first is set to true, it will
    * return the first row of the result as an array.
    *
    * @param  string  Query to run
    * @param  bool    Return only the first row, as an array?
    * @return mixed
    */
    public function query($sql, $only_first = false)
    {
        $this->recent_link =& $this->link;
        $this->sql =& $sql;
        $result = @mysql_query($sql, $this->link);

        $this->query_count++;

        if ($only_first)
        {
            $return = $this->fetch_array($result);
            $this->free_result($result);
            return $return;
        }
        return $result;
    }

    /**
    * Fetches a row from a query result and returns the values from that row as an array.
    *
    * @param  string  The query result we are dealing with.
    * @return array
    */
    public function fetch_array($result)
    {
        return @mysql_fetch_assoc($result);
    }

    /**
    * Will fetch all records from the database, and will optionally return the
    * value of a single field from all records.
    *
    * @param  string  $sql    SQL Query string
    * @param  string  $field  Field/column
    * @return array           Will return array of all db records.
    */
    public function fetch_all($sql, $field = '')
    {
        $return = array();

        if (($result = $this->query($sql)))
        {
            while ($row = $this->fetch_array($result))
            {
                $return[] = ($field) ? $row[$field] : $row;
            }
            $this->free_result($result);
        }
        return $return;
    }

    /**
    * Returns the number of rows in a result set.
    *
    * @param  string  The query result we are dealing with.
    * @return integer
    */
    public function num_rows($result)
    {
        return @mysql_num_rows($result);
    }

    /**
    * Retuns the number of rows affected by the most recent query
    *
    * @return integer
    */
    public function affected_rows()
    {
        return @mysql_affected_rows($this->recent_link);
    }

    /**
    * Returns the number of queries executed.
    *
    * @param  none
    * @return integer
    */
    public function num_queries()
    {
        return $this->query_count;
    }

    /**
    * Lock database tables
    *
    * @param   array  Array of table => lock type
    * @return  void
    */
    public function lock($tables)
    {
        if (is_array($tables) AND count($tables))
        {
            $sql = '';

            foreach ($tables AS $name => $type)
            {
                $sql .= (!empty($sql) ? ', ' : '') . "$name $type";
            }

            $this->query("LOCK TABLES $sql");
            $this->is_locked = true;
        }
    }

    /**
    * Unlock tables
    */
    public function unlock()
    {
        if ($this->is_locked)
        {
            $this->query("UNLOCK TABLES");
            $this->is_locked = false;
        }
    }

    /**
    * Returns the ID of the most recently inserted item in an auto_increment field
    *
    * @return  integer
    */
    public function insert_id()
    {
        return @mysql_insert_id($this->link);
    }

    /**
    * Escapes a value to make it safe for using in queries.
    *
    * @param  string  Value to be escaped
    * @param  bool    Do we need to escape this string for a LIKE statement?
    * @return string
    */
    public function prepare($value, $do_like = false)
    {
        $value = stripslashes($value);

        if ($do_like)
        {
            $value = str_replace(array('%', '_'), array('\%', '\_'), $value);
        }
        return mysql_real_escape_string($value, $this->link);
    }

    /**
    * Frees memory associated with a query result.
    *
    * @param  string   The query result we are dealing with.
    * @return boolean
    */
    public function free_result($result)
    {
        return @mysql_free_result($result);
    }

    /**
    * Turns database error reporting on
    */
    public function show_errors()
    {
        $this->show_errors = true;
    }

    /**
    * Turns database error reporting off
    */
    public function hide_errors()
    {
        $this->show_errors = false;
    }

    /**
    * Closes our connection to MySQL.
    *
    * @param  none
    * @return boolean
    */
    public function close()
    {
        $this->sql = '';
        return @mysql_close($this->link);
    }

    /**
    * Returns the MySQL error message.
    *
    * @param  none
    * @return string
    */
    public function error()
    {
        $this->error = (is_null($this->recent_link)) ? '' : mysql_error($this->recent_link);
        return $this->error;
    }

    /**
    * Returns the MySQL error number.
    *
    * @param  none
    * @return string
    */
    function errno()
    {
        $this->errno = (is_null($this->recent_link)) ? 0 : mysql_errno($this->recent_link);
        return $this->errno;
    }

    /**
    * Gets the url/path of where we are when a MySQL error occurs.
    *
    * @access private
    * @param  none
    * @return string
    */
    protected function get_error_path()
    {
        if ($_SERVER['REQUEST_URI'])
        {
            $errorpath = $_SERVER['REQUEST_URI'];
        }
        else
        {
            if ($_SERVER['PATH_INFO'])
            {
                $errorpath = $_SERVER['PATH_INFO'];
            }
            else
            {
                $errorpath = $_SERVER['PHP_SELF'];
            }

            if ($_SERVER['QUERY_STRING'])
            {
                $errorpath .= '?' . $_SERVER['QUERY_STRING'];
            }
        }

        if (($pos = strpos($errorpath, '?')) !== false)
        {
            $errorpath = urldecode(substr($errorpath, 0, $pos)) . substr($errorpath, $pos);
        }
        else
        {
            $errorpath = urldecode($errorpath);
        }
        return $_SERVER['HTTP_HOST'] . $errorpath;
    }

    /**
    * If there is a database error, the script will be stopped and an error message displayed.
    *
    * @param  string  The error message. If empty, one will be built with $this->sql.
    * @return string
    */
    public function raise_error($error_message = '')
    {
        if ($this->recent_link)
        {
            $this->error = $this->error($this->recent_link);
            $this->errno = $this->errno($this->recent_link);
        }

        if ($error_message == '')
        {
            $this->sql = "Error in SQL query:\n\n" . rtrim($this->sql) . ';';
            $error_message =& $this->sql;
        }
        else
        {
            $error_message = $error_message . ($this->sql != '' ? "\n\nSQL:" . rtrim($this->sql) . ';' : '');
        }

        $message = htmlspecialchars("$error_message\n\nMySQL Error: {$this->error}\nError #: {$this->errno}\nFilename: " . $this->get_error_path());
        $message = '<code>' . nl2br($message) . '</code>';

        if (!$this->show_errors)
        {
            $message = "<!--\n\n$message\n\n-->";
        }
        die("There seems to have been a slight problem with our database, please try again later.<br /><br />\n$message");
    }
}



?>
 

anormal

Member
Licensed User
Longtime User
hi!
thanks for sharing!

i found very very useful to look to complete little apps for learning programming android and b4a

thanks!
 

Fox

Active Member
Licensed User
Longtime User
Thank you very much for this nice learn project. I would to try it myself on my own server. So i use the B4A Project. Created an php file and changed the MY_SQL host user, database etc. I created a table (messages in this example) and 3 Colums Firstname, Lastname and Message of the TYPE CHAR with lenght of 30, 30, 255. But if i want run the program i get an json error like this:
Could not select database and if i run the php file the same. What is my misstake or is anyone here they have an sql file for me or can help me out to solve my problem?
 
Top