0

For the life of me, I cannot figure out why this is happening. I tried typecasting I've tried using built-in parsing functions (intval), and I have tried using string replace methods, but no matter what I do, my $AccountNum returns "1" and after "parsing" it turns into a 0.

I don't have much experience in custom programming on WordPress and am hoping someone here might know where the problem lies. Below is the WordPress page I am trying to implement, and below that is the portion of functions.php that I added on to, which is being implemented, and the last bit is my output.

Please help!

[vc_row][vc_column][vc_column_text][insert_php]

if ($_POST['submit']) {

saveAccountNum($_POST['email'], $_POST['password']);

echo $AccountNum;

get_all_user_info((int)$AccountNum);


}

[/insert_php][/vc_column_text][/vc_column][/vc_row]

part 2 (functions.php script)

function get_all_user_info($AccountNumber){

    global $AccountNum;

    $dbhost = "localhost";
    $dbuser = "#######";
    $dbpass = "#######";
    $db = "#######";
    $conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);


    echo $AccountNumber;
    $userData = "CALL get_all_user_info("+$AccountNumber+")";
    $sql = mysqli_query($conn, $userData);

    global $First_Name;

        if(!$sql){
          die("Didn't work, here's why: " . mysql_error());
        }
        else{
         $object = $sql->fetch_object();
         $First_Name = $object->First_Name;
         echo "All variables made: " .$First_Name;
        }
$conn->close();
 }

part 3 (output)

Changed variable name: ‘1”1’0All variables made:

As you can see, It doesn't give me any errors, but the error arises when I try to parse from a string to an int. This happens whether I try typecasting, or calling functions, and to be honest, I am out of ideas as to why. Any help would be much appreciated.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
  • Not a PHP expert here, but why do you declare a global variable in a function - even more, after a variable with the same name is used in the parent scope? – Alex Filipovici Apr 27 '18 at 06:32
  • I'm referencing it from the header.php file in wordpress. I can try to make it all within one page, but it would be all the same. This is for a school project to demonstrate some database design, I just found that this looks cleaner. – Lucas Trestka Apr 27 '18 at 06:34

1 Answers1

-1

Try this references links

http://php.net/manual/en/function.intval.php

https://stackoverflow.com/questions/7008214/php-string-to-int

https://stackoverflow.com/questions/8529656/how-do-i-convert-a-string-to-a-number-in-php


 $myVar = "13";
 var_dump($myVar); // string '13' (length=2)
 $myVar= intval($myVar);
 var_dump($myVar); // int 13

  $myVar = "13";
  var_dump($myVar); // string '13' (length=2)
  $myVar= (int)$myVar;
  var_dump($myVar); // int 13
Vaibhav Kumar
  • 768
  • 4
  • 12