Php function fails more than once

I made a function to give me a username from my user id. The user ID will be in the 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. Error: "Warning: mysql_select_db (): The supplied argument is not a valid MySQL-Link resource"

code

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

      

+3


source to share


1 answer


Each time you run this function, you only try to require this file once. As you say, do not demand it if it was already necessary before, he saw that it was needed earlier and thus ignored it. So these variables don't exist the second time you call it, and the function inevitably does nothing. I am very surprised that it doesn't throw other errors as the complete file is missing. It shouldn't be extremely important.



+5


source







All Articles