Results 1 to 2 of 2

Thread: PHP MySQL Search Script.

  1. #1
    JonW Guest

    PHP MySQL Search Script.

    I am trying to write a php script that searches for given information in a database and returns the result in a variable. I have created an image that better explains what I am trying to accomplish. I appreciate the help!

    http://mantisclan.com/mysql.jpg

  2. #2
    Join Date
    Feb 2007
    Location
    Ireland
    Posts
    1,007
    Hmm looks like a phpBB3 table... So you want to pass in a username and return the associated user id?

    Anyway, the following should work...

    Assumptions:
    • the table name is 'user'
    • database details are already predefined in the following constants (DB_HOST, DB_USER, DB_PASS, DB_NAME)


    PHP Code:
    function fetchUserID($username) {
        
    // Establish a database connection
        
    $db = new MySQLi(DB_HOSTDB_USERDB_PASSDB_NAME);
        
    // Clean the input so it's not malicious
        
    $username $db->real_escape_string($username);
        
    // Construct the SQL query
        
    $sql "SELECT user_id FROM users WHERE username = '$username'";
        
    // Execute the query in the database
        
    $result $db->query($sql);
        
    // If there was only one result returned, grab the user_id and return it, otherwise return false
        
    if($result->num_rows == 1) {
            
    $dataArray $result->fetch_assoc();

            return 
    $dataArray['user_id'];
        }

        return 
    false;
    }

    $user_id fetchUserID('Admin');
    echo 
    $user_id// Should print out "2" 
    “The best thing about a boolean is even if you are wrong, you are only off by a bit.”

Similar Threads

  1. php search engine script help!
    By goochee in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 31 Jul 2007, 03:39 AM

Posting Permissions

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