Laravel Artisan Migrate "Table Already Exists" or "Foreign Key" Error

While deploying a project I had developed half a year earlier with Laravel 5.2, I ran into quite a few problems. One of them: after running php artisan migrate, I got a “Table Already Exists” error. But I had never created that table before. Digging deeper, I found that the table did exist, but there was no record for it in the project_migrations table. So I deleted the table manually and ran php artisan migrate once more. Sure enough, this time the real error message appeared: “General error: 1005 Can’t create table ‘dbname.#sql-fbc1_15e6’ (errno: 121) (SQL: alter table karl_project add constraint project_user_id_foreign foreign key (user_id) references karl_user (id))” ...

December 12, 2016 · 2 min

Facebook Graph API: How to Fix Missing Email Field for Some Users

I recently ran into a rather interesting little problem. A contest voting system we built had occasional users who couldn’t vote. The system was built with Laravel. Users could log in with Facebook to vote for a designated entry, one vote per day. We only collected the most basic Email and Name data, for the prize draw. Everything was fine during testing, but after launch, a few users complained that after voting they saw the error message “whoops looks like something went wrong,” while most users could vote normally. ...

December 2, 2016 · 3 min

Compiling and Installing a PHP Extension Manually (Linux)

If you want to install an extension for PHP, what should you do? Many people will answer Pecl. That is indeed a very convenient way to install extensions. But what if your server does not have Pecl? And what if, for some reason, you cannot install Pecl? Then you have no choice but to compile and install the extension manually~ This post shows you how to manually compile a PHP extension on Linux. ...

January 26, 2012 · 2 min

PHPConf Takeaways

To be honest, my first language was VB, but the one I actually use most and know best is PHP. For example, when I took part in the Taipei City Software Competition, I solved every problem in PHP, since that’s what I know best~ So when I heard PHPConf was happening, I was so happy that I signed up to be a staff member XD ...

December 3, 2011 · 3 min

[PHP] Listing Files in a Folder with glob, scandir, and readdir

Sometimes you need to write a PHP program that gets a list of the files in a specified folder. For example, I needed this feature when I was making an online hard drive program. Here I’ll introduce three functions that can all do this, though each is used a bit differently: glob, scandir, and readdir. ...

August 17, 2011 · 2 min

Changing PHP's Execution Time Limit to Keep Long-Running Scripts from Being Stopped

By default, PHP has a 30-second execution limit. When a program runs longer than 30 seconds, you get an error like this: “Fatal error: Maximum execution time of 30 seconds exceeded”. This is a safeguard against bugs, such as infinite loops or other strange code that could end up bringing the system down. However, sometimes your program really needs to run for more than 30 seconds. For example, you might be processing a very large file in PHP, such as a database backup that can be hundreds of MB, which takes a long time and can easily exceed 30 seconds. My earlier Plurk BOT and spider programs also ran into this easily. Here I’ll show you two ways to adjust this time limit. ...

August 9, 2011 · 2 min

How to Defend Against Basic SQL Injection in PHP?

For the past few days I’ve been writing a small program to organize contacts, so I’ve been researching quite a few things. While working on the database side, I wanted to guard against so-called SQL injection, where a malicious string is placed in a parameter to attack the database. So I applied some basic defenses. The following is how to add some basic protection against SQL injection~ ...

August 2, 2011 · 3 min

How to Embed Arrays, $_POST, and $_GET Directly in a PHP String (SQL Query)

Over the past few days, while writing a small program, I ran into a problem. I wanted to put a $_POST variable directly into my SQL query string instead of using the concatenation operator. But because of quoting issues, it took me quite a while to figure out how. Here is a brief introduction. I also want to thank Bu and Cang Shixian for teaching me this method! If it weren’t for them, I might still not know about it. Also, this method isn’t limited to $_POST; it works with other arrays too. ...

August 1, 2011 · 2 min

Solving Pascal's Triangle with a Formula! (Thanks to Cang Hai)

The image above is from Wikipedia and is used to explain Pascal’s triangle. Today’s topic is the so-called Pascal’s triangle. Some people also call it Yang Hui’s triangle or Jia Xian’s triangle. The first row’s first entry is “1”. Each number below equals the sum of the number to its upper left and the number to its upper right. If there is no number to the upper left or upper right, it counts as 0. For example, the second row has two numbers, “1, 1”. The third row is “1, 2, 1”. And this triangle can extend infinitely. ...

July 24, 2011 · 2 min

Quickly Building a Multilingual Program with Gettext for PHP

Gettext is a… um, I’m not sure how to describe it. It’s something like a specification, but it’s also a set of libraries. Anyway, it makes it easy to make your program multilingual~ This article is about using Gettext with PHP to develop multilingual programs and websites. ...

July 18, 2011 · 4 min