When using Greater Than symbols (>) in php code I have output dumps in html that cease processing at these symbols, though my text editor (Ultraedit) says there are no syntax errors, and running the script results in correctly parsed output as far as I can tell.
I'm very new at php, so I'm assuming my understanding of what I'm looking at is flawed. I'm using a local production version AMP setup on a windows XP machine, using PHP 5.2.1, Apache 2.2.4, and MySQL 5.0.33
dataselection.html:
<html>
<title>Temp Title</title>
<head>What we have here is a header as a placeholder, I'll worry about the rest if I can get the blasted PHP code figured out.<br></head>
<?php
$host = 'localhost';
$user = 'dweeble';
$pass = 'feezle938';
//Connect to server
$connection = mysql_connect($host,$user,$pass) or die "<b>".(mysql_errno().":".mysql_error()."</b>");
[B][COLOR="Red"]// #1: Error at this point with bold designations. Remainder of code
//dumps to html as text string with last > symbol in line. If I remove them, then the
//code proceeds correctly to next failure point. See temp1.jpg attached.[/COLOR][/B]
//Define database name
$dbname = 'loots';
//Load the database
mysql_select_db($dbname);
//Define the select string
$sql = "SELECT * FROM tblloots GROUP BY Mob ORDER BY Mob ASC";
//Execute select string
$query = mysql_query($sql);
//Loop through string, create option for each entity
if ($query) {
if (mysql_num_rows($query) > 0) {
[B][COLOR="red"]//#2 Error on > symbol. remainder of code dumps to html as a text
//string. Can be adjusted from > to != instead to bypass. temp2.jpg attached.[/COLOR][/B]
while ($row = mysql_fetch_assoc($query)) {
$name=$row["Mob"];
$num=$row["ID"];
$mobByNum[$num] = $name;
}
}
mysql_free_result($query);
}
//Close connection to server
mysql_close($connection);
?>
<body>
<form name="mobselect" action="lbMob.php" method="POST">
<p> Mob Name or Abbreviation:
<select name="Mob">
<?php
foreach ($mobByNum as $num => $name)
{
echo "<option value=\"$num\">$name</option>\n";
[B][COLOR="red"]//#3 Error point, this echo command is only posted to combobox as $name.
//If I run script in Ultraedit it parses to the correct option list. I can then copy and paste that to a
//new html file and it works, but there is no dynamic link then. temp3.jpg attached.[/COLOR][/B]
}
?>
</select>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
I have three points of failure, two of which I can remove, though I lose desired functionality, or precision in error checking. The third failure point I cannot seem to solve, and is giving me the most problems. #1 above is an attempt to bold the error if a database connection cannot be made, but results in the output seen in temp1.jpg. Which corresponds to the first character after the final > in the connection string line of the code.
Error #2, being similar, if I remove the bolding statements from the connection code, it works to error point 2, and results in the output seen in temp2.jpg. Again it dumps out at the greater than symbol. I correct this by checking for != 0 instead, and I believe this is equally valid as you cannot have negative number of records in your table, but am confused as to why it will not allow the > symbol? Almost all examples I find use this convention.
Error #3 is the most frustrating however. The combobox output is only listing the variable as a string, it does not seem to be parsing the variable from the array and inserting it into the combobox select options. When the script is run via Ultraedit the parse shows the correct database results as a list of options, which I can then copy and paste to a new form and it seems to work. Unfortunately I lose my dynamic link to the database, which I'd prefer not to do. These are the various output strings I've tried in the foreach loop; all of which result in simply a variable as a string for output:
echo "<option value=\"$num\">$name</option>\n";
echo "<option value=\"$num\">{$name}</option>\n";
echo "<option value=\"$num\">.$name.</option>\n";
echo "<option value=\"$num\">".$name."</option>\n";
echo "<option>$name</option>\n";
echo "<option>.$name.</option>\n";
echo "<option>".$name."</option>\n";
echo "<option>".{$name}."</option>\n";
Also tried the above as print statements instead of echo, but no variance.
I'm not certain where to go from here. Two days of searching the web hasn't yielded any further insights either. From what I can tell the string variable $name is supposed to be filled in with the value from the array, but it's being treated simply as a text string when the html file is ran, and all attempts so far have resulted in simply an option of $name or some variant thereof depending on my echo/print string.