torsdag 8 december 2016

googling "wireless sensors" and php-mysqli-patch with python

Today I've also worked from home.

I looked at competitors and was trying to figure out their target group to sell to. I accidentially thought of googling "wireless sensors" and got a whole lot more work to do. It made me find 8 new competitors to analyse. They have really good products, maybe a collaboration is better. Often you have to learn their system to set it up and that could be a service to provide instead of making a new system.

Also I worked on fixing the mysql to mysqli with a pythonprogram. It saved me a lot of time but there where some things I yet have to learn about PHP to fix it.

I updated 68 of 70 files and tomorrow I will check if I did it right and learn som additional php to convert the last 2 ones which uses some weird php-arrow like:

$this->connection = mysql_connect($this->db_host,$this->username,$this->pwd);

and a command that has no simple twin in myslqi:

$rs = mysqli_query($conn, $query);
  if(mysql_result($rs,0) > 0)
    return TRUE;
  return FALSE;


Pythonprogram:

import os

"""This program should go through every .php file in subdirectories.

It looks for old mysql commands and changes them to new mysqli ones.

Example:

mysql_query($query) => mysqli_query($conn, $query)

"""

def updateFile(fileName, replacementList):
    content = open(filePath).read()
    for word in replacementList:
        if word in content:
            content  = content.replace(word, replacementList[word])
    open(fileName, "w").write(content)
    if mySQLCheck in content:
        print fileName
   
def printFileContent(filePath):
    content = open(filePath).read()
    print "-------"
    print content

fileEnding = ".php"
fileEndinglength = len(fileEnding)
replacements = {"mysql_error(":"mysqli_error($conn", "mysql_query(":"mysqli_query($conn, ", "mysql_fetch_object":"mysqli_fetch_object", "mysql_real_escape_string(":"mysqli_real_escape_string($conn, ", "mysql_num_rows(":"mysqli_num_rows(", "mysql_fetch_array(":"mysqli_fetch_array(", "mysql_fetch_assoc(":"mysqli_fetch_assoc("}
mySQLCheck = "mysql_"

currentDir = os.getcwd()
for root, dirs, fileNames in os.walk(currentDir):
    for fileName in fileNames:
        if fileName[len(fileName)-fileEndinglength:] == fileEnding:
            filePath = os.path.join(root, fileName)
            updateFile(filePath, replacements)

Inga kommentarer:

Skicka en kommentar