dcsimg
www.webdeveloper.com
Results 1 to 8 of 8

Thread: Which One To Use ? mysqli_stmt_fetch Or mysqli_fetch_array ?

  1. #1
    Join Date
    Oct 2016
    Posts
    152

    Which One To Use ? mysqli_stmt_fetch Or mysqli_fetch_array ?

    Php Folks,

    You will notice that, I have a question on my comments where I show confusion on how to proceed forward. I ask which 1 of the following 3 I should use which will suit the context of my code well.
    I commented-out the ones that I personally thought did not fit into my codes' context. But, I need your professional opinion.

    //Which of the following to use and why that one over the other 2 ?
    $row = mysqli_stmt_fetch($stmt); //Which line to use ? This line or 2 of the next 2 ?
    //$row = mysqli_fetch_array($query, MYSQLI_ASSOC); //Which line to use ? This line or the one above this lone or the one below this line ?
    //$row = mysqli_fetch_array($result, MYSQLI_ASSOC);



    It is a Log In Page script where the users are given a choice to either login to their accounts by typing their "username" or "email".

    Code:
    <?php
     
    /*
    ERROR HANDLING
    */
    declare(strict_types=1);
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
     
    include 'config.php';
     
    // check if user is already logged in
    if (is_logged() === true) 
    {
    	//Redirect user to homepage page after 5 seconds.
    	header("refresh:2;url=home.php");
    	exit; //
    }
    
    
    
    	if (isset($_POST["login_username_or_email"]) && isset($_POST["login_password"]))
    	{
    		$username_or_email = trim($_POST["login_username_or_email"]); //
    		$password = $_POST["login_password"];		
             
    		//Select Username or Email to check against Mysql DB if they are already registered or not.
    		$stmt = mysqli_stmt_init($conn);
    		
            if(strpos("$username_or_email", "@"))
    		{
    			$email = $username_or_email;
    			$username = "";
    			
    			$query = "SELECT ids, usernames, passwords, emails, accounts_activations_statuses FROM users WHERE emails = ?";
    			$stmt = mysqli_prepare($conn, $query);			
    			mysqli_stmt_bind_param($stmt, 's', $email);
    			mysqli_stmt_execute($stmt);
    		    //$result = mysqli_stmt_get_result($stmt); //Which line to use ? This line or the next ?
    			$result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status); // Which line to use ? This line or the one above ?
    		}
    		else
    		{
    			$username = $username_or_email;
    			$email = "";
    			$query = "SELECT ids, usernames, passwords, emails, accounts_activations_statuses FROM users WHERE usernames = ?";
    			$stmt = mysqli_prepare($conn, $query);
    			mysqli_stmt_bind_param($stmt, 's', $username);
    			mysqli_stmt_execute($stmt);
    			$result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status);
    		}       	
    		
    		//Which of the following 3 to use and why that one over the other 2 ?
    		$row = mysqli_stmt_fetch($stmt); //Which line to use ? This line or 2 of the next 2 ?
    		//$row = mysqli_fetch_array($query, MYSQLI_ASSOC); //Which line to use ? This line or the one above this lone or the one below this line ?
    		//$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    		
    		mysqli_stmt_close($stmt);
    		
    		printf("%s (%s)\n",$row["usernames"],$row["passwords"]);
    		
    		if ($result == false)
    		{
    			echo "No result!";// For debugging purpose!
    			exit();
    		}
    		elseif ($row['accounts_activations_statuses'] == '0')
    		{
    			{
    				echo "You have not activated your account yet! Check your email for instructions on how to activate it. 
    				Check your spam folder if you don't find an email from us.";
    				exit();
    			}
    		}
    		else
    		{
    		
    			if (password_verify($password, $db_password))		
    			{
    				echo "IF triggered for password_verify! password_verify ok"; // For debugging purpose!
    			
    			$_SESSION["user"] = $username;
    			header("location:home.php?user=$username");				
    		}
    		else
    		{
    			echo "Incorrect User Credentials !';<br>";
    			exit();
    		}
    	}
    
    	
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
    <title><?php $site_name?> Member Login Page</title>
      <meta charset="utf-8">
    </head>
    <body>
    <form method="post" action="">
    	<h3><?= $site_name ?> Member Login Form</h3>
    	<fieldset>
    		<label for="login_name">Username/Email:</label>
    		<input type="text" name="login_username_or_email" id="login_name" value="">
    		<br>
    		<label for="login_pass">Password:</label>
    		<input type="password" name="login_password" id="login_pass" value="">
    	</fieldset>
    	<div class="submitsAndHiddens">
    		<label for="login_remember">Remember Login Details:</label>
    		<input type="checkbox" name="login_remember" />
    		<br>
    		<button type="submit">Login</button>
    		<br>
    		<a href="login_password_reset.php">Forgot your Password ? Reset it here!</a>
    		<br>
    		<a href="register.php">Register here!</a>
    	</div>
    </form>
    
    </body>
    </html>

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,395
    You would use the one that is appropriate for your needs and to give you the best possible secure way of handling data.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Oct 2016
    Posts
    152

    Thumbs up

    Quote Originally Posted by \\.\ View Post
    You would use the one that is appropriate for your needs and to give you the best possible secure way of handling data.
    Thanks. But, do you mind reviewing my code and making changes wherever you deem necessary.
    Newbies would learn from your comments.

    Thanks!

  4. #4
    Join Date
    Oct 2016
    Posts
    152
    Folks,

    Do you mind reviewing my code in my original post and edit wherever you deem editing is necessary ?
    I prefer 2 versions where on one you use the mysqli_stmt_fetch and on the other mysqli_fetch_array.
    That way, we newbies learn both from you with your examples.
    Last edited by uniqueideaman; 09-30-2017 at 09:58 PM.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,208
    As per http://php.net/manual/en/mysqli-stmt.fetch.php , you use mysqli_stmt_fetch() only if you have set up your statement to use bound (output) variables. So the answer depends on whether you want to do it that way, or prefer to grab values from an array of column data returned by mysqli_fetch_array(). Neither is right/wrong better/worse, just a matter of style.

    Personally, I wouldn't use either, I'd use PDO in OOP style.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  6. #6
    Join Date
    Oct 2016
    Posts
    152

    Thumbs up

    Quote Originally Posted by NogDog View Post
    As per http://php.net/manual/en/mysqli-stmt.fetch.php , you use mysqli_stmt_fetch() only if you have set up your statement to use bound (output) variables. So the answer depends on whether you want to do it that way, or prefer to grab values from an array of column data returned by mysqli_fetch_array(). Neither is right/wrong better/worse, just a matter of style.

    Personally, I wouldn't use either, I'd use PDO in OOP style.
    I'm getting onto pdo in a month's time. But now, I don't want to quit mysqli procedural half-way or I wasted 7 months for nothing. Let's see one thing to the end. Else, I'll catch the "quitting half-way fever".
    I'd like to see both examples and I'm sure other newbies too! After that, it would be easy for me to decide which to use.
    Infact, a mini tutorial would be good.

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,836
    If you need examples of these functions why don't you simply look at the manual? Every function has great examples of how it is to be used. You don't need the people on this forum to do your research for you.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  8. #8
    Join Date
    Oct 2016
    Posts
    152
    Quote Originally Posted by ginerjm View Post
    If you need examples of these functions why don't you simply look at the manual? Every function has great examples of how it is to be used. You don't need the people on this forum to do your research for you.
    Sometimes, the manual is too complicated for a beginner.
    The folks at forums understand this and show us codes based on our level of understandings.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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