[nycphp-talk] Mysql question!
Ben Sgro (sk)
skyline at publicmine.com
Mon Oct 30 18:57:14 EST 2006
Hello Paul
if exists(!Customer_Info) //Here it doesn't work, error!!!!
Is 'Customer_Info' a constant?
Otherwise I dont see how this works, its not a function (), its not a variable $..I'm not big on OOPHP some if that is some sorta construct
ignore my message.
- Ben
ps: These are some wrappers I use to make my code simpler to look at.
/* DBAS_MySQLConnect
$sqlInfo is an array that holds values particular to the app this is used in.
ex: $sqlInfo = array('databaseUser' => mysql, 'databasePass' => 's3cr3t');
*/
function DBAS_MySQLConnect(&$db, $sqlInfo)
{
$cIp = $_SERVER["REMOTE_ADDR"];
$dbName = $sqlInfo['databaseUser'];
$dbPass = $sqlInfo['databasePass'];
$db = mysql_pconnect("", $dbName, $dbPass);
if ( $db )
{
error_log("DBAS: $cIp connected to mysql server");
}
else
{
error_log("DBAS: error connecting to mysql server");
}
}
/* DBAS_MySQLUseDB
*/
function DBAS_MySQLUseDB($dbName, $db)
{
$res = mysql_select_db($dbName, $db);
if ( $res )
{
error_log("DBAS: using database $dbName");
}
else
{
error_log("DBAS: cannot use database $dbName");
}
}
/* DBAS_MySQLQuery
*/
function DBAS_MySQLQuery($query, $db)
{
$result = mysql_query($query, $db);
if ( !$result )
{
$errorStr = mysql_error( $db );
$errorNum = mysql_errno( $db );
$errorStr = "error($errorStr):error no($errorNum)";
error_log("DBAS: MySQL $errorStr");
}
return $result;
}
----- Original Message -----
From: tuon1 at netzero.net
To: talk at lists.nyphp.org
Sent: Monday, October 30, 2006 6:21 PM
Subject: [nycphp-talk] Mysql question!
Hi, everybody!
I want to ask you all to help me out in solving my database question.
Here is what I want to accomplish:
I want to add my users, one by one, to my Mysql database via a registration form.
When a user fills out a registration form, my script should gather all the information
and add them to the database. I pretty much got all that part working, but I want my
script to check to see whether a table exists or not. If it doesn't exist, my script
should create a table and then proceed to add the information to the database;
and if it does exist, then it should not create one and proceed to use the existed table.
1) Is there a php function that take care of this job that I can use?
I tried to use: if exists(mytablename)
but, it doesn't work.
Here is my code: (some codes are left out for illustration purpose.)
<?php
include("dbinfo.inc.php");
class TableInfo
{
function _construct() //A Constructor
{
/* Initialize database */
mysql_connect($localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
if exists(!Customer_Info) //Here it doesn't work, error!!!!
{
$query = "CREATE TABLE Customer_Info
(
FirstNameCol VARCHAR(15) NOT NULL,
LastNameCol VARCHAR(20) NOT NULL,
AddressCol VARCHAR(40) NOT NULL,
CityCol VARCHAR(20) NOT NULL,
StateCol VARCHAR(20) NOT NULL,
ZipCodeCol INT(5) NOT NULL,
AreaCodeCol INT(3) NOT NULL,
PhoneCol INT(15) NOT NULL,
EmailCol VARCHAR(30) NOT NULL,
LoginNameCol VARCHAR(20) NOT NULL,
PasswordCol VARCHAR(20) NOT NULL
)";
mysql_query($query);
}
//Add new customer to database
function AddNewCustomer($FirstName, $LastName, $Address,
$City, $State, $ZipCode,
$AreaCode, $Phone, $Email,
$WebsiteURL, $LoginName, $Password
)
{
$query = 'INSERT INTO Customer_Info (FirstNameCol,
LastNameCol, AddressCol, CityCol, StateCol,
ZipCodeCol, AreaCodeCol, PhoneCol,
EmailCol, WebsiteURLCol,
LoginNameCol, PasswordCol
)
VALUES ("'. $FirstName . '", "' . $LastName . '",
"' . $Address . '", "' . $City . '",
"' . $State . '", "' . $ZipCode . '",
"' . $AreaCode . '", "' . $Phone . '",
"' . $Email . '",
"' . $WebsiteURL . '", "' . $LoginName . '",
"' . SHA1($Password) . '")';
}
Feel free to correct my code and give suggestions for better techniques.
Thanks!
Paul
------------------------------------------------------------------------------
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk
NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com
Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20061030/053976ed/attachment.html>
More information about the talk
mailing list