php code help.

I have a function with displays a list of files in a drop down form.

but I need to have the files shown in the list based on username.

ive tried this code in phpmyadmin and it works fine, but cant seem to get it working in the page.

Any ideas.

"SELECT * FROM files WHERE username = '$user'";

Code shown after login (index.php)

<?php

session_start();
include_once('../processes/functions.php');

$user = $_SESSION['user'];

$fileList = fn_getFileList();

echo "
function fn_getFileList()
{
	fn_dbConnect();
	$query = "SELECT * FROM files WHERE username = '$user'";
	$result = mysql_query($query);
	$numRows = mysql_num_rows($result);
	if (mysql_num_rows($result) > '0')
	{
		// Dump query result into file array
		while ($file = mysql_fetch_assoc($result))
		{
			// Push $file array into $fileList array
			$fileList[] = $file;
		}
		return $fileList;
	}
}

Open in new window

Cheryl LanderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

quincydudeCommented:
f (mysql_num_rows($result) > 0)
0
quincydudeCommented:
if (mysql_num_rows($result) > 0)
0
quincydudeCommented:
try
function fn_getFileList()
{
        fn_dbConnect();
        $query = "SELECT * FROM files WHERE username = '$user'";
        $result = mysql_query($query);
        $numRows = mysql_num_rows($result);
        if (mysql_num_rows($result) > 0)
        {
                var i = 0;
                // Dump query result into file array
                while ($file = mysql_fetch_assoc($result))
                {
                        // Push $file array into $fileList array
                        $fileList[i] = $file[0];
                        i++; 
                }
                return $fileList;
        }
}

Open in new window

0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

k_dietzCommented:
make sure you're identifying $user from your session variable within your function to make sure it's showing in your SQL query.
0
hieloCommented:
make sure that your fn_dbConnect updates a global variable and also declare that variable as global within your fn_getFileList().
$link=NULL;
fn_dbConnect(){
 global $link;
 $link=mysql_connect("localhost","username","password") or die(mysql_error);
 mysql_select_db("dbName") or die( mysql_error());
}
function fn_getFileList()
{
 global $link;
	fn_dbConnect();
	$query = "SELECT * FROM files WHERE username = '$user'";
	$result = mysql_query($query,$link);
	$numRows = mysql_num_rows($result);
	if (mysql_num_rows($result) > '0')
	{
		// Dump query result into file array
		while ($file = mysql_fetch_assoc($result))
		{
			// Push $file array into $fileList array
			$fileList[] = $file;
		}
		return $fileList;
	}
}

Open in new window

0
Michael701Commented:
$user is NOT available inside the function

use
$fileList = fn_getFileList($user);

