...

View Full Version : Whats wrong with this code ??



wap3
04-17-2003, 09:49 PM
Hi guys

I seem to be a bit rusty at php, cos I haven't touched it for a while.

Can somebody see why this doesnt work ??



// start a new session
session_start();

// include site reference file
include ('../php/site_includes.php');

// check if the user is logged in
if (!session_is_registered("news"))
header("Location:../members/er_login.php");
else
$details = get_name();
state_name($details);

// retrieves name from database
function get_name() {
$conn = db_connect();
$query = "Select * from users where username = News";
$result = @mysql_query($query);
$items_array = db_result_to_array($result);
return array($$items_array);
}

// turns results from a query into an array
function db_result_to_array($result) {
$res_array = array();
for ($count=0; $row = @mysql_fetch_array($result); $count++)
$res_array[$count] = $row;
return $res_array;
}

function state_name($details) {
foreach ($details as $row) {
echo "$row[firstname]"; }
}

thanks

:thumbsup:

Galdo
04-17-2003, 09:59 PM
No closing } for the for in the db_result_to_array function?



// start a new session
session_start();

// include site reference file
include ('../php/site_includes.php');

// check if the user is logged in
if (!session_is_registered("news"))
header("Location:../members/er_login.php");
else
$details = get_name();
state_name($details);

// retrieves name from database
function get_name() {
$conn = db_connect();
$query = "Select * from users where username = News";
$result = @mysql_query($query);
$items_array = db_result_to_array($result);
return array($$items_array);
}

// turns results from a query into an array
function db_result_to_array($result) {
$res_array = array();
for ($count=0; $row = @mysql_fetch_array($result); $count++)
$res_array[$count] = $row;

}

return $res_array;
}

function state_name($details) {
foreach ($details as $row) {
echo "$row[firstname]"; }
}

wap3
04-17-2003, 10:03 PM
hmm, no that doesn't seem to be it.

There is only one opening bracket, hence one closing.

I tried it but it just gives me a parse error.

Any more ideas ?

:confused:

Íkii
04-17-2003, 10:04 PM
also note:

session_is_registered is deprecating/ed in recent builds - you should be moving toward declaring and testing session variables through the $_SESSION array

session_start();
$_SESSION['news'] = 'true';

if(isset($_SESSION['news']))

sort of syntax.

wap3
04-17-2003, 10:13 PM
Ok thanks for that Okii I have changed that over to keep up with the standards.

Told you I am abit rusty

Still stuck though :confused:

missing-score
04-17-2003, 10:23 PM
I've commented in some of the bits that look wrong



// start a new session
session_start();

// include site reference file
include ('../php/site_includes.php');

// check if the user is logged in
if (!session_is_registered("news"))
header("Location:../members/er_login.php");
else
$details = get_name();
state_name($details);

// retrieves name from database
function get_name() {
$conn = db_connect(); // Im assuming that you have this function defined and that it connects to the database properly?

$query = "SELECT * FROM `users` WHERE username = 'News'"; // No quotes around News, there should be as you are searching for a string. I put it to uppercase, although it shouldnt make a difference.

$result = mysql_query($query); // Take out the @ to check the error is not happening here
$items_array = db_result_to_array($result);
return array($items_array); // 2 $ signs, removed one.
}

// turns results from a query into an array
function db_result_to_array($result) {
$res_array = array();
for ($count=0; $row = mysql_fetch_array($result); $count++) // Again, removed @ to check error is not happening here.
$res_array[$count] = $row;
return $res_array;
}

function state_name($details) {
foreach ($details as $row) {
echo $row["firstname"];
// Took out double quotes as they are not needed. I put them within the $row[firstname] as it is better to use quotes here.
}
}


Hope that helps you.

duniyadnd
04-17-2003, 10:36 PM
if (!session_is_registered("news"))
header("Location:../members/er_login.php");
else
$details = get_name();
state_name($details); //this line takes place no matter what condition




That means if your session is not registered, then you still go to state_name($details) where $details is empty.

Fix:




if (!session_is_registered("news"))
header("Location:../members/er_login.php");
else
{
$details = get_name();
state_name($details);
}

missing-score
04-17-2003, 10:41 PM
You are probaly best putting curly braces around all your if/else statements.

1: it makes it easier to read.
2: You dont get occurances like that ( well spotted duniyadnd ;) )


We were talking a bit about standards here:

