Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    35
    Thanks
    4
    Thanked 1 Time in 1 Post

    question about mysqli

    PHP Code:
        function activate_user($confirm_code) {
            
    $query "SELECT *
            FROM users
            WHERE confirm_code = ? AND activated = 0
            LIMIT 1"
    ;
            
            if(
    $stmt $this->conn->prepare($query)){
                
    $stmt->bind_param('s'$confirm_code);
                
    $stmt->execute();
                if(
    $stmt->affected_rows == 1){
                    
    $stmt->close();
                    return 
    true;
                }else {
                    
    $stmt->close();
                    return 
    false;
                }
            }
        } 
    I am using different code, because i cant figure out how to use this one...

    Sooom, after the query, how can i pass info about what user has been selected and all the info about him?

    My database structure for "users" table is smtn like this:

    uid username password mail confirm_code activated

    Basicly what i want is to select user with that specific confirm_code he recives via email and then pass all the info about the user back to method that called this method...

    I know this sounds very noobish, but my skill is still low/mid with php... hehe...

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You need to fetch the data from the resultset after the execute.

    http://us.php.net/manual/en/mysqli-stmt.execute.php

    http://us.php.net/manual/en/mysqli-stmt.fetch.php

  • #3
    New Coder
    Join Date
    Jun 2009
    Posts
    35
    Thanks
    4
    Thanked 1 Time in 1 Post
    Hmm ok, so i fetch:

    PHP Code:
        function activate_user($confirm_code) {
            
    $query "UPDATE users
            SET activated = 1
            WHERE confirm_code = ?
                AND activated = 0
            LIMIT 1"
    ;
            
            if(
    $stmt $this->conn->prepare($query)){
                
    $stmt->bind_param('s'$confirm_code);
                
    $stmt->execute();
                if(
    $stmt->fetch()){
                    
    $stmt->close();
                    return 
    true;
                }
            }
        } 
    I need to catch it in a variable? like $test = $stmt->fetch() ?

    If this query was succesfull, 1 user should be selected, how can i now pass the info about the user back to the script calling this function?

    Maybe i am missing here some really basic thing, dunno... Maybe i am not making my self clear enough? haha...

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Wait, your query just changed to an UPDATE query, which you don't fetch from.

    If your query is a SELECT query then the results of the select go into the variables you specified with the bind_result method. I gave you links to the PHP manual; follow the examples on those pages closely and you'll figure out what you're missing.

  • #5
    New Coder
    Join Date
    Jun 2009
    Posts
    35
    Thanks
    4
    Thanked 1 Time in 1 Post
    What i want to do is basically like this:

    http://www.phpeasystep.com/phptu/24.html

    PHP Code:
    $rows=mysql_fetch_array($result1);
    $name=$rows['name'];
    $email=$rows['email'];
    $password=$rows['password'];
    $country=$rows['country']; 
    But i don't know what is the mysqli version of mysql_fetch_array? this?

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You've been using mysqli to execute a prepared statement so far. You don't have to use prepared statements with mysqli. You can use mysqli as OO or procedural, and make it look almost identical to the mysql extension. If you do that, then yes, you can use mysqli_fetch_array.

    Is there some compelling reason you are executing a prepared statement?

  • #7
    New Coder
    Join Date
    Jun 2009
    Posts
    35
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Fumigator View Post
    Is there some compelling reason you are executing a prepared statement?
    Hmm noo... I am still in the process of learning, so maybe i have just too many thing in my head right now... hehe...

    What i mean to say is that i am just following some tut's i have read, and this is where i came, really hehe...

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Here's how I use mysqli. I recommend this tutorial

    PHP Code:
    //connect
    $mysqli = new mysqli(MYSQL_HOSTNAMEMYSQL_USERNAMEMYSQL_PASSWORDMYSQL_DATABASE);

    //select
    $query "SELECT whatever FROM wherever";
    $result $mysqli->query($query);
    if (!
    $result) {
                
    $errorString "Query Error! Text: $query<br />\n    Error: ".$mysqli->error."\n";
                
    $log fopen("mysqlerrorlog.txt""a+");
                
    fwrite($log$errorString);
                
    fclose($log);
                die(
    $errorString);
    }

    //fetch
    for ($i 0$i $result->num_rows$i++) {
        
    $resultSet[$i] = $result->fetch_assoc();

    I recommend making this a function that you call or an object you create and call methods of.

  • Users who have thanked Fumigator for this post:

    GaVrA (07-14-2009)

  • #9
    New Coder
    Join Date
    Jun 2009
    Posts
    35
    Thanks
    4
    Thanked 1 Time in 1 Post
    So then i can access my users table:

    uid username password mail confirm_code activated

    like this:

    $resultSet[0] is uid
    $resultSet[1] is username
    $resultSet[2] is password

    Right? Or did i get smtn wrong here...

    Also isn't it better for this code to be like this:

    PHP Code:
    //connect
    $mysqli = new mysqli(MYSQL_HOSTNAMEMYSQL_USERNAMEMYSQL_PASSWORDMYSQL_DATABASE);

    //select
    $query "SELECT whatever FROM wherever";
    $result $mysqli->query($query);
    if (!
    $result) {
                
    $errorString "Query Error! Text: $query<br />\n    Error: ".$mysqli->error."\n";
                
    $log fopen("mysqlerrorlog.txt""a+");
                
    fwrite($log$errorString);
                
    fclose($log);
                die(
    $errorString);
    }else{
          
    //fetch
          
    for ($i 0$i $result->num_rows$i++) {
                
    $resultSet[$i] = $result->fetch_assoc();


  • #10
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    $resultSet[0] is uid
    $resultSet[1] is username
    $resultSet[2] is password

    Right? Or did i get smtn wrong here...
    Wrong... fetch_assoc() puts the stuff into an associative array. $resultSet[0]['uid'], $resultSet[0]['username'], $resultSet[0]['password']. If you only want to fetch one result row, you don't need the loop.

    Your change works too, but since I die() if an error is found, it's not necessary to put the fetch on the else side of that compare.


  •  

    Posting Permissions

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