in reply to Re: DBD DBI Mysql Connect
in thread DBD DBI Mysql Connect

Hey there, thank you, here is a piece of code but I am sure this problem is in config somewhere. I have a few websites with working mysql and cgi running on my old Mac that I need to replace. This is only a snippet to test database connection while I am seeing up a new server on Sierra.
use DBI; use Cwd; use CGI qw(:all); use LWP::Simple qw(get); use Net::SMTP; use Time::HiRes qw(gettimeofday); use File::Copy; $Cgi_Dir = qq(/Library/WebServer/CGI-Executables/bonriche); # MySql Database information $Db = qq(Platinum); $DbPort = qq(3306); $DbUserid = qq(root); $DbTable = qq(Classifieds); $DbUsers = qq(Users); $DbReceipts = qq(Receipts); $DbHost = qq(127.0.0.1); $DbPasswd = qq(******); $DbConnectionInfo = qq(DBI:mysql:database=$Db:$DbHost:$DbPort); # -------------------------------------------------------------------- +----------------------------------------- # # Query the database for the highest 'RegNum' and generate a new 'RegN +um' $Dbh = DBI->connect($DbConnectionInfo,$DbUserid,$DbPasswd); $Sth = $Dbh->prepare("SELECT RegNum FROM $DbUsers ORDER BY RegNum DESC + LIMIT 1"); $Sth->execute() or $Err_Num = "3002"; $Err_Mess = "$DBI::errstr": if ($Err_Num eq "0") { $ChkRegNum = ""; while (($ChkRegNum) = $Sth->fetchrow_array) { $LastRegNum = "$ChkRegNum"; } $Sth->finish(); $Dbh->disconnect; }

Replies are listed 'Best First'.
Re^3:DBD DBI Mysql Connect
by huck (Prior) on Sep 10, 2017 at 21:39 UTC

    die "Cannot connect: $DBI::errstr" unless ($Dbh);

Re^3: DBD DBI Mysql Connect
by afoken (Chancellor) on Sep 11, 2017 at 20:33 UTC

    I really miss strict, warnings, taint mode, and RaiseError.

    Interpolating variables into SQL statements usually triggers my "use placeholders" warning (Re: Counting rows Sqlite, Re^2: Massive Memory Leak). But in the only SQL statement you show here, you "just" interpolate a constant table name into the SQL statement. Not as bad as variable data from the network, but still, you should use DBI's quote_identifier() method. That won't even break when you change $DbUsers.

    More:

    • You use string eq to compare numbers. Better use ==. How to remember that.
    • SELECT RegNum FROM Users ORDER BY RegNum DESC LIMIT 1 returns at most one value (LIMIT 1). That value is always the maximum value, due to ORDER BY RegNum DESC. Why don't you just SELECT MAX(RegNum) FROM Users? (MAX() in MySQL)
    • Plus, the code that you posted does not even compile:
      X:\>perl 1199050.pl syntax error at 1199050.pl line 26, near ""$DBI::errstr":" syntax error at 1199050.pl line 34, near "}" Execution of 1199050.pl aborted due to compilation errors. X:\>

      Better copy&paste from the actual source. Manual typing adds extra errors.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)