473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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".

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /*
  4. ERROR HANDLING
  5. */
  6. declare(strict_types=1);
  7. ini_set('display_errors', '1');
  8. ini_set('display_startup_errors', '1');
  9. error_reporting(E_ALL);
  10. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  11.  
  12. include 'config.php';
  13.  
  14. // check if user is already logged in
  15. if (is_logged() === true) 
  16. {
  17.     //Redirect user to homepage page after 5 seconds.
  18.     header("refresh:2;url=home.php");
  19.     exit; //
  20. }
  21.  
  22.  
  23.  
  24.     if (isset($_POST["login_username_or_email"]) && isset($_POST["login_password"]))
  25.     {
  26.         $username_or_email = trim($_POST["login_username_or_email"]); //
  27.         $password = $_POST["login_password"];        
  28.  
  29.         //Select Username or Email to check against Mysql DB if they are already registered or not.
  30.         $stmt = mysqli_stmt_init($conn);
  31.  
  32.         if(strpos("$username_or_email", "@"))
  33.         {
  34.             $email = $username_or_email;
  35.             $username = "";
  36.  
  37.             $query = "SELECT ids, usernames, passwords, emails, accounts_activations_statuses FROM users WHERE emails = ?";
  38.             $stmt = mysqli_prepare($conn, $query);            
  39.             mysqli_stmt_bind_param($stmt, 's', $email);
  40.             mysqli_stmt_execute($stmt);
  41.             //$result = mysqli_stmt_get_result($stmt); //Which line to use ? This line or the next ?
  42.             $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 ?
  43.         }
  44.         else
  45.         {
  46.             $username = $username_or_email;
  47.             $email = "";
  48.             $query = "SELECT ids, usernames, passwords, emails, accounts_activations_statuses FROM users WHERE usernames = ?";
  49.             $stmt = mysqli_prepare($conn, $query);
  50.             mysqli_stmt_bind_param($stmt, 's', $username);
  51.             mysqli_stmt_execute($stmt);
  52.             $result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status);
  53.         }           
  54.  
  55.         //Which of the following 3 to use and why that one over the other 2 ?
  56.         $row = mysqli_stmt_fetch($stmt); //Which line to use ? This line or 2 of the next 2 ?
  57.         //$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 ?
  58.         //$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  59.  
  60.         mysqli_stmt_close($stmt);
  61.  
  62.         printf("%s (%s)\n",$row["usernames"],$row["passwords"]);
  63.  
  64.         if ($result == false)
  65.         {
  66.             echo "No result!";// For debugging purpose!
  67.             exit();
  68.         }
  69.         elseif ($row['accounts_activations_statuses'] == '0')
  70.         {
  71.             {
  72.                 echo "You have not activated your account yet! Check your email for instructions on how to activate it. 
  73.                 Check your spam folder if you don't find an email from us.";
  74.                 exit();
  75.             }
  76.         }
  77.         else
  78.         {
  79.  
  80.             if (password_verify($password, $db_password))        
  81.             {
  82.                 echo "IF triggered for password_verify! password_verify ok"; // For debugging purpose!
  83.  
  84.             $_SESSION["user"] = $username;
  85.             header("location:home.php?user=$username");                
  86.         }
  87.         else
  88.         {
  89.             echo "Incorrect User Credentials !';<br>";
  90.             exit();
  91.         }
  92.     }
  93.  
  94.  
  95. ?>
  96.  
  97. <!DOCTYPE html>
  98. <html>
  99. <head>
  100. <title><?php $site_name?> Member Login Page</title>
  101.   <meta charset="utf-8">
  102. </head>
  103. <body>
  104. <form method="post" action="">
  105.     <h3><?= $site_name ?> Member Login Form</h3>
  106.     <fieldset>
  107.         <label for="login_name">Username/Email:</label>
  108.         <input type="text" name="login_username_or_email" id="login_name" value="">
  109.         <br>
  110.         <label for="login_pass">Password:</label>
  111.         <input type="password" name="login_password" id="login_pass" value="">
  112.     </fieldset>
  113.     <div class="submitsAndHiddens">
  114.         <label for="login_remember">Remember Login Details:</label>
  115.         <input type="checkbox" name="login_remember" />
  116.         <br>
  117.         <button type="submit">Login</button>
  118.         <br>
  119.         <a href="login_password_reset.php">Forgot your Password ? Reset it here!</a>
  120.         <br>
  121.         <a href="register.php">Register here!</a>
  122.     </div>
  123. </form>
  124.  
  125. </body>
  126. </html>
  127.  
  128.  
  129.  
Sep 29 '17 #1
1 1672
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.
Oct 1 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
2
by: Raymond H. | last post by:
Hello, I create a vb4 project which can also naviger on Internet via the WebBrowser control which I put on my form. My question is: if this program is installed on a station having already...
15
by: Herman | last post by:
Hi everyone, I'm currently studying for my Master's in Computer Science, and I will be working on my thesis this summer. I've been thinking about constructing a web services application for my...
9
by: CY FOK | last post by:
Hi I am planning to open a software company to develop client-server apps and web applications for my client. Now, i am in a difficult situation to determine what is the best platform i should use...
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
3
by: jason | last post by:
If you are sending out bulk emails in the range of 800 to 1000 per day which component is better: 1. ASPEmail 2. CDO ....and further, do they also BOTH use a SEND TO QUEUE option. I wish...
17
by: lawrence | last post by:
How is it possible that the question "How do I detect which browser the user has" is missing from this FAQ: http://www.faqts.com/knowledge_base/index.phtml/fid/125 and is only here on this...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
2
by: rn5a | last post by:
A Form has a select list which is populated from a MS-Access database table. The DB table from where the select list is populated has 2 columns - CountryID & CountryName. When the Form is posted,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.