http://www.codingforums.com/showthread.php?s=&threadid=18239

duniyadnd
04-17-2003, 10:48 PM
Just to add on to some stuff missing-score said, and through his example, it helps a lot, don't put any @ signs until after you checked your code, that way you can see what the errors are all about.

Duniyadnd

wap3
04-17-2003, 10:49 PM
Ok no luck still. I have tried making all the changes you guys have kindly suggested but it does nothing.

This is what I have now, after the modifications.



// start a new session
session_start();

// include site reference file
include ('../php/site_includes.php');

// check if the user is logged in
if ($_SESSION['news'] = 'true') {
$details = get_name();
state_name($details); }
else {
header("Location:../members/er_login.php");
}


// retrieves name from database
function get_name() {
$conn = db_connect();
$query = "SELECT * FROM users WHERE username = 'News'";
$result = mysql_query($query);
$items_array = db_result_to_array($result);
return array($items_array);
}

// turns results from a query into an array
function db_result_to_array($result) {
$res_array = array();
for ($count=0; $row = mysql_fetch_array($result); $count++)
$res_array[$count] = $row;
return $res_array;
}

function state_name($details) {
foreach ($details as $row) {
echo $row["firstname"]; }
}

duniyadnd
04-17-2003, 10:51 PM
Real quick before i go through the whole code


if ($_SESSION['news'] = 'true')

Should be


if ($_SESSION['news'] == 'true')

What you did was say that $_SESSION['news'] IS equal to 'true' regardless of the facts, instead of comparing.

Duniyadnd

missing-score
04-17-2003, 10:53 PM
Right, now I hope I haven't missed anything, but what is going wrong? You say it is not working, but what is not working?

1. Make sure you db column is no Username instead of username ( caps )

2. If no luck, post the db_connect() function.

3. Show us any errors you have.

wap3
04-17-2003, 11:03 PM
Hi guys

Sorry to be a pain

With that latest piece of code I posted there (with the addition of the ==), I have no errors at all.

The database connection code works fine, because I am using it on the front login page and that all works fine.

And I have checked the database field names and its all fine and the data needed is in the table

its so weird ??

:confused:

duniyadnd
04-17-2003, 11:03 PM
Not too fond of this function, changed it around a little bit




function db_result_to_array($result)
{
$count = 0;
while ($row = mysql_fetch_array($result))
{
$res_array[$count] = $row;
$count++;
}

return $res_array;
}

duniyadnd
04-17-2003, 11:05 PM
Sorry to be a pain

We'll live.. ;) Believe me, if you were a pain, we would stop replying. :thumbsup:


I have no errors at all

You won't always get error messages, but if you have logic put down in code that contradicts what you want to do with the correct syntax, that's what you have to look for.

Duniyadnd

missing-score
04-17-2003, 11:08 PM
What is not working?????? Is it just the code in general, or does some parts just not work?

This would really help if you could tell us, cos it streamlines debugging alot... :thumbsup:

wap3
04-17-2003, 11:33 PM
Hi

It just does nothing,

It should list the firstnames in the database but it does noting.
No error messages, no nothing.

:confused:

missing-score
04-17-2003, 11:39 PM
I have a nice sql_script pak that I made. It has basic sql functions (more coming) and also some similar to some of the functions you are using.

Take a look. I need to put some more documentation, but this version should be ok.

It makes a debugging and error report of what happens, it may help you.

wap3
04-18-2003, 12:53 AM
Hi just to let you know I seem to have figured it out now.

This bit of the code:



// check if the user is logged in
if ($_SESSION['news'] = 'true') {
$details = get_name();
state_name($details); }
else {
header("Location:../members/er_login.php");
}


should be:



// check if the user is logged in
if ($_SESSION['news'] = 'true') {
list($details) = get_name();
state_name($details); }
else {
header("Location:../members/er_login.php");
}


It seems to work . . . for now anyway.

Thanks for all your help in trying to solve that


:thumbsup:

duniyadnd
04-18-2003, 07:23 AM
Great, glad it works now. Though there seems to be one problem in the code you submitted above. Again the "if statement", you are setting a value, not comparing, you should have "==" rather than "=" there.

Otherwise it will never go to this line:


header("Location:../members/er_login.php");

Duniyadnd

wap3
04-18-2003, 02:42 PM
Yer your right duniyadnd,

I did have that already, just missed it out here.

thanks

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum