Connecting to MySQL is pretty simple. There are several different ways of doing it through PHP like MySQLi and PDO. The way I usually do it is creating a mysql class that contains the connect, close, select, insert, and all the other functions readily needed for using PHP and MySQL. Unfortunately some clients that I work with don’t always use PHP 5 or higher..
So here is the way to connect just using functions so that it will work with earlier versions of PHP.
You can assign the database connection properties as variables, contants, or hard coded. Today I am going to pass variables into the function. You could also get much more detailed with it and make error handling if connection fails, but again… not today.
Also below is all the necessary PHP MySQL function. Check em. Good for Reference when not using a framework.
<?php
$link = mysql_connect("DB_HOST", "DB_USER", "DB_PASSWORD") or die(mysql_error());
?><?php
mysql_close($link);
?><?php
// INSERT
$result = mysql_query("INSERT INTO table(field1, field2) VALUES('value1', 'value2' ) ") or die(mysql_error());
// SELECT
$result = mysql_query("INSERT INTO table(field1, field2) VALUES('value1', 'value2' ) ") or die(mysql_error());
// WHERE
$result = mysql_query("SELECT * FROM table WHERE field1='value1'") or die(mysql_error());
// ORDER BY
$result = mysql_query("SELECT * FROM table ORDER BY fieldName") or die(mysql_error());
// GROUP BY with ORDER BY (NOTE: GROUP BY must come before ORDER BY)
$result = mysql_query("SELECT * FROM table GROUP BY fieldName ORDER BY fieldName") or die(mysql_error());
// LIMIT
$result = mysql_query("SELECT * FROM `your_table` LIMIT 0, 10") or die(mysql_error());
// WHERE CONDITIONALS ..more
$result = mysql_query("SELECT * FROM table WHERE field1='value1' OR field2='value2'");
$result = mysql_query("SELECT * FROM table WHERE field1='value1' && field2='value2'");
// COMPARISONS
$result = mysql_query("SELECT * FROM table WHERE field1 LIKE value%");
$result = mysql_query("SELECT * FROM table WHERE field1 NOT LIKE value%");
$result = mysql_query("SELECT * FROM table WHERE field1 LIKE value_");
$result = mysql_query("SELECT * FROM table WHERE field1 LIKE value__");
$result = mysql_query("SELECT * FROM table WHERE field1 LIKE value%");
// SUB QUERY
$result = mysql_query("SELECT * table1 WHERE table2_id IN(SELECT table2_id FROM table2 WHERE data = data )");
?>| = | equals |
| < | less than |
| > | greater than |
| <= | less than or equal to |
| >= | greater than or equal to |
| != | not equal to |
| IS NOT NULL | has any value |
| IS NULL | has no value at all |
| BETWEEN | within a specified range |
| NOT BETWEEN | outside a specified range |
| OR | one of two equations are true |
| || | same as OR |
| AND | both equations are true |
| && | same as AND |
| NOT | both equations are false |
| ! | same as NOT |
| LIKE | Simple pattern matching | ||||
| NOT LIKE | Negation of simple pattern matching | ||||
|
|||||
| STRCMP() | Compare two strings | ||||
UPDATE!! A plugin is now available for this tutorial that includes more features read more...
When running a site with a boat load of javascript, you may want to run a script read more...
Download Plugin THEMES: theme-lighttheme-dark<link href="{{ path to js read more...
Greetings All! Since everyone was digging the Star Comment Rating I decided that read more...
How often do you work with tables? I work with them quite a bit and styling t read more...
This is a great right up. I’ll be using this tomorrow to try and reconnect MySql. Also, don’t forget your writeup about the mod rewrite and .htaccess. Thanks agian.Good but you forgot to select databasemysql_select_db( ‘database_name’,$link_connect);
i think u forget a lot of functions which u could use in order to manage your MySQL DB inside your php code likemysql_fetch_array()
mysql_fetch_object()
mysql_fetch_assoc()
mysql_fetch_field()
mysql_fetch_lengths()
mysql_fetch_row ()
mysql_real_escape_string()
mysql_num_rows()
mysql_create_db ()
mysql_result ()
and those which i mentioned, is which have a high priority and using frequently
thanks
Hi Ahmed,You are absolutely correct. I’ve been meaning to put those on there! Thanks
I strongly recomment moving the the mysql pdo extension. It’s structure is object oriented and I have read that the mysql_xxxx libs will get deprecated in the future version of PHP. You can extend the pdo_mysql class.