Connect to database with php

Before you can pull data from a database you must first establish a connection to it. Connecting to a database with php is very simple.

<?php
// Create connection
$con=mysqli_connect("rubelix.com","bob","password","my_db");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

Note that when your database is on the same server (which is very common on hosting servers) you can replace the domain name in the connection string with the word ‘localhost‘. It will then look like below:

<?php
// Create connection
$con=mysqli_connect("localhost","bob","password","my_db");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

Simple php Arrays

Arrays in php are ordered maps. An array in its simplest form is simply a list of values using the same variable name. Below is how you create a simple array in php:

Simple array
<?php

$pets = array(“dog”,”cat”,”mouse”,”bird”);

?>

What is SOLID?

SOLID is an acronym that stands for the first 5 principles of Object Oriented design. It helps to create code that can be easily maintained and extended in the future.

S – Single Responsibility Principle – each class should do one thing and one thing only. It should also do it very well

O – Open/Closed Principle – Entities are Open for extension, Closed for modification.

L – Liskov Substitution Principle – Objects in a program should be able to be replaced with instances of their subtypes without affecting the correctness of the program.

I – Interface segregation principle – many client-specific interfaces are better than one general-purpose interface.

D – Dependency Inversion Principle – Depend upon abstractions, not on concretions.

Escaping special characters

Sometimes you will need to use special characters but not want them to be interpreted as such. For example, if you have a string of “This is the “best” steak I ever had” the string would stop at “This is the “. In order to allow the quotes around the word steak to be accepted as part of the string, you will need to escape them. “This is the /”best/” steak I ever had” is correct. There are several special characters you may want to escape in javascript. Below is the list of how to do so.

\’ single quote
\” double quote
\\ backslash
\n new line
\r carriage return
\t tab
\b backspace
\f form feed

lastIndexOf()

Much like the indexOf() method, but this one finds the location of the last iteration of the specified text. It should be 22, as that is where the last time ‘dog’ is mentioned.

var sentence = “Which dog is the best dog?”;
sentence.lastIndexOf(“dog”);

indexOf()

This method will return a number representing the position in the string where it finds the first occurrence of the desired text. In this case it should be 16.

var sentence = “The quick brown fox jumped over the lazy dog”;
sentence.indexOf(“fox”);

javascript objects

Objects in javascript are nothing more than data with specific properties. Below is a general object of a dog:

var dog= {name:”Fido”, breed:”Beagle”, age:4, furColor:”brown”};

To access the object’s properties, simply call them like so:
name = dog.name;

Common javascript events

Below are a few of the more common events used by javascript:

Event Description
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page

 

console.log()

Console.log is helpful for debugging javascript code. It allows you to print to the console as opposed to printing to an HTML file. Not all browsers support the console function so if the one you are using does not, get a better browser.

To view the console, hit F12 on your browser and select the Console option.

Example: This example will print the result of ‘c’ to the console, which is 7. It is not displayed on the HTML file so it does not show in the browser window,

<!DOCTYPE html>
<html>
<body>

<h1>Test Page</h1>

<script>
a = 1;
b = 6;
c = a + b;
console.log(c);
</script>

</body>
</html>