PHP:功能无查询&返回

时间:2014-08-28 15:56:29

标签: php mysql

我希望PHP上的人可以帮助我解决问题,我真的不明白。

我有2个脚本。 1:test.php 2:functions.php。

我创建了一个小测试,我调用了functions.php frim test.php中的函数,它运行正常。我得到了回报,这是预期的。我还有第三个脚本register.php,我对数据库有一个查询,工作正常。

所以我希望查询能够作为函数.php

编写的函数

问题:它似乎无法进行数据库查询!但是有创建连接

如果我将完全相同的查询移动到test.php,它就可以了!这有什么特别的原因吗?我退出了php,但对Java,C,JavaScript,Python有所了解。

我已检查过我的include / require是否全部有序。

1:test.php:

 <?php
 require 'includes/functions.php';
 $name = 'testuser';
 $_ok = 0;
 $_ok = check_username ($name);
 printf ( 'Navn i database?: ' . $_ok . '<br>' );
 ?>

2:functions.php:

<?php
require 'includes/db_connect.php';
// Check connection
if (! $mysqli) {
die ( 'mysqli_init_failed' );
}
if (mysqli_connect_errno ()) {
die ( 'Failed to connect to the Database, Sorry..! errorcode: ' .
mysqli_connect_errno() . ' <br>' . 'Error Type: ' . mysqli_connect_error ()     );
}
if ($debug_mode_c) {
print ('Connection Established: ' . mysqli_get_host_info ( $mysqli ) . '<br>') ;
print ('session_id: ' . session_id ()) . '<br>';
}

//将HTML-Header设置为utf-8。 标题(&#39;内容类型:text / html; charset = UTF-8&#39;);

 // Functions
 function check_username($_ok) {
 $name = 'testuser';
 $prep_stmt = "SELECT username FROM customs WHERE username=? LIMIT 1 ";
 $stmt = mysqli_prepare($mysqli, $prep_stmt);
 mysqli_stmt_bind_param($stmt, 's', $name);
 mysqli_stmt_execute($stmt);
 mysqli_stmt_bind_result($stmt, $name_db);
 mysqli_stmt_fetch($stmt);
  if ($name == $name_db) {
  echo "hello";
  mysqli_close ($stmt);
  $_ok = 0;
  } else {
 $name = '';
  $_ok = 2;
 }
 mysqli_free_result($stmt);
 mysqli_close($stmt);
 return $_ok;
 }

2 个答案:

答案 0 :(得分:0)

也许找不到查询的原因。 显然,包含建立连接的include脚本没有加载,因为函数在functions.php中被调用。 当查询代码在test.php中时,包括functions.php,读取所有代码,也读取数据库的连接。 但即使包括&#39; db_connect&#39;内部功能,它不会工作!?! : - (

答案 1 :(得分:0)

PHP中没有像noquery()函数那样的东西。请在数据库连接的包含文件中查看它。您将在数据库连接的包含文件中找到用户定义的函数。