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 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP new coder problem

    I keep getting the error:
    Warning: extract() expects first argument to be an array in c:\program files\apache group\apache\htdocs\test2.php on line 20

    I cant figure out what is wrong can anyone help me?


    <html>
    <head>
    <title>PHP Test</title>
    </head>
    <body>
    <?php
    $host = "Localhost";
    $user="root";
    $password="Duranies";
    $database="MemberDirectory";
    $connection = mysql_connect($host,$user,$password)
    or die("The database is unavailable at the moment");
    $db = mysql_select_db($database,$connection)
    or die("DB is Unavailable");
    $userEntry = "Secret";
    $query = "SELECT password FROM Member WHERE loginName='gsmith'";
    $result = mysql_query($query)
    or die ("couldnt execute query.");
    $row = mysql_fetch_array($result,MYSQL_ASSOC);
    extract($row);
    if ($userEntry== $row['password'])
    {
    echo "Login Accepted<br>";
    }
    else
    {
    echo "invalid password<br>";
    }
    ?>
    </body>
    </html>

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    what do you get if you <?print_r($row);?> before the extract ? , e.g. is $row empty?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages
    what do you get if you <?print_r($row);?> before the extract ? , e.g. is $row empty?
    Hmm. I would suspect to get the error on the mysql_fetch_array() then.

    Anyway, i don't understand the logic in your code. I'd replace
    PHP Code:
    $userEntry "Secret";
    $query "SELECT password FROM Member WHERE loginName='gsmith'";
    $result mysql_query($query)
    or die (
    "couldnt execute query.");
    $row mysql_fetch_array($result,MYSQL_ASSOC);
    extract($row);
    if (
    $userEntry== $row['password'])
    {
    .... 
    by
    PHP Code:
    $query "SELECT count(*) as numcheck FROM Member WHERE loginName='gsmith' and password='Secret'";
    $result mysql_query($query) or die ("couldnt execute query.");
    if (
    mysql_num_rows($result)!=1){
        die (
    'Possible databaseproblem.');
    } else {
        
    $row mysql_fetch_assoc($result);
        if (
    $row['numcheck']==1){
        ... 
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #4
    New Coder
    Join Date
    Apr 2004
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I copyied the code exactly out of the textbook im so damm sick of it containing errors can anyone suggest a good book for someone looking to learn php & mysql?

    Any chance you guys could actually explain in laymans terms the lines that are wrong and why! I can easily swap bad code for the lines you suggest but im no nearer to understanding why it didnt work ! Thanks
    Last edited by Bazrazmataz; 04-19-2004 at 04:55 PM.

  • #5
    New Coder
    Join Date
    Apr 2004
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Im also getting a error message with this chunk of code:

    Parse error: parse error in c:\program files\apache group\apache\htdocs\petsend.php on line 25


    <html>
    <head><title>Pet Catalog</head>
    <body>
    <?php
    $user="root";
    $host="localhost";
    $password="Duranies";
    $database="petCatalog";
    $connection = mysql_connect($host,$user,$password)
    or die("Couldnt connect to database");
    $db=mysql_select_db($database,$connection)
    or die("Couldnt connect to db");
    $pettype="horse";
    $query= "select * from pets where petType='$pettype'";
    $result = mysql_query($query)
    or die("Could not execute query");

    echo "<h1>Horses</H>";
    echo "<table cellspacing='15'>";
    echo"<tr><td colspan='3'><hr></td></tr>;
    while ($row = mysql_fetch_array($result))
    {
    extract($row);
    $f_price = number_format($price,2);
    echo"<tr>\n
    <td>$petDescription</td>\n
    <td align='right'>\$$f_price</td>\n
    </tr>\n";
    echo "<tr><td collspan='3'><hr></td></td>\n";
    }
    echo "</table>\n";
    ?>
    </body>
    </html>

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I can understand your frustration, but PHP is a language that is very much alive and new functions are born and syntaxe-rules change.
    So by the time you buy a book, it's bound to be outdated.
    Also, 100 pieces of examplecode is not the same as an application with 100 pages of code. Each example is propably set up to be an independent piece of code, and therefore becomes a bit artifical and unefficient.
    If you wan't to learn to code, then you also need to read a lott of code. And look up everything you don't understand in the manual or in forums like this one.

    In the code you post, there is not a single line of code that is completely correct. (That is, is addition to being unefficient an not very logicaly structured.)
    As i'll gladly demonstrate
    PHP Code:

    /* These 4 variables out simply a waste of space. 
    - Unnescecary;
    - Unefficient the stringvalues are surrounded in double quotes, which means they'll be unnescecary parsed
    - Unsecure :
       * your connectiondetails should not be inside your script, but inside a server side include with a php-extension, that is preferably stored in a directory that is not accesible from the web
       * Unsecure because the variables keep this value throughout the scriptexecution.  You should unset the variables after the connection is made.   
    $host = "Localhost";  
    $user="root";
    $password="Duranies";
    $database="MemberDirectory";
    */
    /* Your connectinstring should be inside an include, and there, they should be made up like this
    $connection = @mysql_connect('localhost','root','Duranies') or die('The database is unavailable at the moment');
    $db = @mysql_select_db('MemberDirectory',$connection) or die('DB is Unavailable');
    Notice the @ to suppres errormessage, that would leak sensitive infoabout your muSQL account and db.
    Your incmludestament then is something like */
    require('./36ghht5vvcx56vhg.php');
    /* another pointless variable and some ilogic code that should be replaced . See below? The individual problems
    $userEntry = "Secret";   ---> pointles, double quotes
    $query = "SELECT password FROM Member WHERE loginName='gsmith'";  --> you don't need userdata so don't select it. Just do a count
    $result = mysql_query($query) or die ("couldnt execute query.");   --> use the linkidentifier inside themysql_query
    $row = mysql_fetch_array($result,MYSQL_ASSOC);  --> simply use the more elegant mysql_fetch_assoc()
    --> you shoul also check the number of returned rows before starting to loop through the recordset. 
    extract($row);   --> completely pointless since on the next line you use the $row-array !!
    if ($userEntry== $row['password'])  --> should be if ($userEntry== $password)
    The alternative:*/
    $query "SELECT count(*) as numcheck FROM Member WHERE loginName='gsmith' and password='Secret'"
    $result mysql_query($query,$connection) or die ('couldnt execute query.'); 
    if (
    mysql_num_rows($result)!=1){ 
       die(
    'Possible databaseproblem.'); 
    } else { 
       
    $row mysql_fetch_assoc($result); 
       if (
    $row['numcheck']==1){ 
            echo 
    'Login Accepted<br />';  //These echo's were almost perfect
       
    } else { 
            echo 
    'invalid password<br />';
       } 


    About your latest post : what is line 25 ?
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    New Coder
    Join Date
    Apr 2004
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow thanks !

    This is so damm hard ive decided to ditch this for dummies book and get something rite up 2 date ive kinda lost my faith in it but im determined to learn! You seem like you know your stuff reall well raf how long did it take u to get to grips with the language?


  •  

    Posting Permissions

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