Peri.me

Sjsu Student’s weblog

Bad Queries

  • Filed under: PHP
Friday
Jan 15,2010

Just got a taste of a bad query.

uf_files = 3400 rows
uf_uploads = 17,000 rows

I wanted to get fields from table uf_files, based on a uf_uploads. So I wrote
SELECT f.* FROM `uf_files` f WHERE
(SELECT count(upload_id) FROM `uf_uploads` u WHERE
u.file_id = f.file_id AND u.site_id = $siteID AND u.status = 'uploaded') = 0

Query Start Time: 01/15/10 15:24:52
Query End Time: 01/15/10 15:29:32

THAT IS A BAD QUERY! A query should not take that long when less than 21,000 rows are involved.

I splitted the queries into 2 parts.

$query = "SELECT file_id FROM `uf_uploads` WHERE site_id = $siteID AND (status = 'uploaded')"; $result = $mysqli->query($query);
$values = '0';
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$values .= ', '. $row['file_id'];
}
}
$query = "SELECT * FROM `uf_files` WHERE file_id NOT IN ($values)";

Together, these 2 queries takes 1 second. Big difference!

Just a nice example of writing good and bad queries.

New rule of life

Sunday
Nov 22,2009

On  my Computer,  I have now come to expect following things to be done under 10 seconds

- finding a file or program

- Coming across something new, and either searching  for videos, or information.

- Opening a program or a folder  I use monthly

- Finding a text I bookmarked before.

- Finding things I have waiting on my queue of “To Watch”

- Finding a specific link in my bookmark

- Creating a new instance of MPC

- Creating screenshots

All of this had become possible with the firefox and its add-ons, autoHotKey, Select & crop of Paint, Re-Organizing my bookmarks and videos every now and then, and windows 7 ability to remember recent open files for programs

What need to be done:

- Be able to go desktop without a short delay caused by windows sidebar

- Use firefox address bar without my temporarily freezing

- Finding a way to organize all of my notes, of all sorts of kinds.

- Appropriate way to create a to-do list that I actually follow. (tried 4-5 different methods, and none of them are working for  me)


Flash video pauses in FireFox

  • Filed under: Firefox
Sunday
Oct 4,2009

While watching video in firefox, there are slightly pauses that occur in firefox. Here is how I was able to resolve it. The problem seems to occur because the firefox is saving the current session, so the solution is to increase the interval at which the firefox saves your session.
1. In the address bar, type: about:config
2. Filter: browser.sessionstore.interval
3. Change it from 10000 to a higher value. I chose 60000.
So now the browser will save my current Firefox session every 10 minutes.

Source: http://several.amplify.com/2009/08/21/how-to-fix-annoying-youtube-pauses-in-firefox/

Installing PHP-FFMPEG

Tuesday
Sep 15,2009

1. I am trying to install it but got an error.
2. So I read Cannot find autoconf. Please check your autoconf installation, when I tried to install them, I got further error.
3. To fix these new errors, I had to replace ./configure && make && make install with ./configure –prefix=$HOME && make && make install , and I got these 2 to install. Since I am on shared hosting, I don’t have access to certain directories which it was trying to install the software to, so I needed to use my home directory for installation. Now the software is installing in a bin directory that resides in my /home/username/.

4. For PHP-FFMPEG, when I run

phpize

I still get the following error:

Configuring for:
PHP Api Version:         20020918
Zend Module Api No:      20020429
Zend Extension Api No:   20050606
Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF
environment variable is set correctly and then rerun this script.

5. So, I need to specify $PHP_AUTOCONF to where I installed autoconf, my home directory.

export PHP_AUTOCONF=/home/hsbsitez/bin:$PHP_AUTOCONF

6. I still got same error when running phpize.
7. I decided to follow the ffmpeg-php on dreamhost shared host.
8. Still did not worked, so I gave up, and decided to just use exec command of php.

Resources:
1. phpfox help. FFMpeg and FFMpeg-php
2. Cannot find autoconf. Please check your autoconf installation
3. Can’t install FFMpeg

Aptanaa

  • Filed under: PHP
Sunday
Sep 6,2009

Adding additional Libraries

1. Right click on the project, and choose import.
2. Now you can select the appropriate libraries.

PHP Code Generator 1. It is pretty explanatory that you can first create a php interface class, and then create a php class which will be based on the interface. In the PHP class, instead of copy and pasting functions from the interface, you can use Source->Override/Implement Methods.
2. You can also generate get & set method for each variables by Source->Generate Getters and Setters.

