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.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts

    Problem getting info from database

    Getting this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/xtraz/public_html/test/news/post_news/index.php on line 4

    What exactly does this error mean? A whitespace is a space after an end tag right? What about T_ENCAPSED?

    Code:
    <?php
    session_start();
    include "database.php";
    $level = "SELECT level from user WHERE userid='$_SESSION['userid']'";
    $result = mysql_query($level,$con);
    echo (mysql_fetch_array($result));
    if($level !=4)
    {
    echo <meta http-equiv='refresh' content="0;url=">;
    }
    ?>
    
    <form name="news" action="" method="post">
    <input type="text" name="title" value="Title" />
    <textarea rows="2" cols="20">
    </textarea>
    
    <?php
    $query = "INSERT INTO news(title, news, date) VALUES('mysql_real_escape_string($_POST[title])','mysql_real_escape_string($_POST[news])', NOW())"; 
    mysql_query($query); 
    print "News successfully added."; 
    mysql_close($c);
    ?>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    Nothing to do with database. That's strictly a PHP syntax issue. You'd have the same error in code unrelated to a database if you made the same error.

    I'm *NOT* a PHP person, but I'd guess that PHP is having trouble with your " and ' nesting.

    Why not clarify the code, thus:
    Code:
    $level = "SELECT level from user WHERE userid='" . $_SESSION["userid"] . "'";
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Old Pedant View Post
    Nothing to do with database. That's strictly a PHP syntax issue. You'd have the same error in code unrelated to a database if you made the same error.

    I'm *NOT* a PHP person, but I'd guess that PHP is having trouble with your " and ' nesting.

    Why not clarify the code, thus:
    Code:
    $level = "SELECT level from user WHERE userid='" . $_SESSION["userid"] . "'";
    Parse error: syntax error, unexpected '<' in /home/xtraz/public_html/test/news/post_news/index.php on line 9

    crap, thought this was mysql since it was getting the info from the database

    EDIT: Nvm. Fixed that error/ Now getting: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/xtraz/public_html/test/news/post_news/index.php on line 5

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xtraz/public_html/test/news/post_news/index.php on line 6

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    Again, I'm not a PHP person, but in this line:
    Code:
    $result = mysql_query($level,$con);
    $level can't possibly be an invalid "resource" because it's only a string. But $con well could be invalid.

    Normally, in PHP code I see, where the user only has one DB connection per page, the second argument to mysql_query() is omitted, since then mysql will use the default current connection.

    You might try that:
    Code:
    $result = mysql_query($level);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Old Pedant View Post
    Again, I'm not a PHP person, but in this line:
    Code:
    $result = mysql_query($level,$con);
    $level can't possibly be an invalid "resource" because it's only a string. But $con well could be invalid.

    Normally, in PHP code I see, where the user only has one DB connection per page, the second argument to mysql_query() is omitted, since then mysql will use the default current connection.

    You might try that:
    Code:
    $result = mysql_query($level);

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xtraz/public_html/test/news/post_news/index.php on line 6

    error after error

    EDIT: Nvm fixed tht error. But now its echoing array at the top of the page
    Last edited by elitis; 05-11-2011 at 12:48 AM.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    Have to get somebody who knows PHP involved, I'm afraid.

    You might also need to show the code for the database.php file.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Old Pedant View Post
    Have to get somebody who knows PHP involved, I'm afraid.

    You might also need to show the code for the database.php file.
    the database file is just normal standard code for connecting to the database.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    Quote Originally Posted by elitis View Post
    But now its echoing array at the top of the page
    Yes?

    What did you expect.

    You are doing this:
    Code:
    echo (mysql_fetch_array($result));
    You asked it to fetch an array, and then you echoed the array.

    And the next line makes less sense:
    Code:
    if($level !=4)
    But that will *always* be true.

    You previously assigned the *STRING* containing the SQL command to the $level variable.

    And that STRING will never be equal to the number 4.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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