DBI/mySQL Select Question - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

DBI/mySQL Select Question

.Blind2Life..Blind2Life. Posts: 2Member
How do you make a Select for UserID from table Users WHERE CharName = $character_name.

I've tried multiple ways but I keep getting this error:
[code]Can't call method "prepare" on an undefined value at #####.cgi line ####.[/code]

Comments

  • .Blind2Life..Blind2Life. Posts: 2Member
    [b][red]This message was edited by the .Blind2Life. at 2002-4-18 15:8:54[/red][/b][hr]
    tell me what is wrong with this
    [code]$statement = "SELECT `UserID` FROM `Users` WHERE 1 AND `CharName` LIKE '$character_name'";
    $sth = $dbh->prepare( $statement ) or die "Can't prepare $statement: $dbh->errstr
    ";
    $sth->execute or die "can't execute the query: $sth->errstr";
    $user_id = $sth->fetchrow_array();[/code]

    Or even taking out the $statement and applying the Select from inside the ( )'s in the DBH prepare line.

    [The select was taking from a successful select in PHPmysqladmin.]

  • JonathanJonathan Posts: 2,914Member
    Hi,

    I presume you are connecting to the database handle successfully? After your connection line, or rather before you do that query, put this:-

    print "DBH: $dbh
    ";

    If it doesn't print some long number in place of $dbh then you are not connecting to the database server successfully, and thus have no chance of being able to execute an SQL statement...of course, it could be something else. :)

    Enjoy your day,

    Jonathan


    ------------------------------------------
    Count downloads from your site for free!
    http://www.downloadcounter.com/

  • gsfgsf Posts: 2Member
    Your syntax looks good. I don't think you're connecting. I coded this. It connects to database mysql. Just change the variables at the top to match your situation. If there's a wrapping problem in this post and the code doesn't work, send me an e-mail and I'll e-mail you the code as a text file.

    ###########Start Code

    #!/usr/bin/perl

    #Change the variables to match your situation. Your database is differant than mine. #Remember that your shebang needs to be on the
    #first line.
    #
    #DBI and MySQL in Perl? Do I do that? Yep! And quite well I might add!
    #Hope this helps. ~George [email protected]

    use DBI;

    #Setup Variables
    #####
    #Name of DSN, Don't change if using MySQL database.
    #####
    $databaseserver="mysql";

    #####
    #Name of computer hosting the database, keep to localhost if you are unsure.
    #####
    $dbhostname="localhost";

    #####
    #Name of database to connect to.
    #####
    $dbase="mysql";

    #####
    #MySQL username to authenticate with.
    #####
    $mysqlusername="yourMySQLusername";

    #####
    #MySQL password to authenticate with.
    #####
    $mysqlpassword="yourMySQLpassword";


    $wordtosearchfor="root";


    ###############Rest of Script

    &dbthreadresponse;


    #####
    #Threadresponse: Custom Code
    #####
    sub dbthreadresponse{

    #Connect
    &dbconnect;


    $sth = $dbh->prepare ("SELECT `User` FROM `User` WHERE 1 AND `User` LIKE '$wordtosearchfor' ");
    $sth->execute ();
    while (@val = $sth->fetchrow_array ())
    {
    @threadresponse="$val[0]";
    print "@threadresponse
    ";
    }

    #Disconnect
    &dbdisconnect;
    }




    #####
    #dbconnect: Connects to the database.
    #####
    sub dbconnect{
    $dbh = DBI->connect ("DBI:mysql:host=$dbhostname;database=$dbase","$mysqlusername","$mysqlpassword",
    {PrintError => 0, RaiseError => 1});
    }


    #####
    #dbdisconnect: Gracefully closes the connection to the database.
    #####
    sub dbdisconnect{
    $sth->finish ();
    $dbh->disconnect ();
    }


    #This is the last line of the script.#

Sign In or Register to comment.