words all fail the magic prize

 

web stuff category archive

music stuff, bookmarked for later

How to Digitize an LP
Streaming audio on GNU/Linux

Audacity
Gnome Wave Cleaner
icecast.org

and many more software links at Wikipedia: List of Linux audio software

Posted by pzed on March 9, 2010 at 2.56pm

MySQL and PHP

I learned some SQL a few years ago, and with a refresher find that it’s fairly straightforward. And of course the nice thing about scripting your queries is that you can test them until they’re correct, and then forget about them.

Integrating this into PHP is still a bit of a challenge for me after, what, a day-and-a-half of trying? Things to remember:

  • Five basic steps to querying
    1. Open the database connection
    2. Select the database (it seems to me these first two have been combined in PHP’s mysqli functions, but I’m using a slightly older text)
    3. Run the query
    4. Fetch the query results into an array
    5. Format and print the results
  • Steps one through three require error handling
  • The array that’s created in step three is an associative array in which the field name given in the query corresponds to the data returned. The array is processed one row at a time. There’s no need to include the table name, because the array corresponds to the query structure, rather than the table structure. This is easy to visualize if you think of the table that is displayed when a query is run from the mysql command prompt.

So it’s fairly simple to generate a flat html table from data that exist across multiple database tables. I’d like to figure out how to generate not-so-flat data; for example, suppose (just suppose) I had a database in which a number of books are stored in one table, and their chapters are stored in another. I’d like an output that would list the book, then the contents, for each book. The simple approach described above won’t do it. Maybe in another day-and-a-half this too will seem simple, but right now I just can’t quite figure it out.

Williams, Hugh E. and David Lane. Web Database Applications with PHP and MySQL. Sebastapol, CA: O’Reilly, 2004.

Posted by pzed on January 30, 2008 at 1.49pm

a quick note

-> means “is being passed to.”

I’m sure I whipped right past this yesterday, but it makes sense to me now. The books I’m working through are both read to go to MySQL, but I don’t think I am. For most of the last two days I haven’t had access to the internet, so I put aside my music library reading to focus on the basics of PHP. These are, of course, related, and I soon have to get back the the music stuff. I want to keep at the PHP, though. It’s straightforward enough, but there are some conceptual aspects that I’m struggling with a bit, and I think another day’s immersion will do the trick.

Posted by pzed on January 25, 2008 at 10.28am

includes and functions

includes

  • the usefulness of reusing code should be obvious to everyone!
  • require() and include() call code from a file
  • the main difference is that if something goes wrong, require() will return a fatal error, include() will only give a warning
  • data need not be passed to the include because it is inserted in the code and executed as if present all along (I think)
  • it’s a good idea to store include files outside the document tree, making them invisible to internet interlopers

functions

  • “A function is a self-contained module of code that prescribes a calling interface, performs some task, and optionally returns a result”
  • a library of user-defined functions can be stored in an include file
  • data must be passed to a function using a parameter (see “scope”, below)
  • user-defined functions must be declared, e.g.
  •   function say_hi()
      {
      echo 'hi';
      }

  • function names can only contain letters, digits, underscores; cannot begin with a digit; and cannot be the same as an existing function name

scope

  • scope controls where a variable is visible and useful
  • variables declared inside a function are in scope from the declaration until the function’s end brace; this is function scope, the variables are local variables
  • variables declared outside functions are in scope from the declaration to the end of the file, but not inside functions; this is global scope, the variables are global variables
  • if a require() or include() statement is used within a function, function scope applies; if not, global
  • the keyword global can be used to give a variable defined within a function global scope
  • to allow functions to operate on global variables, use a pass by reference; the function receives a reference to the original variable, and any modifications performed on the reference are also performed on the original
  • a pass by reference is created by prefixing an ampersand to the variable name in the function’s parameter list
  • the return keyword can be used to stop a function, usually in response to some condition; return can also be used to return a value
  • it is possible to call a function recursively, but this seems dangerous and doesn’t appear to have many benefits

Welling, Luke and Laura Thomson. PHP and MySQL Web Development. 2nd ed. Indianapolis: Sam’s, 2003.

Posted by pzed on January 24, 2008 at 4.55pm

arrays in php

  • an array is a type of variable used to store a set of values (that much I knew)
  • numerically indexed arrays are indexed numerically (!) starting at 0
  • an array can be initialized directly or an array can be created by copying from another array using the = operator
  • an array can also be created using data stored in a file, or in a database
  • loops are convenient ways to access numerically indexed arrays
  • an associative array allows the association of any key or index with each value
  • the each() function can be used to loop through an associative array
  • the list() function can be used to split an array into a number of values – I assume this corresponds to the number of associations
  • multidimensional arrays are created by nesting
  • one advantage of associative arrays is that data can be accessed by the name of the key, which makes more sense than a simple digit
  • thus it is easier to identify and access a single value in a complex array
  • however, looping is simpler with numerically indexed arrays, so these will be preferable in some situations

Welling, Luke and Laura Thomson. PHP and MySQL Web Development. 2nd ed. Indianapolis: Sam’s, 2003.

Posted by pzed on January 23, 2008 at 5.44pm

a mishmash of basic php

  • literal is the term (noun) used to describe raw data, such as text
  • variables and literals can be combined in output in two ways: variable names can be placed alongside literals within double-quoted strings, or single quoted literals can be concatenated with variables. The concatenation symbol is the period.
  • this distinction occurs because php attempts to evaluate the contents of double-quoted strings, whereas single-quoted strings are not evaluated
  • found a nice discussion of the difference between GET and POST methods here: http://www.cs.tut.fi/~jkorpela/forms/methods.html
  • identifier is a generic term for the names given to variables, functions, classes
  • php identifiers are case sensitive, and cannot begin with a digit (I haven’t quite figured out the use of $ yet)
  • php does not require variables to be declared before they are used
  • constants can be declared using the define() function
  • variables can have one of four types of scope
    1. built-in superglobal variables are visible everywhere within a script
    2. global variables declared in a script are visible throughout the script, but not inside functions
    3. variables used inside functions are local to the function
    4. variables used inside functions that are declared as global refer to the global variables of the same name

I think this last should read “variables that are declared as global inside functions refer to the global variables of the same name”.

Welling, Luke and Laura Thomson. PHP and MySQL Web Development. 2nd ed. Indianapolis: Sam’s, 2003.

Posted by pzed on January 23, 2008 at 12.40pm

I should just know this stuff

but it never hurts to review:

  • three-tier architectures: client(browser) ↔ web server ↔ database server
  • database applications are stateful, http is stateless
  • the browser is a thin client, but it is possible to pass some of the work to the browser; e.g. JavaScript can be used to validate data before sending to the web server

I have a tendency to skim discussion of strings, variables, math, increments, expressions, operators. . . . Can’t I just look that stuff up? But I may need to think about character encoding and data entry. On to the object-oriented stuff:

  • an object is a container in which data and functions can be bound
  • a class is an object (?) that can be used to create many similar objects (it doesn’t help that the book I’m using keeps using the term package to describe the example of a class that is used†)
  • a method is a function that belongs to a class (or package); a method can be called on an object that has been created by the class
  • each specific object created by the class is an instance

† – It reads like this: An object is created by a package. A method (or member function) is called on an object. A method is provided by a package. The package in question is a class (aren’t all packages – should I just forget they ever said “package”?). An object created by a class is an instance. Now I get an example that shows me how to create a user-defined class that counts. There’s nothing like learning by doing. Back to class:

  • variables defined within a class are member variables; functions, member functions.
  • “The class definition defines how data and functionality are actually bound together—member variables and functions take their meaning from the class of which they’re a part.”
  • class definitions can be placed in include files, the benefits of which aren’t listed in my text, but I imagine must include:
    1. efficiency: the class needn’t be written out every time it’s needed, saving time and bandwidth
    2. uniformity: by having only one copy of a class that could be used by multiple programmes, we ensure that the class always functions in the same way
    3. security: the class remains on the server, and I’m guessing this might make things a little more secure in some way. Or not. I just can’t finish a list after only two items.
  • a constructor is a method that is called when an object is created, and can be used to set up the initial state of the new object
  • a destructor is a method that is called when an object is destroyed
  • destructor functions are useful for housekeeping tasks like closing the connection to a DBMS or saving user preferences to a file

PHP also has private members variables, but it’s very rare that these ever become law.

I think I’ll save inheritance for tomorrow.

Williams, Hugh E. and David Lane. Web Database Applications with PHP and MySQL. Sebastapol, CA: O’Reilly, 2004.

Posted by pzed on January 22, 2008 at 5.28pm