Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2005
    Location
    Dallas, Texas
    Posts
    3

    Unanswered: Get Data from Mysql

    I am new to using DB and need some help on how to get my data out of MYSQL.

    I have a table named saddlebo_sbsongs with columns labled
    id
    artist
    type

    I am trying to get my data out using the following script to a web page..


    #!/usr/bin/perl -w

    use CGI qw(:standard);
    use strict;
    use DBI;
    print header;
    print "This is a test <BR>";


    my $dbh = DBI->connect("DBI:mysql:saddlebo_sbsongs:localhost","s addlebo_WEBUSER","lfsb");

    my $STH = $dbh->prepare("SELECT Artist FROM `sbsongs_data`");

    #$STH->execute;
    $STH->execute # Execute the query
    or die "Couldn't execute statement: " . $STH->errstr;

    if ($STH->rows == 0) {
    print "No names matched `$Artist'.\n\n";
    }

    $STH->finish;
    print "\n";
    print "Enter name ";
    $dbh->disconnect;


    Pretty much at a lost at this point.... some direction would be very helpful

    Thanks to those that will help me out...

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    for starters, you say your table is called saddlebo_sbsongs, but your query is trying to pull data from a table called sbsongs_data
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Sep 2005
    Location
    Dallas, Texas
    Posts
    3
    my mistake... the table is sbsongs_data


    I am new to using DB and need some help on how to get my data out of MYSQL.

    I have a table named saddlebo_sbsongs with columns labled
    id
    artist
    type

    I am trying to get my data out using the following script to a web page..


    #!/usr/bin/perl -w

    use CGI qw(:standard);
    use strict;
    use DBI;
    print header;
    print "This is a test <BR>";


    my $dbh = DBI->connect("DBI:mysql:saddlebo_sbsongs:localhost","s addlebo_WEBUSER","lfsb");

    my $STH = $dbh->prepare("SELECT Artist FROM `sbsongs_data`");

    #$STH->execute;
    $STH->execute # Execute the query
    or die "Couldn't execute statement: " . $STH->errstr;

    if ($STH->rows == 0) {
    print "No names matched `$Artist'.\n\n";
    }

    $STH->finish;
    print "\n";
    print "Enter name ";
    $dbh->disconnect;


    Pretty much at a lost at this point.... some direction would be very helpful

    Thanks to those that will help me out...

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    well, then, the query looks okay, i guess you have a perl problem, so i'll move your thread to that forum
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    May 2005
    Posts
    20
    Perhaps this will help?
    Code:
    #!/usr/bin/perl
    
    use CGI qw(:standard);
    use CGI::Pretty qw();
    use strict;
    use warnings;
    
    use DBI qw(:sql_types);
    
       # DSN, Username and Password
       our($s_DSN)="dbi:mysql:nevali_JFF:localhost";
       #our($s_Username)=<Your username here>;
       #our($s_Password)=<Your password here>;
    
       my($o_dbh,$o_sth,$s_SQLStatement,@arh);       # dbh, sth, SQL Statement, array of references to hash
       my($o_dbh)=DBI->connect($s_DSN,$s_Username,$s_Password) or die "Unable to connect to the Database!\n";
       $o_sth=$o_dbh->prepare($s_SQLStatement="select P.FID from P, S, T where P.UID = S.UID and S.UID = T.UID order by P.FID");
       $o_sth->execute() or die $o_dbh->errstr();
       # reference to array of returned fieldnames
       my($ra_FieldName)=$o_sth->{NAME_uc};
       # reference to array of returned fieldtypes
       my($ra_FieldType)=$o_sth->{TYPE};
       while (my(@a_Value)=$o_sth->fetchrow_array) {
          # turn the array of returned fields into a hash and push it into the array of references to hash
          my(%h);
          @h{(@$ra_FieldName)}=@a_Value;
          push(@arh,{%h});
           };
       $o_sth->finish();
       $o_dbh->disconnect();
    
       unless (@arh) { # No rows returned
          print CGI::header()
                .CGI::start_html('Possibles:');
          print CGI::h1("No rows returned!");
          print CGI::end_html()
           } else { # Rows returned
          print CGI::header()
                .CGI::start_html('Possibles:');
          # Play with the array of references to hash
          my($i);
          for ($i=0; $i <= $#arh; $i++) {
             print CGI::h1($arh[$i]->{FID});
              };
          print CGI::end_html()
           };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •