Hi all,
Im trying to follow the PHP fast and easy web development book. But i want to use postgresql instead of mysql. So now I need help converting the example php scripts to work with postgresql.
<?
//indicate the database you want to use
$db_name = "testDB";
//connect to database
$connection = pg_connect("host=localhost user=herrapa password=herrapa")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or
die(mysql_error());
//start creating the SQL statement
$sql = "CREATE TABLE $_POST[table_name] (";
//continue the SQL statement for each new field
for ($i = 0; $i < count($POST[field_name]); $i++) {
$sql .= $POST[field_name][$i]." ".$_POST[field_type][$i];
if ($POST[field_length][$i] != "") {
$sql .= " (".$POST[field_length][$i]."),";
} else {
$sql .= ",";
}
}
//clean up the end of the string
$sql = substr($sql, 0, -1);
$sql .= ")";
//execute the query
$result = pg_query($sql,$connection) or die(mysql_error());
//get a good message for display upon success
if ($result) {
$msg = "<P>".$_POST[table_name]." has been created!</P>";
}
?>
<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 3</TITLE>
</HEAD>
<BODY>
<h1>Adding table to <? echo "$db_name"; ?>...</h1>
<? echo "$msg"; ?>
</BODY>
</HTML>
*************************************************'
<?
//check for required fields
if ((!$POST[f_name]) || (!$POST[l_name]) || (!$POST[username])
|| (!$POST[password])) {
header( "Location: http://127.0.0.1/show_adduser.html");
exit;
}
//set up the names of the database and table
$db_name = "testDB";
$table_name = "auth_users";
//connect to the server and select the database
$connection = pg_connect("host=localhost user=herrapa password=herrapa")
or die(mysql_error());
$db = @pg_select($db_name, $connection) or
die(mysql_error());
//create and issue query
$sql = "INSERT INTO $table_name (f_name, l_name, username,
password)
VALUES ('$POST[f_name]', '$POST[l_name]', '$POST[username]',
password('$POST[password]'))";
$result = pg_query($sql,$connection) or die(mysql_error());
?>
<HTML>
<HEAD>
<TITLE>Add a User</TITLE>
</HEAD>
<BODY>
<H1>Added to auth_users:</H1>
<P><STRONG>First Name:</STRONG><BR>
<? echo "$_POST[f_name]"; ?></p>
<P><STRONG>Last Name:</STRONG><BR>
<? echo "$_POST[l_name]"; ?></p>
<P><STRONG>Username:</STRONG><BR>
<? echo "$_POST[username]"; ?></p>
<P><STRONG>Password:</STRONG><BR>
<? echo "$_POST[password]"; ?></p>
<P><a href="show_adduser.html">Add Another</a></p>
</BODY>
</HTML>
**************************************************'
<?
//validate important input
if ((!$POST[table_name]) || (!$POST[num_fields])) {
header( "Location: http://127.0.0.1/show_createtable.html");
exit;
}
//begin creating form for display
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"do_createtable.php\">
<INPUT TYPE=\"hidden\" NAME=\"table_name\"
VALUE=\"$_POST[table_name]\">
<TABLE CELLSPACING=5 CELLPADDING=5>
<TR>
<TH>FIELD NAME</TH><TH>FIELD TYPE</TH><TH>FIELD LENGTH</TH></TR>";
//count from 0 until you reach the number of fields
for ($i = 0; $i < $_POST[num_fields]; $i++) {
//add to the form, one row for each field
$form_block .= "
<TR>
<TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"field_name[]\"
SIZE=\"30\"></TD>
<TD ALIGN=CENTER>
<SELECT NAME=\"field_type[]\">
<OPTION VALUE=\"char\">char</OPTION>
<OPTION VALUE=\"date\">date</OPTION>
<OPTION VALUE=\"float\">float</OPTION>
<OPTION VALUE=\"int\">int</OPTION>
<OPTION VALUE=\"text\">text</OPTION>
<OPTION VALUE=\"varchar\">varchar</OPTION>
</SELECT>
</TD>
<TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"field_length[]\"
SIZE=\"5\"></TD>
</TR>";
}
//finish up the form
$form_block .= "
<TR>
<TD ALIGN=CENTER COLSPAN=3><INPUT TYPE=\"submit\" VALUE=\"Create
Table\"></TD>
</TR>
</TABLE>
</FORM>";
?>
<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 2</TITLE>
</HEAD>
<BODY>
<H1>Define fields for <? echo "$_POST[table_name]"; ?></H1>
<? echo "$form_block"; ?>
</BODY>
</HTML>
<?
//check for required fields
if ((!$POST[username]) || (!$POST[password])) {
header( "Location: http://127.0.0.1/show_login.html");
exit;
}
//setup names of database and table to use
$db_name = "testDB";
$table_name = "auth_users";
//connect to server and select database
$connection = pg_connect("host=localhost user=spike password=9sj7En4")
or die(mysql_error());
$db = @pg_select_db($db_name, $connection) ©Ú
die(mysql_error());
//build and issue the query
$sql = "SELECT * FROM $table_name WHERE username =
'$POST[username]' AND password = password('$POST[password]')";
$result = @pg_query($sql,$connection) or die(mysql_error());
//get the number of rows in the result set
$num = pg_num_rows($result);
//print a message or redirect elsewhere, based on result
if ($num != 0) {
$msg = "<P>Congratulations, you're authorized!</p>";
} else {
header("Location: http://127.0.0.1/show_login.html");
exit;
}
?>
<HTML>
<HEAD>
<TITLE>Secret Area</TITLE>
</HEAD>
<BODY>
<? echo "$msg"; ?>
</BODY>
</HTML>
anyone ? 🙂