[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

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

0
Cheryl Lander
Asked:
Cheryl Lander
  • 7
  • 5
  • 5
  • +3
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
Cheryl LanderAuthor Commented:
works great.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 5
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now