-
Connecting to MySQL
To connect to MySQL on our servers, you can use this format:
<?php
$link=mysql_connect("localhost","USER_NAME","PASSWORD");
mysql_select_db("DATABASE_NAME");
$query = mysql_query("SELECT * FROM `your_database` WHERE (`your_field` = '$some_variable') ");
if (mysql_num_rows($query) > 0) {
while ($info=mysql_fetch_array($query)) {
//your code commands go here....
}
}
mysql_close($link);
?>
top
-
Connecting to Postgres
To connect to Postgresql on our servers, you can use this format:
<?php
$pg_connection = pg_connect("dbname=template1 user=YOUR_USERNAME password=YOUR_PASSWORD");
$query = pg_exec ("SELECT * FROM your_database");
for ($row = 0; $obj = @pg_fetch_object ($query, $row); $row++) {
//code can go here or to narrow results down, use this below:
if(stristr($obj->usename, "$your_variable")) {
//code commands go here....
}
}
pg_freeresult ($query);
?>
top
-
Code Examples
Here are some code examples. You can always find out much more at php's website. They've got many comments/code examples.
<?php
// To check if a file exists using http or ftp use the following:
$fp = @fopen("http://www.someurl.com/testfile.htm","r");
if ($fp)
{ print"The file exists!"; }
else
{ print"The file does not exist"; }
// Note: The "@" in front of fopen suppresses the error output of the function.
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file ('http://www.example.com/');
// Loop through our array, show html source as html source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #{$line_num} : " . htmlspecialchars($line) . "
\n";
}
// Another example, let's get a web page into a string. See also file_get_contents().
$html = implode ('', file ('http://www.example.com/'));
// this will create a new file and write to it using vi editor
$f=popen("vi newfile.txt","w");
sleep(1);
fputs($f,"i"); // insert
sleep(1);
fputs($f,"Hello world\r"); // write the text to vi Editor
sleep(1);
fputs($f,chr(27)); // cancel action
sleep(1);
fputs($f,":wq\r"); // write and quit
pclose($f);
// this is how you open a directory and read all the contents
$handle=opendir($my_directory."/");
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." ) {
//your commands go here
}
}
closedir($handle);
?>
top
-
PHP Programming Links
here are some excellent sites that have help forums and code examples:
:: php Builder
:: php Club
:: Hot Scripts
:: Resource Index
:: WebMonkey
top