3

我制作了一个函数,可以根据用户 ID 为我提供用户名。不同的用户 id 将处于一个 while 循环中,因此该函数将被多次使用。它适用于一个用户 ID,但如果有多个用户 ID,则会给我一个错误。错误是“警告:mysql_select_db():提供的参数不是有效的 MySQL-Link 资源”

代码是

<?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>
4

1 回答 1

5

每次运行该函数时,您都试图只要求该文件一次。既然你说如果之前已经需要它就不需要它,它会看到它之前已经被需要,因此忽略它。因此,这些变量在您第二次调用它时不存在,并且该函数不可避免地什么都不做。我非常惊讶它不会产生其他错误,因为整个文件都丢失了。它一定不是非常重要。

于 2012-04-08T23:14:57.400 回答