and add the variable to the parameter list
function fn_getFileList(&$user)
{
        fn_dbConnect();
        $query = "SELECT * FROM files WHERE username = '$user'";

Open in new window

0
Cheryl LanderAuthor Commented:
heilo
Parse error:  syntax error, unexpected T_GLOBAL in functions.php on line 34

quincydude:
Parse error:  syntax error, unexpected T_VAR in functions.php on line 40

Michael701:
Warning:  Missing argument 1 for fn_getfilelist() in /functions.php on line 32


0
quincydudeCommented:

function fn_getFileList()
{
        fn_dbConnect();
        $query = "SELECT * FROM files WHERE username = '$user'";
        $result = mysql_query($query);
        $numRows = mysql_num_rows($result);
        if (mysql_num_rows($result) > 0)
        {
                var i = 0;
                // Dump query result into file array
                while ($file = mysql_fetch_array($result))
                {
                        // Push $file array into $fileList array
                        $fileList[i] = $file[0];
                        i++; 
                }
                return $fileList;
        }
}

Open in new window

0
hieloCommented:
>>heilo
>>Parse error:  syntax error, unexpected T_GLOBAL in functions.php on line 34
I don't know what line 34 is so it is helps if you point out what those lines are. However:
there is a missing "function" keyword in front of fn_dbConnect and missing parenthesis after the first mysql_error. Try:
$link=NULL;
function fn_dbConnect(){
 global $link;
 $link=mysql_connect("localhost","username","password") or die(mysql_error());
 mysql_select_db("dbName") or die( mysql_error());
}
function fn_getFileList()
{
 global $link;
$filelist=array();
$user = mysql_real_escape_string($_SESSION['user']);
	fn_dbConnect();
	$query = "SELECT * FROM files WHERE username = '$user'";
	$result = mysql_query($query,$link) or die(mysql_error());
	$numRows = mysql_num_rows($result);
	if ($numRows  > 0)
	{
		// Dump query result into file array
		while ($file = mysql_fetch_assoc($result))
		{
			// Push $file array into $fileList array
			$fileList[] = $file;
		}
	}
return $fileList;
}

Open in new window

0
Cheryl LanderAuthor Commented:
quincydude:

Parse error:  syntax error, unexpected T_VAR in /home/bodyacti/public_html/intranet/processes/functions.php on line 40

line 40 = var i = 0;


heilo.
Warning:  mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home/bodyacti/public_html/intranet/processes/functions.php on line 36

Warning:  mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/bodyacti/public_html/intranet/processes/functions.php on line 36

Warning:  mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/bodyacti/public_html/intranet/processes/functions.php on line 39

line 36 =  mysql_select_db("dbName") or die( mysql_error());

line 39 = {

0
Cheryl LanderAuthor Commented:
heilo.
function
 fn_dbConnect
(){
already gets the user pass etc.
I think its trying to process it twice.

ive changed the values to meet user pass etc.

Open in new window

0
hieloCommented:
>>I think its trying to process it twice.
I'm not sure what you are doing, but I included function fn_dbConnect() so you see how I declare the $link variable with global within the function AND also within function fn_getFileList() so that when you call mysql_real_escape_string it works as expected. So what I posted was meant to illustrate the point of what you need to do with the $link variable.

>>line 36 =  mysql_select_db("dbName") or die( mysql_error());
 I am obviously connecting to some non-existent db, but again, it was not meant for you to execute it exactly as provided. You need to use whatever username and password are necessary and use the right db name. The point is that you need a global $link variable.
0
Cheryl LanderAuthor Commented:
Sorry quote new to this.

>>line 36 =  mysql_select_db("dbName") or die( mysql_error());
 Iam obviously connecting to some non-existent db, but again, it was notmeant for you to execute it exactly as provided. You need to usewhatever username and password are necessary and use the right db name.The point is that you need a global $link variable.

I had done this.


0
hieloCommented:
post all your code and specify the lines you are seeing errors. If you respond within 5min I'll be able to take a look at it.
0
quincydudeCommented:
oops i m getting crazy
function fn_getFileList()
{
        fn_dbConnect();
        $query = "SELECT * FROM files WHERE username = '$user'";
        $result = mysql_query($query);
        $numRows = mysql_num_rows($result);
        if (mysql_num_rows($result) > 0)
        {
                $i = 0;
                // Dump query result into file array
                while ($file = mysql_fetch_array($result))
                {
                        // Push $file array into $fileList array
                        $fileList[$i] = $file[0];
                        $i++; 
                }
                return $fileList;
        }
}

Open in new window

0
agamalCommented:
Still working on it ... or solved ......
0
Cheryl LanderAuthor Commented:
still working on it ;-)
0
agamalCommented:
Pls submit the complete code and tell me where is the errors ???

0
Cheryl LanderAuthor Commented:
Ive provided the original code and errors. Ive just gone back into it and the errors as posted above are still occuring.
0
hieloCommented:
Based on what you posted originally, instead of:

<?php
 
session_start();
include_once('../processes/functions.php');
$user = $_SESSION['user'];
$fileList = fn_getFileList();
...
 
simply try:
<?php
session_start();
include_once('../processes/functions.php');
$user = $_SESSION['user'];
$fileList = fn_getFileList($user);
...

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cheryl LanderAuthor Commented:
works great.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.