Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php function wont work more than once

Tags:

php

mysql

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>
like image 292
Jeff Avatar asked Aug 03 '26 06:08

Jeff


1 Answers

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.

like image 70
animuson Avatar answered Aug 05 '26 20:08

animuson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!