/    Sign up×
Community /Pin to ProfileBookmark

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

[B]Error 1[/B]
My first error displays as

[QUOTE]

Parse error: syntax error, unexpected T_VARIABLE, expecting ‘,’ or ‘;’ in /Applications/XAMPP/xamppfiles/htdocs/taskone/extension.php on line 9

[/QUOTE]

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>[/CODE]

My PHP

[code=php]<html>
<body>

<?php

$x = $_REQUEST[“loops”];
while ($_REQUEST > 0)
{
echo “Welcome ” $_POST[“fname”] . $_POST[“sname”] .
” You are ” $_POST[“age”] ” years old.”;
$x–;
}

?>

</body>[/code]

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.

[B]Error 2[/B]
The second error that is displaying says

[QUOTE]

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

[/QUOTE]

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 [url]http://localhost/students1.php[/url] and when this is entered is the point at which the error is displayed.

students1.php

[code=php]<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>[/code]

dbinfo.inc.php

[code=php]<?php
$username=”root”;
$password=””;
$database=”students1″;
?> [/code]

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.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@simplypixieNov 24.2012 — In your first error, you have a few missing periods (full stops) in line 9
[code=php]echo "Welcome " . $_POST["fname"] . " " . $_POST["sname"] . " You are " . $_POST["age"] . " years old.";[/code]

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).
×

Success!

Help @AndrewShipway spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...