PHP demo programs

To communicate with the database we use the MySQLi Extension. Please see the source code and refer to the mysli manual pages.

Note:

In order to be sure that mysqli uses the correct MySQL communication socket, we need to verify its value in the php.ini file. This is usually stored in /etc. Please locate it and edit / search for the mysqli.default_socket (and eventually also for mysqli.default_port). For example:

shell> vi /etc/php.ini

...
[MySQLi]
...
  mysqli.default_socket = /tmp/mysql.sock
...

On Mac OS, you should probably edit /private/etc/php.ini.

While the the source installed version of MySQL uses /tmp/mysql.sock as default socket, for pre-compiled installations typically it is /var/lib/mysql/mysql.sock or /var/run/mysqld/mysqld.sock.

To verify a socket, run for example this command:

shell> mysqladmin -u root -p variables | grep socket

See
MySQL reference page.

A simple script to perform a simple query and print the result:

// We query the BSC catalogue
<?php
$mysqli = new mysqli('localhost', 'mpeusr', 'mpe2018pass', 'mpe2018db');
$sql = "SELECT name, ra, de, vmag FROM BSC ORDER BY vmag LIMIT 8";
$result = $mysqli->query($sql);

echo "<table><tr>\n";
while ($finfo = $result->fetch_field())
  echo '<th>'. $finfo->name .'</th>';   // field names
echo '</tr>';
while ($star = $result->fetch_row()) {  // rows with the data
  echo "<tr>\n";
  foreach ($star as $value)
    echo "<td nowrap>$value</td>";
  echo "</tr>\n";
}

echo "</tr>\n";  // end of the table

$result->free();   // clean the data object
$mysqli->close();  // close the server connection
?>

Here we describe the program import_vot_demo.php used to automatically import into a MySQL table the content of a VOTable (which is an extended version of XML).
Let’s go into the PHP directory and run the demo program to import some VOTables.

shell>
  cd Soft/PHP
  ls

bsc_query.php import_vot_demo.php json_format_f.php vot2dbt.php xml2Array_class.php

If you see duplicated file names or just those with extension .php_txt, this is because files with the default extension .php cannot be downloaded using their web link. Verify files size and eventually download the former and change the extension.

Like for other scripting languages, if the file is executable then you can run it directly:

shell>
  chmod +x import_vot_demo.php
  ./import_vot_demo.php

Usage:
  ./import_vot_demo.php vot=your_votable

  optional parameters:
    [host=db_hostname] def. = localhost
    [usr=db_user]      def. = mpeusr
    [pass=db_password] def. = mpe2018pass 
    [db=db_name]       def. = mpe2018db
    [tab=db_tabname]   def. = root name of your input VOTable

Let’s import the “ROSAT All-Sky Survey: Bright Sources” catalogue contained in the table rassbsc.vot:

shell>
  gunzip ../../Data/rassbsc.vot.gz
  ./import_vot_demo.php vot=../../Data/rassbsc.vot

Warning: because the file is read in one shot, there could be memory limitation problems. As an exercise, it is left to the user to find a viable solution to this.
Suggestion: either split the file (repeating the header section) or replace in the code simplexml_load_file with another file reading procedure.