Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have the following PHP function that should evaluate a string, to check "if it exists in the database or not" the string comes to the $row variable as an email format. How can I evaluate the string? Please help and thank you!


PHP
<?php

if (isset($_POST['submit'])) {
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $username = $_POST['username'];
    checkUsername($firstName, $lastName, $username);
    }

function checkUsername($firstName, $lastName, $username){
include 'connect_sql.php'; 

        $sql = "SELECT UserName FROM [Membership].[dbo].[Center] WHERE UserName='$username'";
        $stmt = sqlsrv_query( $conn, $sql );
        $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);

        if(sqlsrv_num_rows($row) > 0 ) {              
            echo "<div id='loginmsg'>Member already exist</div>";
        }

        else {
            echo "<div id='loginmsg'>Member is new</div>";
        }
    }
?>


What I have tried:

I tried the following (sqlsrv_num_rows($row) > 0 ) having fetch a row but it jumps to else condition. I tried running the sql query directly at the database and it works fine. I'm not sure what's wrong with the code or how to handle when it finds a row to echo that the record exist or not. Please help!
Posted
Updated 21-Oct-16 18:31pm
v5

1 solution

Quote:
It is because sqlsrv_query() uses SQLSRV_CURSOR_FORWARD cursor type by default. However, in order to get a result from sqlsrv_num_rows(), you should choose one of these cursor types below:

SQLSRV_CURSOR_STATIC
SQLSRV_CURSOR_KEYSET
SQLSRV_CURSOR_CLIENT_BUFFERED


For more information check following thread in stackoverflow. Similar question is already answered here-
php - sqlsrv_num_rows Not Returning Any Value - Stack Overflow[^]

Hope, it helps :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900