0

I have a html form that takes in an account number and a submit value called retrieve. When this retrieve button is clicked I need certain attributes to print out based on the customer's account number. I tried this in my code. For some reason it enters the function but does not print any of the values of the row out. I am very confused and not sure where to go from here to solving the issue. I do have dummy data in mysqlworkbench. I have used the same db connection for other pages and it worked fine when I posted a form back into the database. So I am not sure what the issue is exactly.

I am not sure if the database lost connection where I call the query in result? It prints out the query of result, but when I try to do a var_dump after the query it is saying NULL. So something might be wrong there?

I added the error check when I create the variable $result within the function and every time I add the account number it print's out Could Not Enter Data but not the mysql error.

My Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

  <title>Create Account</title>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>


<body>

  <div class="header">Our Really Cool Banking App</div>

  <div id="leftcolumn"> 
      <!-- Creating Buttons here -->
      <div id="nav">
        <ul>
          <li><a href="banking.php">Home</a></li>
          <li><a href="checking.php">Checking</a></li>
          <li><a href="savings.php">Savings</a></li>
          <li><a href="createaccount.php">Create Account</a></li>
          <li><a href="createloan.php">Create Loan</a></li>
        </ul>
      </div>
  </div>
  <h2>Checking Account Information Details</h2>

  <div class="inputBox">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
      Account Number: <input type="text" name="accountNum"><br>
      <input type="submit" id="Retrieve" name="Retrieve" value="Retrieve">
    </form>
  </div>

  <?php
  //error_reporting(E_ALL); 
  //ini_set('display_errors', 1);

  $properties = parse_ini_file("properties.ini");
  if ( !( $database = mysql_connect( $properties["dbUrl"], $properties["username"], $properties["password"] ) ) ) 
  {
    echo "<p> Be sure to fill out information in the properties.ini file </p>";
    die( "Could not connect to database </body></html>" ); 
  } 

  echo $_POST['Retrieve'] . "is the value";

  if ( !mysql_select_db( $properties["dbName"], $database ) ) 
    die( "Could not open the database </body></html>" );


  if (isset($_POST['Retrieve']))
  {
   accountInfo();
  }

  function accountInfo() {
    $currentAccountNum = $_POST[accountNum];

    $data = "SELECT * FROM Account WHERE AcctNum = '$currentAccountNum'";

    $result=mysqli_query($database, $data);

    if(! $result )
    {
       die('Could not enter data: ' . mysql_error());
    }


    while($row = mysqli_fetch_array($result)) {
       echo $row['AcctNum'];
       echo "<br>";
       echo $row['MemberId'];
       echo "<br>";
       echo $row['creationDate'];
       echo "<br>";
       echo $row['CreatedByEmployee'];
       echo "<br>";
       echo $row['type'];
       echo "<br>";
    }
  }

  mysql_close( $database ); 
?>

</body>

</html>
user2522055
  • 159
  • 2
  • 13
  • 4
    A: You're mixing APIs - it's not rhum & coke. – Funk Forty Niner May 06 '14 at 00:27
  • why mysql above and then mysqli on the middle? just stick with mysqli, its the improved version – user1978142 May 06 '14 at 00:27
  • @Fred-ii- I switched back, but that is not the issue. It seems that when I try and execute the query there it is not working because I set error checking after it now. – user2522055 May 06 '14 at 00:32
  • @kevinabelita Although I changed that stupid error of mine. It has something to do with the db not being connected I think. – user2522055 May 06 '14 at 00:33
  • Switched back to what? – Funk Forty Niner May 06 '14 at 00:33
  • @Fred-ii- I thought you were talking about my use of mysqli and mysql? I switched mysqli back to mysql to keep it consistent with the rest of my code. – user2522055 May 06 '14 at 00:34
  • Yes that's exactly what I meant about that. You can't mix `mysqli_*` with `mysql_*` functions which is what you're doing now, least in what you posted. It's one or the other. That's the major issue here. – Funk Forty Niner May 06 '14 at 00:39
  • Explain to me this; why do you have `$database = mysql_connect( $properties` and `!mysql_select_db( $properties` and `$result=mysqli_query` and `mysqli_fetch_array` and `mysql_close( $database )`? How do they all tie in together? Am I missing something; is there something I'm not grasping? – Funk Forty Niner May 06 '14 at 00:51

2 Answers2

2

You are using mysqli_fetch_array() when you are looking for mysqli_fetch_assoc().

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • I am not sure what the difference is? – user2522055 May 06 '14 at 00:31
  • http://stackoverflow.com/questions/11480129/mysql-fetch-row-vs-mysql-fetch-assoc-vs-mysql-fetch-array – Jesse Hockenbury May 06 '14 at 00:33
  • That clarified an issue I did not know about, but this did not solve the issue I was having. – user2522055 May 06 '14 at 00:40
  • @user2522055 Another tip: If something is already broken & a proposed solution is changing one simple function from `mysqli_fetch_array` to `mysqli_fetch_assoc`, you should just do that without asking to see if it works. You don’t understand why? Okay, then this mistake will help you learn. – Giacomo1968 May 06 '14 at 00:42
0

You have quotes in your SQL-Statement. try:

 $data = "SELECT * FROM Account WHERE AcctNum = " . $currentAccountNum;
ratmalwer
  • 700
  • 5
  • 14
  • Thanks for your help. The problem seems to be the line after that. I just edited the code on stack overflow and it has error checking, so every time I add the correct account number it jumps into the error check but does not print out the mysqlerror – user2522055 May 06 '14 at 00:40
  • I would echo($data) and have a look at the statement :-) – ratmalwer May 06 '14 at 00:46