A Little Noise

October 1, 2012

Connection Conundrum

Filed under: MySQL Gotchas,PHP Gotchas — snoyes @ 12:13 pm

Define a user like this:

GRANT ALL ON *.* TO 'myuser'@'localhost' IDENTIFIED BY 'super$ecret';

Then try a PHP script like this:

<?php
mysqli_connect("localhost", "myuser", "super$ecret");
?>

What happens and why? How could you avoid it?

And for glory, what single line could you add that would prevent the error, without making any changes to the mysqli_connect line?

November 15, 2005

Static Variable Variables

Filed under: PHP Gotchas — snoyes @ 4:00 am
<?php
  class A {
    static function b() {
      echo "It works\n";
    }
  }
  $theClass = "A";
  $x = new $theClass;

  /* Are any of the following legal? Which? */

  A::b();
  $x->b();
  $theClass::b();
?>

Show Answer ▼

November 14, 2005

Form Names

Filed under: PHP Gotchas — snoyes @ 4:00 am

Given the following page:

<form>
  <input type="radio" name="BT.123" value="active" />
  <input type="radio" name="BT.123" value="inactive" />
  <input type="submit" value="Submit" />
</form>

<?php
  echo "GET: ", $_GET['BT.123'];
  echo "POST: ", $_POST['BT.123'];
  echo "REQUEST: ", $_REQUEST['BT.123'];
?>

Assume the “inactive” button is checked and the form is submitted. What is the output?
Show Answer ▼

November 12, 2005

Post increment

Filed under: PHP Gotchas — snoyes @ 4:00 am
<?php
$a = 4;
echo $a+++$a++;
?>

What is the output?
Show Answer ▼

November 11, 2005

Concatenate

Filed under: PHP Gotchas — snoyes @ 4:00 am

What, if anything, is the minimum change (fewest inserted/deleted/modified characters) required to make the output The result is bar:


<?php
class A {
  private $foo = "bar";
  public function __toString() {
    return $this->foo;
  }
}

$myObject = new A();
echo "The result is " . $myObject;
?>

Show Answer ▼

Powered by WordPress