Jump to content

Issues carrying over variable


akirablaid

Recommended Posts

Hey guys, this is my first post and I hope your knowledge can help, I'd like to use this site as a resource.

I'm trying to put together a members' page for logged in users in a script I'm building. I want to call a function, which grabs all the userdata from the sql database. This should enable me to write on the members page using handy variables like $u_name and $u_id.

Here's the function I'm calling:

function ui($user) {
$match = "select * from users where username = '".$user."'";
$result = mysql_query($match) or die ("Could not match data because ".mysql_error()); 
$num_rows = mysql_num_rows($result); 
if ($num_rows <= 0) { 
  echo "Sorry, info for this user not found.";
} else {
   $qry = mysql_fetch_array($result);
$ui_un = $qry[username];
$ui_id = $qry[id];
$ui_dname = $qry[dname];
$ui_fname = $qry[fname];
$ui_lname = $qry[lname];
  }
}

And so I call this with ui($mysite_username); and then attempt to print users information. It simply does not work. Weather I use $ui_un or $qry[username], the variables simply do not carry over from the function (which is in the config file). So, how can I get these variables to carry through?

Regards,

Akira

Link to comment
https://forums.phpfreaks.com/topic/146574-issues-carrying-over-variable/
Share on other sites

Hey, try this. I just neatened it up a bit. If this doesn't work, if you could post an error message/what it's doing wrong would be useful.

 

function ui($user)
{
   $result = mysql_query("SELECT * FROM users where username = '$user'") or die ("Could not match data because ".mysql_error());
   if(mysql_num_rows($sql) == 0)
   {
	echo "Sorry, info for this user not found.";
   }
   else
   {
   $qry = mysql_fetch_array($result);
   $ui_un = $qry['username'];
   $ui_id = $qry['id'];
   $ui_dname = $qry['dname'];
   $ui_fname = $qry['fname'];
   $ui_lname = $qry['lname'];
  }
}

can't remember if it actaully matter of not, but better syntax would be:

Instead of $qry[username];

use $qry['username']; <-- notice the quotes? may help

if that doesn't work then i'd suggest that your database isn't being accessed correctly, check your connectiion information

<?php

function ui($username)
{
global $user;

$query = mysql_query("
	SELECT * FROM `users`
	WHERE `username` = '".mysql_real_escape_string($username)."'
	LIMIT 1
");

if (mysql_num_rows($query) == 1)
{
	$user = mysql_fetch_array($query, MYSQL_ASSOC);
	return true;
}
else
{
	return false;
}
}

// ####
// Example

if (ui('test'))
{
echo htmlspecialchars($user['username']);
}
else
{
echo "Sorry, info for this user not found.";
}

?>

 

Not exactly what you want but you get the idea.

  Quote

<?php

function ui($username)
{
global $user;

$query = mysql_query("
	SELECT * FROM `users`
	WHERE `username` = '".mysql_real_escape_string($username)."'
	LIMIT 1
");

if (mysql_num_rows($query) == 1)
{
	$user = mysql_fetch_array($query, MYSQL_ASSOC);
	return true;
}
else
{
	return false;
}
}

// ####
// Example

if (ui('test'))
{
echo htmlspecialchars($user['username']);
}
else
{
echo "Sorry, info for this user not found.";
}

?>

 

Not exactly what you want but you get the idea.

 

Pretty much right, but for myself I would do a return with an array of the variables instead of using global.

<?php
function ui($user) {
// added limit 1, since you only need one row, yes?
$query = "select * from users where username = '".$user."' LIMIT 1";
$result = mysql_query($query) or die ("Could not match data because ".mysql_error());

// if there weren't any rows, return false, otherwise return the array 
if(mysql_num_rows($result) < 1) 
	return false;
 else 
	return mysql_fetch_array($result);
}

// call functions
$userInfo = ui('some_username');


// if it was returned as an array, we have data!
if(is_array($userInfo)) 
print_r($userInfo);
else
echo 'no data for this user!';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.