MySQLi Tutorial: Connect, Select, Display, Close.

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:

CREATE TABLE IF NOT EXISTS `people` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `age` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
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:

<?php
/* 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 */
$sql->close();
?>

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!

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

PHP Tutorial: Introduction

In order to use PHP you will need three things:

  • Webserver (or webhost) with PHP installed
  • A text editor
  • A web browser
  • Optional: a database, such as MySQL

We’ll begin with getting in to PHP, so open up your text editor and enter the following (don’t copy and paste, you’ll learn slower!)

<?php
 
?>

Your PHP code should always start and end with the above code. Although PHP has other tags that can be used such as <% %> (ASP tags) and (these are short tags), the ASP tags will not be working as of PHP6, and some people feel that the short tags are bad practice.

Ok back to the tutorial! Sometimes while you’re coding, you might want to leave yourself a note or two. You can accomplish this with comments. PHP has three types of comments, as displayed below.

<?php
#  The first comment type is a hash comment
#  anything after the hash, on the same line
#  will be ignored by PHP.

// the second type is the double forward slash comment
// anything after the double slashes, on the same line
//  will be ignored by PHP
 
/*  The third type is the multi-line comment
anything between the opener: / * (no space)
and the closer: * / (again, no space)
will be ignored by PHP */
?>

And there you have it, some basic syntax to help you with your PHP development!

Hope to see you in the next tutorial.

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