Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the simplest way for me to

Status
Not open for further replies.

M626

Programmer
Mar 13, 2002
299
What is the simplest way for me to connect to a MySQL database,
Search a row for a match of client, if it finds a match to my
Variable($Client), do an UPDATE, else
do an INSERT of that CLIENT to the Table.
 
A couple of things you need to have in place first

1 - A MySQL database

2 - A way to connect to it, DBI and DBD::MySQL I would suggest

What do you have right now?

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
# You can create a database handle using DBI:
use DBI;

my $dbh = DBI->connect("DBI:mysql:database=mydatabasename;host=mydatabaseserver", "databaseusername", "databasepassword");

# then you do inserts like this:
$dbh->do(qq(INSERT INTO tablename VALUES(a,b,c)));

# updates like this:
$dbh->do(qq(UPDATE tablename SET field="value" WHERE otherfield="somevalue"));

# deletes like this:
$dbh->do(qq(DELETE FROM tablename WHERE otherfield="somevalue"));

# and selects something like this:
my $sth = $dbh->prepare(qq(SELECT x,y,z FROM tablename WHERE myfield="somevalue"));
$sth->execute();

while(my @arr = $sth->fetchrow_array()) {
print qq($arr[0] $arr[1] $arr[2]\n);
}

# and definitely read perldoc DBI for tuning these calls

YMMV
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top