Kate's Code Derps

A topnotch WordPress.com site

PHP – Debugging Tip: file_put_contents — December 21, 2012
TIP: SQL Query – Replacing Rows With Same Data — December 13, 2012
TIP: CSS Opacity vs. RGBA Background — December 12, 2012
Code Snippet: AS3 Has Property Check — December 10, 2012
AS3 Constant Variables —
PHP: var_dump($variable) — December 7, 2012
PHP – Couldn’t fetch mysqli — December 5, 2012

PHP – Couldn’t fetch mysqli

In my unfamiliarity with mysqli, I fought with this one a while before figuring out that, no duh, closing the mysqli with mysqli_close($mysqli); destroys the connection and makes subsequent calls (I was logging in, closing, then trying to insert using the same mysqli link) impossible.

Solution: As scripts automatically close database connections when they complete, we don’t technically have to write the close ourselves unless we need to force it to close on long running processes that don’t need the connection to be open for it. But since I’m doing very quick pulls and inserts and no real processing, I can just let the PHP close the connection for me.

 

the connection resource is automatically destroyed after the processing on a page request ends. To SOLVE, I have to re-require connect.php (my connection to the DB) for each time I send out the req.

 

 

Accessing Nested Associative Array from JSON_Decode in PHP — December 4, 2012
PHP, calling variables from one PHP to another with require_once using Global —

PHP, calling variables from one PHP to another with require_once using Global

I struggled forever trying to figure out the correct way to refer to variables of an include_once or require_once php file from inside a function of anyone PHP class. I got as far as defining global variables…but everything I read seemed to miss one key part, or at least un-emphasize it enough that I missed it: You have to REDECLARE the global variable in the calling PHP before using.

first.php defines a variable with global $puppies.

second.php wants to call that variable in the function CountPuppies. I have to first write my include_once or require_once before that call, then redeclare global $puppies before I do a call like $count = $puppies.

SQL vs SQLI —

SQL vs SQLI

Any tutorials telling you to use commands like mysql_select_DB is out of date. It should all be mysqli stuff now, and you instantiate it at the top of your connect.php with

$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);

 

Yeah, that one was fun to figure out.