Я хочу вызвать определенную функцию из моего database.php
файла и получить возвращаемое значение. Здесь я пытаюсь сделать это:
ЯШ:
function submit_verification_code(){
$.ajax({
url: "database.php",
type: "post",
data: ({
'code': code_entered,
}),
dataType:"text",
context: this,
success : function(response) {
console.log('RESPONSE: ' + response);
//OPTIONAL_FUNCTION_TO_DO_SOMETHING_WITH_THE_RESPONSE(response);
},
error: function(jqXHR,textStatus,errorThrown){
//OPTIONAL_FUNCTION_TO_DO_SOMETHING_WITH_THE_ERROR(jqXHR);
console.log(errorThrown);
}
});
}
database.php
if(isset($_POST['code'])){
does_code_match($_POST['code']);
}
function does_code_match($code){
connect();
die('test');
$sql = 'select * from emailstobeverified where email=';
$sql .= "'" . $_SESSION['email'] . "'";
$sql .= ' and verification_code=';
$sql .= $code;
$sql .= ';';
$count = query($sql)->num_rows;
die(strval($count));
disconnect();
echo strval($count);
exit;
//Once you've outputted, make sure nothing else happens
}
does_code_match
функция выполняется, и журнал консоли печатает <br />
при вызове console.log("RESPONSE" + response)
. Но я хочу напечатать значение $count
EDIT:
Проблема с функцией connect()
. Если я позвоню die('test')
непосредственно перед вызовом connect()
, он вернет ответ! В консоли написано «Привет, мир». Если я позвоню прямо ПОСЛЕ вызова connect()
, он напечатает это:
<b>Warning</b>: Use of undefined constant conn - assumed 'conn' (this will throw an Error in a future version of PHP) in <b>C:\xampp2\htdocs\database.php</b> on line <b>72</b><br />
<br />
<b>Warning</b>: Use of undefined constant dbhost - assumed 'dbhost' (this will throw an Error in a future version of PHP) in <b>C:\xampp2\htdocs\database.php</b> on line <b>72</b><br />
<br />
<b>Warning</b>: Use of undefined constant dbuser - assumed 'dbuser' (this will throw an Error in a future version of PHP) in <b>C:\xampp2\htdocs\database.php</b> on line <b>72</b><br />
<br />
<b>Warning</b>: Use of undefined constant dbpass - assumed 'dbpass' (this will throw an Error in a future version of PHP) in <b>C:\xampp2\htdocs\database.php</b> on line <b>72</b><br />
<br />
<b>Warning</b>: Use of undefined constant db - assumed 'db' (this will throw an Error in a future version of PHP) in <b>C:\xampp2\htdocs\database.php</b> on line <b>72</b><br />
test
и ответ <br />
Функция подключения:
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "example";
$conn;
function connect(){
$GLOBALS[conn] = new mysqli($GLOBALS[dbhost], $GLOBALS[dbuser], $GLOBALS[dbpass],$GLOBALS[db]) or die("Connect failed: %s\n". $GLOBALS[conn] -> error);
}