Saturday, July 5, 2008

Connecting MySQL wid PHP

Opening a connection to MySQL database from PHP is easy. Just use the mysql_connect() function like this

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'petstore';
mysql_select_db($dbname);
?>

$dbhost is the name of MySQL server. When your webserver is on the same machine with the MySQL server you can use localhost or 127.0.0.1 as the value of $dbhost. The $dbuser and $dbpass are valid MySQL user name and password. For adding a user to MySQL visit this page : MySQL Tutorial

Don't forget to select a database using mysql_select_db() after connecting to mysql. If no database selected your query to select or update a table will not work.


Sometimes a web host will require you to specify the MySQL server name and port number. For example if the MySQL server name is db.php-mysql-tutorial.com and the port number is 3306 (the default port number for MySQL) then you you can modify the above code to :

$dbhost = 'db.php-mysql-tutorial.com:3306';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'petstore';
mysql_select_db($dbname);
?>

It's a common practice to place the routine of opening a database connection in a separate file. Then everytime you want to open a connection just include the file. Usually the host, user, password and database name are also separated in a configuration file.

An example of config.php that stores the connection configuration and opendb.php that opens the connection are :

Source code : config.phps , opendb.phps

// This is an example of config.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'phpcake';
?>

// This is an example opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>

So now you can open a connection to mysql like this :

include 'config.php';
include 'opendb.php';

// ... do something like insert or select, etc

?>

Something About Javascript

is a versatile language. It can be used to create menus, validate forms, provide interactive calendars, post the current day's headlines, produce background effects on a Web page, track a visitor's history on your site, and play games, among many other things. That's probably why it's one of the most popular languages on the World Wide Web.

Netscape created JavaScript in 1995. Originally called "LiveScript," it was designed to make Web pages more interactive. In the beginning the language was plagued with security problems which, for the most part, have been overcome. The current version of JavaScript is 1.5.

Differences between C and C++

Implicit Assignment from void*

You cannot implicitly assign from a void* to any other type. For instance, the following is perfectly valid in C (in fact, it's arguably the preferable way of doing it in C)
int *x = malloc(sizeof(int) * 10);
but it won't compile in C++. (Try it yourself!)

The explanation from Bjarne Stroustrup himself is that this isn't type safe. What this means is that you can have a void* that points to anything at all, and if you then assign the address stored in that void* to another pointer of a different type, there isn't any warning at all about it.

Consider the following:
int an_int;
void *void_pointer = &an_int;
double *double_ptr = void_pointer;
*double_ptr = 5;
When you assign *double_ptr the value 5, it's writing 8 bytes of memory, but the integer variable an_int is only 4 bytes. Forcing a cast from a void pointer makes the programmer pay attention to these things.

Freeing arrays: new[] and delete[]

In C, there's only one major memory allocation function: malloc. You use it to allocate both single elements and arrays:
int *x = malloc( sizeof(int) );
int *x_array = malloc( sizeof(int) * 10 );
and you always release the memory in the same way:
free( x );
free( x_array );
In C++, however, memory allocation for arrays is somewhat different than for single objects; you use the new[] operator, and you must match calls to new[] with calls to delete[] (rather than to delete).
int *x = new int;
int *x_array = new int[10];

delete x;
delete[] x;
The short explanation is that when you have arrays of objects, delete[] with properly call the destructor for each element of the array, whereas delete will not.

You must declare functions before use

Although most good C code will follow this convention, in C++ it is strictly enforced that all functions must be declared before they are used. This code is valid C, but it is not valid C++:
#include 
int main()
{
foo();
return 0;
}

int foo()
{
printf( "Hello world" );
}

Gotcha for a C++ programmer using C

Structs and Enums

You have to include the struct keyword before the name of the struct type to declare a struct: In C++, you could do this
struct a_struct
{
int x;
};

a_struct struct_instance;
and have a new instance of a_struct called struct_instance. In C, however, we have to include the struct keyword when declaring struct_instance:
struct a_struct struct_instance;
In fact, a similar situation also holds for declaring enums: in C, you must include the keyword enum; in C++, you don't have to. As a side note, most C programmers get around this issue by using typedefs:
typedef struct struct_name
{
/* variables */
} struct_name_t;
Now you can declare a struct with
struct_name_t struct_name_t_instance;
But there is another gotcha for C++ programmers: you must still use the "struct struct_name" syntax to declare a struct member that is a a pointer to the struct.
typedef struct struct_name
{
struct struct_name instance;
struct_name_t instance2; /* invalid! The typedef isn't defined yet */
} struct_name_t;

C++ has a much larger library

C++ has a much larger library than C, and some things may be automatically linked in by C++ when they are not with C. For instance, if you're used to using g++ for math-heavy computations, then it may come as a shock that when you are using gcc to compile C, you need to explicitly include the math library for things like sin or even sqrt:
% g++ foo.cc

or

% gcc foo.c -lm

No Boolean Type

C does not provide a native boolean type. You can simulate it using an enum, though:
typedef enum {FALSE, TRUE} bool;

main Doesn't Provide return 0 Automatically

In C++, you are free to leave off the statement 'return 0;' at the end of main; it will be provided automatically:
int main()
{
printf( "Hello, World" );
}
but in C, you must manually add it:
int main()
{
printf( "Hello, World" );
return 0;
}