Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Whats wrong with this code ??

    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 ??

    PHP Code:
    // 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


  • #2
    New Coder
    Join Date
    Aug 2002
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Whats wrong with this code ??

    No closing } for the for in the db_result_to_array function?

    PHP Code:
    // 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]"; }

    Last edited by Galdo; 04-17-2003 at 10:01 PM.

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ?


  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Whats wrong with this code ??

    I've commented in some of the bits that look wrong

    PHP Code:
    // 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.

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:

    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:

    PHP Code:

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

    Last edited by duniyadnd; 04-17-2003 at 10:43 PM.

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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/showthre...threadid=18239

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    Regular Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    PHP Code:
    // 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"]; }


  • #11
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Real quick before i go through the whole code

    PHP Code:
    if ($_SESSION['news'] = 'true'
    Should be

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

    Duniyadnd

  • #12
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #13
    Regular Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ??


  • #14
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not too fond of this function, changed it around a little bit

    PHP Code:

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

    return 
    $res_array;

    Last edited by duniyadnd; 04-17-2003 at 11:06 PM.

  • #15
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry to be a pain
    We'll live.. Believe me, if you were a pain, we would stop replying.

    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


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •