www.webdeveloper.com
Results 1 to 2 of 2

Thread: A few errors for a PHP beginner

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    1

    A few errors for a PHP beginner

    I'm new to PHP and have been studying for 2 weeks as part of one of my university modules. I have a good understanding of HTML but PHP is a completely new concept to me. My only previous knowledge in a programming language is an introductory level in Java. I've been struggling with a couple of errors for a few hours now and have searched through several websites include W3Schools and PHP.net for solutions but have had no luck as of yet.

    My operating system is OSX 10.6.8
    I am using XAMPP


    Error 1
    My first error displays as
    Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /Applications/XAMPP/xamppfiles/htdocs/taskone/extension.php on line 9
    My HTML
    Code:
    <html>
    <body>
    
    <form action="extension.php" method="post">
    Name: <input type="text" name="fname"><br />
    Surname: <input type="text" name="sname"><br />
    Age: <input type="text" name="age"><br />
    Number of loops:<select name="loops">
    <option value="1" selected="selected">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    </select><br/>
    <input type="submit">
    </form>
    
    </body>
    </html>
    My PHP
    PHP Code:
    <html>
    <body>

    <?php

    $x 
    $_REQUEST["loops"];
    while (
    $_REQUEST 0)
    {
    echo 
    "Welcome " $_POST["fname"] . $_POST["sname"] . 
    " You are " $_POST["age"" years old.";
    $x--;
    }

    ?>

    </body>
    The purpose of the drop down box is for the user to decide how many times they wish for the Name, Surname and Age to be printed. Prior to the above error, I was receiving a different error for the while loop on line 7 and am not entirely sure if or how I have solved that.

    Error 2
    The second error that is displaying says
    Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0

    Fatal error: Unknown: Failed opening required '/Applications/XAMPP/xamppfiles/htdocs/tasktwo/students1.php' (include_path='.:/Applications/XAMPP/xamppfiles/lib/php:/Applications/XAMPP/xamppfiles/lib/php/pear') in Unknown on line 0
    This error is being caused by a database I have created in MySQL named students1. The database contains one table named students1 and in this table there are three entries. I have been given the following two PHP files by my lecturer and told to access it by http://localhost/students1.php and when this is entered is the point at which the error is displayed.

    students1.php
    PHP Code:
    <html>

    <head>Getting data from a MySQL database using PHP</head>

    <body>

    <p>This php powered page will print out the contents of a table in a database

    <?php

    // This part of the code sets up our database connection
    // The credentials are in the file dbinfo.inc.php
    // Once the connection is made, we make the query

    include("dbinfo.inc.php");
    mysql_connect(localhost,$username,$password);
    @
    mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM table1";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    mysql_close();
    ?>

    <table>
    <tr> 
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
    </tr>


    <?php

    // This bit of the code sets up a loop and gets the 
    // results from the query row at a time

    $i=0;
    while (
    $i $num) {
    $firstname=mysql_result($result,$i,"firstname");
    $lastname=mysql_result($result,$i,"lastname");
    $age=mysql_result($result,$i,"age");
    ?>

    <tr>
    <td><?php echo "$firstname?></td> 
    <td><?php echo "$lastname?></td>
    <td><?php echo "$age?></td>
    </tr>

    <?php
    $i
    ++;

    echo 
    "</table>";
    ?>

    </body>
    </html>
    dbinfo.inc.php
    PHP Code:
    <?php
    $username
    ="root";
    $password="";
    $database="students1";
    ?>


    I'm new to the forum so am not sure if I've posted correctly, I have checked your forums sticky posts and the FAQs and tried to keep by them. If any extra info is required please let me know.

    Any help is much appreciated.

  2. #2
    Join Date
    Aug 2012
    Posts
    155
    In your first error, you have a few missing periods (full stops) in line 9
    PHP Code:
    echo "Welcome " $_POST["fname"] . " " $_POST["sname"] . " You are " $_POST["age"] . " years old."
    I am not 100% sure about your 2nd error, but looking at your query, you are trying to select from a table called table1 where you should be selecting from the table called students1 (according to what you have written).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles