\<\?php
//Step 1: connecting to database server
$connection = mysql_connect("localhost","root","");
//("server","user","password")
//Step 2: selecting a database
$sel_database = mysql_select_db("database_name"); //("name of the database that you want to connect to")
//Setp 3: doing the query
$query = "SELECT * FROM table1";
$result = mysql_query($query,$connection);
//Step 4: presenting retrieved data
while ($row = mysql_fetch_array($result)){
foreach($row as $value){
echo $value."\t";
}
echo "
";
}
//Step 5: closing the connection
mysql_close($connection);
?>
Sunday, February 8, 2009
Connecting to MySQL Database in 5 Simple steps using php
Thursday, February 5, 2009
Some useful PHP function (RegEx):
- Email address validation:
function isEmail($email){
$regex = "/\w+@\w+.\w+/";
return preg_match($regex, $email);
} - URL validation:
function isURL($url){
$regex = "/(https?ftpfile):\/\/.\w+.\w\w+/";
return preg_match($regex, $url);
} - Validate IP:
function isIp($ip){
$block = "(25[012345]2[01234]\d[01]?\d\d?)";
$regex = "/^($block\.$block\.$block\.$block)$/";
return preg_match($regex, $ip);
} - Is MAC address:
function isMac($mac, $separator){
$mac = strtoupper($mac);
$block = "([A-F][A-F][A-F][0-9][0-9][A-F][0-9][0-9])";
$regex = "/^($block$separator$block$separator$block$separator$block$separator$block$separator$block)$/";
return preg_match($regex, $mac);
} - Is this a private IP:
function isPrivateIp($ip){
// 10.0.0.0 – 10.255.255.255
// 172.16.0.0 – 172.31.255.255
// 192.168.0.0 – 192.168.255.255
$block = "(25[012345]2[01234]\d[01]?\d\d?)";
$rangeA = "/^(10)\.($block)\.($block)\.($block)$/";
$rangeB = "/^(172)\.(3[01]2[0-9]1[6-9])\.($block)\.($block)$/";
$rangeC = "/^(192)\.(168)\.($block)\.($block)$/";
$regex = "/^($block\.$block\.$block\.$block)$/";
if (preg_match($rangeA, $ip))
return true;
else if (preg_match($rangeB, $ip))
return true;
else if (preg_match($rangeC, $ip))
return true;
else if (preg_match($regex, $ip))
return false;
else
return false;
} - Is it a class A private ip or B or C:
function getPrivateIpClass($ip){
// 10.0.0.0 – 10.255.255.255
// 172.16.0.0 – 172.31.255.255
// 192.168.0.0 – 192.168.255.255
$block = "(25[012345]2[01234]\d[01]?\d\d?)";
$rangeA = "/^(10)\.($block)\.($block)\.($block)$/";
$rangeB = "/^(172)\.(3[01]2[0-9]1[6-9])\.($block)\.($block)$/";
$rangeC = "/^(192)\.(168)\.($block)\.($block)$/";
$regex = "/^($block\.$block\.$block\.$block)$/";
if (preg_match($rangeA, $ip))
return "A";
else if (preg_match($rangeB, $ip))
return "B";
else if (preg_match($rangeC, $ip))
return "C";
else if (preg_match($regex, $ip))
return $ip." is not a private IP.";
else
return $ip." is not a valid IP.";
}
Monday, February 2, 2009
C#: Reading output from PHP
Steps (MS Visual C#):
- Create a new Console Application
- Declare four variables req, response, output and reader as WebRequest, WebResponse, Stream and StreamReader
- Create an web request using WebRequset.Create(string url)
(e.g. req = WebRequest.Create(http://localhost/index.php);) - Set WebRequest method as POST
(req.Method = "POST"); - Set :
response = req.GetResponse();
output = response.GetResponseStream();
reader = new StreamReader(output); - Now reader has the output from the PHP file
- To read from the reader, reader.ReadLine() method can be used
Example:
(C# : phpEchoReader.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;namespace phpEchoReader {
class Program {
static void Main(string[] args) {
WebRequest req;
WebResponse response;
Stream output;
StreamReader reader;
req =
WebRequest.Create("http://localhost/index.php");
req.Method = "POST";
response = req.GetResponse();
output = response.GetResponseStream();
reader = new StreamReader(output);
while (!reader.EndOfStream)
{
Console.WriteLine(reader.ReadLine());
}
Console.WriteLine("Press any key too continue...");
Console.ReadLine();
}
}
}
(php code: index.php)
for ($i = 0; $i <>
echo $i."\n";
?>
Output:
Subscribe to:
Posts (Atom)