Auto Completion

1. If the string variable name is similar to set method, then the program auto-input the possible string.

Key Shortcuts

Editing
1. If I want to access code of a function, hold down the ctrl key, the function will turn into a link, and I can click on it. It will open the appropriate file and take me to that function.

Function.
1. To access the manual, highlight the php function, right click on it and select Open PHP Manual or Ctrl + F2.

Breakpoints

They are used to specify certain places in code where you can observe the internal data, and see if there is any issue.
1. First double click on the area next to the line, and a blue dot shall appear.
2. In the toolbar, click the Bug icon to start debugging.
3. First, it will pause at the first line of PHP (to disable it, click on the error next to Debug, and go to Debug Configuration. Un-check Break at First Line.
4. Click the resume button([]>) to continue, and now it will stop at your specified break-point. If you have not specified break-point, then the program would terminate.
5. Assuming you are right now on your specified break-point, to go to next line of code, click Step Over.
6. Step Into will take me to source code of the object or the function that I may be calling or creating. Step Return will take me out from there and take me back.
7. In the source code, I can hover over the object or a function, and it will give me the type of the object, or comments I had written about the function.
8. In the Variables View, I can edit the variables to whatever I want, and see the impact of the changes on my code.

If I want to set-up a breakpoint for a for-loop, when let’s says $x=5, then we want a conditional breakpoint
1. Create a breakpoint on the line.
2. Right click the break-point and select Breakpoint Properties.
3. In the box Enter Condition, I can enter in $x == 5

To start debugging at a different file, rather than index.php

1. Go to Debug Configuration.
2. Create a new debug configuration.
PHP Debugger: XDebug
Use Speicifed Script: Browse and select the file.
3. Above assumes that the new configuration is being made when you had the ‘different file’ opened.

Apache – Mod_rewrite no work.

Saturday
Sep 5,2009

“Please check if you have AllowOverride All directive in your httpd.conf file.
This directive should be placed in the appropriate Apache container that points to your Web application directory.
I assume that you have .htaccess file in that directory.”
ResourcE: http://forums.zend.com/viewtopic.php?f=8&t=157

Coding Annoyances

  • Filed under: PHP
Saturday
Sep 5,2009

I have been programming in PHP for past 5-6 years. It started out with include(‘index.php’) function in my html page and has grown into my own passion.

Over the years, I have seen various kind of changes that has occurred in the way I code. Here are few of them

1. Adding comments

This is actually a recent change. I start all of my person comments in the format /** **/, and all of the codes that I have commented out, I use //. This let’s me differentiate between code that I have used either for testing purposes or old code that is replaces, and the part where I explain what does the following code does.
I always put the comment on the line above the code, which it is about. The only exceptions may be variables.
Other use I have found is to put in the header of the file, name of all of the functions in the file, and a one line description next to it. So when I open up a file in 6 months, and I don’t remember my function name convention, I can just look at the header, and go down the list.

2. Controller File

I think that is what it is called. Basically, in the recent scripts I have written, they basically have 1 main file which decides as to which function to call. In the past, I had a global index, and would include all of the other files code in that file.
Before:
1. Send get statements to Index.php with the filename, and arguments
2. If the file exists, then include(filename);
Now:
1. Each different scripts(filescollection, vincier, search engine) have their own main.php class.
2. It is known as to which script is being used, via the url of the page that has been identified using .htaccess
3. For example http://hsbsitez.com/vincier , this go to vincier_main.php.
4. Now the scripts loads settings from the database, and loads in all of the vincier files.
5. The main file does alot of if…else, switch…case arguments based on the get arguments, and decides which function to execute.
6. Now each script has their own look, which is not possible in the previous method.

Url method

One of the most annoying issue I had was to figure out each time as to what url-formatting I use let’s say for download a file, showing links, and showing list of anime’s categories. So to resolve, each script has its own getUrl($type, $dataID, $name, $subUrl) function, which uses switch…case and returns as to what the url should look like. So now I only have to remember as to what I specified as the type for each url format…

Code re-using

I have decided that if I am going to run same code via two different methods, then that code should have its own method. I have found it to be very useful when I have to change part of the code.

Functions with more power

It has come to my attention that it is much better if 1 function decides as to what should be the result, rather than figuring that out in many different functions and then making a call to that function. For example, when adding a log to my database. I would just called log_insert with the $log_text and $log_type. I found it to be more easier if I were to have the log_insert function write the $log_text depending on what is $log_type. So it just removed certain responsiblities from all of the other functions. So in the future, when I wanted to use the log_insert function at some other part of my code, it became less of a hassle since I did not had to look how I had done created $log_text for that $log_type.

Filename convention

It sure gets annoying when I found that I had used different ways of name my files over the years. Now, I have simplified to following: SystemName-subject.php
for example: for a guestbook script, I would name it: guestbook-functions.php to specify that this files has all of the generic functions. For a file that deals with certain aspect of the script, let’s say connecting with the database, then it would be guestbook-mysql.php and that file would contain all of the various functions to make it easier to do some of the routing things like converting Select results into an array.

Html Convetion

Here is a convention that I have decided to following when coding anything in html:

It makes it easier for me to locate where a certain option is, and view it.

Now, I am thinking of using Zend framework and see if I can further remove the issues that I come across, such as having to renaming, or trying to figure out the table format in a database. It is a real nuiscance every time I need to make a new query, and I can’t recall what naming format I have used, since i have changed my table-cell naming convention overtime, and I really dont want to go through my old code and rename it to fit my new convention.
Furthermore, I find myself to be more lazy when I have to write down something I have done hundreds, or thousands of times. I could really use a way to paste certain code, or highlight a variable and specify a function to wrap around it.

JCreator Le – Output to command prompt

  • Filed under: Java
Sunday
Aug 30,2009

1. Go to Configure -> Options...

2. Select “JDK Tools” in the left column.

3. Select Tool Type: Run Application

4. Click “<Default>“, click the “Edit…” button

5. Un-check the “Capture output” checkbox

6. Click OK twice.

Resource: http://www.skylit.com/javamethods/faqs/jcreator4.html

about:config changes

  • Filed under: Firefox
Saturday
Aug 29,2009

These are admin level changes you can make to firefox, to make it work differently to suit your need.

Type about:config into Firefox’s address bar and click the “I’ll be careful, I promise!” button.

“plugin does not provide secure updates”

When installing an addon from non-mozilla site, you might get the error “plugin does not provide secure updates.

1. To remove the error, right click on the screen. Go to New->Boolean:

Enter the preference Name: extensions.checkUpdateSecurity

2. By default, it will be selected to false, and you can leave it as is.

Make incompatiable extensions work

1. Follow the steps above, except use the preference name: extensions.checkCompatibility.

Slow address bar
In the address bar, I type a character and the firefox freezes for few seconds. Why? because it’s algorithm is searching through my history and bookmarks to find matches. Sometime it is annoying, like when I want to an url and I have to wait. So I have changed the following values in auto:config to make it work quickly.

browser.urlbar.matchBehavior = 2

browser.urlbar.maxRichResults = 5

browser.urlbar.search.timeout = 25

Resource:

1. http://dotnetwizard.net/soft-apps/firefox-fix-error-message-%E2%80%98plugin-does-not-provide-secure-updates-the-plugin-will-not-be-installed%E2%80%99/

2. http://lifehacker.com/355973/make-your-extensions-work-with-the-firefox-35

3. http://blog.kagesenshi.org/2008/08/getting-around-slow-firefox3-urladdress.html

Friday
Aug 28,2009

I have spent about 5-6 hours tried to get glut to work. Here is what finally worked for me.

1. Follow the steps at: http://www-users.itlabs.umn.edu/classes/Spring-2009/csci4107/GlutSetupWin.html

2. IF you recieve the following error:

1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdlib.h(371) : error C2381: ‘exit’ : redefinition; __declspec(noreturn) differs
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\gl\glut.h(146) : see declaration of ‘exit’
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdlib.h(371) : warning C4985: ‘exit’: attributes not present on previous declaration.
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\gl\glut.h(146) : see declaration of ‘exit’
1>c:\users\harp\desktop\contacts\[2009]3.fall\cs 116a\hw1\main.cpp(100) : error C3861: ‘exit’: identifier not found

then, follow step 3.[Reference = http://stackoverflow.com/questions/14264/using-glut-with-visual-c-express-edition].

3. Open glut.h.

4. Find extern _CRTIMP void __cdecl exit(int); and replace it with the following code:

/* extern _CRTIMP void __cdecl exit(int);  /* Changed for .NET */
#  if _MSC_VER >= 1200
extern _CRTIMP __declspec(noreturn) void __cdecl exit(int);
#  else
extern _CRTIMP void __cdecl exit(int);
#  endif