drbe16008 2012-04-08 23:09
浏览 46
已采纳

php函数不会多次工作

I have made a function to give me the name of a user from their user id. The different user id's will be in a while loop so the function will be used multiple times. It works fine for one user id but gives me an error if there is more than one. The error is "Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource"

the code is

<?php 

function user_details($user_id) {

require_once('../Connections/runner.php'); 

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string   ($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_users = $user_id;
$first_name = "";
$last_name = "";
mysql_select_db($database_runner, $runner);
$query_users = sprintf("SELECT first_name, last_name, profile_img_small FROM sign_up WHERE   user_id = %s", GetSQLValueString($colname_users, "int"));
$users = mysql_query($query_users, $runner) or die(mysql_error());
$row_users = mysql_fetch_assoc($users);
$totalRows_users = mysql_num_rows($users);
$first_name = $row_users['first_name'];
$last_name = $row_users['last_name'];
$profile_sml = $row_users['profile_img_small'];

echo "$first_name $last_name";
}



?>

<?php
$user_id = 10;
?>

<a href="*"><?php user_details("$user_id"); ?></a>

<?php
$user_id = 9;
?>

<a href="*"><?php user_details("$user_id"); ?></a>
  • 写回答

1条回答 默认 最新

  • dtc4547 2012-04-08 23:14
    关注

    Every time you run the function, you're attempting to require that file only once. Since you're saying don't require it if it's already been required before, it's seeing it has been required before and thus ignoring it. So those variables don't exist the second time you call it and the function inevitably does nothing. I'm thoroughly surprised it doesn't produce other errors since an entire file is missing. It must not be extremely important.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