bschultz Posted September 8, 2019 Share Posted September 8, 2019 (edited) I have a php file with a bunch of variables declared. I now need a function to put data on a separate page that pulls variables from the required page. So, on the required page, I have a variable set like this: $qb1 = "John Doe"; The function on the new script is called like this: getplayer($qb1); I've tried this without the dollar sign in the function like this: getplayer(qb1); and then adding the $dollar sign in the rest of the function code...but it never populates the sql query correctly. I'm guessing this is a case for variable variables or something similar, but I don't know how to get the variable value from the required page. Any ideas? Thanks. Here's the code: <?php function getplayer($position) { // start function //$new_position = "$" . $position; $new_position = $position; $file = "their.php"; require "$file"; //echo $qb1; this displays the name John Doe...as $qb1 is declared in the required file $db_select_their = "bschultz_their"; $servername = "localhost"; $username = "user"; $password = "pass"; $dbname = "db_name"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM (select number as position_number, name as position_name, pronunciation as position_pronunciation, pos as position_pos, height as position_height, weight as position_weight, year as position_year, city as position_city, state as position_state, notes as position_notes FROM $db_select_their WHERE name = '$new_position') as position"; //$sql = "SELECT * FROM bschultz_their WHERE name = $new_position"; echo $sql; //exit; /* this displays this text... SELECT * FROM (select number as position_number, name as position_name, pronunciation as position_pronunciation, pos as position_pos, height as position_height, weight as position_weight, year as position_year, city as position_city, state as position_state, notes as position_notes FROM bschultz_their WHERE name = '') as position The name is empty */ $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "#: " . $position_number . " - Name: " . $position_name . ""; } } else { echo "0 results"; } } // end function getplayer("${$qb1}"); $conn->close(); ?> Edited September 8, 2019 by bschultz Quote Link to comment https://forums.phpfreaks.com/topic/309202-function-with-required-file-and-variables/ Share on other sites More sharing options...
Barand Posted September 8, 2019 Share Posted September 8, 2019 You are overthinking it using variable variables. if you have ... $qb1 = 'John' then ${$qb1) is looking for tha contents of $John (which doesn't exist and hnce the blank name). You need to turn your error reporting on. Use getplayer($qb1); Quote Link to comment https://forums.phpfreaks.com/topic/309202-function-with-required-file-and-variables/#findComment-1569509 Share on other sites More sharing options...
bschultz Posted September 8, 2019 Author Share Posted September 8, 2019 I've tried that...the echo of the sql shows the name is blank Quote Link to comment https://forums.phpfreaks.com/topic/309202-function-with-required-file-and-variables/#findComment-1569510 Share on other sites More sharing options...
bschultz Posted September 8, 2019 Author Share Posted September 8, 2019 (edited) I got it working...left the dollar sign out of the function call getplayer('qb1'); and then an if / elseif statement inside the function: if ($position == 'qb1') { $new_position = $qb1; } elseif ($position == 'qb2') { $new_position = $qb2; } Thanks.... Edited September 8, 2019 by bschultz Quote Link to comment https://forums.phpfreaks.com/topic/309202-function-with-required-file-and-variables/#findComment-1569511 Share on other sites More sharing options...
cyberRobot Posted September 11, 2019 Share Posted September 11, 2019 Alternatively, you could use an associative array for storing the player names. So your their.php file might look something like this: <?php $players = array( 'qb1' => 'John Doe', 'qb2' => 'Sam Smith', ); ?> Then your function and function call could be <?php function getplayer($position) { //GET PLAYER INFORMATION $file = "their.php"; require $file; //IF PLAYER FOUND, GET NAME $player = false; //set default value in case player isn't found if(isset($players[$position])) { $player = $players[$position]; } //RETURN RESULT return $player; } $player = getplayer('qb1'); var_dump($player); ?> Quote Link to comment https://forums.phpfreaks.com/topic/309202-function-with-required-file-and-variables/#findComment-1569581 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.