ArchivesPHP and MySQLi Tutorials of

PHP Tutorial: No IF’s, ELSEIF’s, or ELSE’s about it!

Once you’re comfortable with strings and variables, it’s time to really put them into action. You can accomplish this byusing PHP’s if/elseif/else structure, don’t worry I’ll explain. The if() structure evaluates an expression, that expression will return either TRUE or FALSE. Let’s do an example:

/* we'll set a variable, and use it in an if() statement */
$mynumber = 10;
/* now we build the if() {} else {} statement */
if ( $mynumber > 5 ) {
    echo 'The variable $mynumber is greater than 5!';
} else { /*otherwise, it must not be greater than 5 */
    echo 'The variable $mynumber is not greater than 5.';

So let’s talk about what just happened. We told PHP to check if the variable $mynumber was greater than. In our case, $mynumber was greater than so that example should display: The variable $mynumber is greater than 5! After that, if the expression ($mynumber > 5) returns false the else code block will come into play, and display: The variable $mynumber is not greater than 5.

“Mike you mentioned elseif, what’s that?” Well the elseif() goes between the if() {} and the else {}, like another level of expression checking. Let’s see a new example:

/* so first, we'll set a variable
this time we'll use a string */
$apple = 'red';
/* now we'll build our if/elseif/else structure 
We're going to use the == (equal to) operator */
if ( $apple == 'green' ) {
    echo 'The apple is green!';
} elseif ( $apple == 'yellow' ) {
    echo 'The apple is yellow!';
} else {
    echo "The apple is not green or yellow, but $apple.";

Note: When setting a variable or echoing a string, or ending any statement in PHP be sure to end your statement with a semi-colon ( ; ).

So to go over what just happened: We set a variable named $apple and set it to have a value of red. We then asked PHP to check if that variable had a value of green, and if not check if it has a value of yellow, and once again, if not, execute the code inside the else {} code block, which will echo out what color our $apple actually is.

Hopefully that helps you understand the structure behind an if/elseif/else structure.

Thanks for reading, I hope you found this helpful!

By Mike on March 23, 2011 | PHP, Tutorials
Tags: , ,

MySQLi Tutorial: Connect, Select, Display, Close.

Here is the table I’ll be using in this example, copy and paste it into your MySQL admin tool, commonly phpMyAdmin:

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `age` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
INSERT INTO `people` (`id`, `name`, `age`) VALUES(1, 'Mike', 26);
INSERT INTO `people` (`id`, `name`, `age`) VALUES(2, 'Joe', 19);
INSERT INTO `people` (`id`, `name`, `age`) VALUES(3, 'Stephanie', 18);
INSERT INTO `people` (`id`, `name`, `age`) VALUES(4, 'Angela', 21);

Ok, lets dive right in, first we’ll need to connect to our database, using mysqli:

/* so we start by intializing the mysqli class
we will pass our credentials into the class initilization 
the order: host, username, password, database*/
$sql = new mysqli('localhost','username','password','database');
/* ok so we've connected, let's write our query, 
we'll set a variable called name */
$name = 'Mike';
$query = $sql->query("SELECT `age` FROM `people` WHERE `name` = '$name'");
$result = $query->fetch_object();
echo "$name is {$result->age} years old.";
/* ok we're done, let's close our connection */

And there we have it, a very simple connect, and display using the mysqli extension in PHP5. If you are coming from the mysql_* procedural functions, MySQLi can take some getting used to, fiddling with simple selects like this should help you get familiar with the syntax of using the class. If you are used to using classes (of your own or pre-made) this should come easily to you.

I hope you found this useful, thanks for reading!

What is MySQLi? What does it do?

MySQLi is an extension for PHP, often referred to as MySQL improved. It was introduced in PHP 5.0, and will be in every version following (until something better comes along). It allows you to use all of the MySQL database (version 4.1.3 or newer) servers features. Some features of MySQLi are (from the PHP manual):

  • It’s object oriented interface, making it easier to use
  • Support for prepared statements, helping secure you code
  • Support for multiple statements, allowing you to run more than one query at a time

MySQLi not only has an object oriented interface but also a prodecural one. Making it even more widely usable.

Example of connection with the object oriented method:

$sql = new mysqli('localhost','username','password','database');

Example of connection with the procedural method:

$link = mysqli_connect('localhost','username','password','database');

And that is a nice, quick, simple introduction to the MySQLi extension for PHP.

By Mike on | MySQLi
Tags: , ,