Hi I'm having trouble with some php code that I have been writing for my final year project at university and was wondering if any could have look...
The code is quite simply meant to open a database I have created on my local machine and build a table using HTML with its values. I'm using Xampp to check everything works but when I open this php file through my browser it just brings back a blank page.
The code:
<?php
//connect to the database
$mysqli = mysqli_connect("localhost", "my_username", "my_password", "my_database");
$content = "<h1>Antrim</h1>";
//check if the database has any records
$sql = 'SELECT branch.name, salesperson.salesman_no, salesperson.first_name, salesperson.last_name, hhc.hhc_asset_no, single_dock.single_d_asset_no, printer.printer_asset_no'
. ' FROM branch'
. ' LEFT JOIN ('
. ' salesperson, hhc, single_dock, printer'
. ' ) ON ( salesperson.branch_no = branch.branch_no'
. ' AND hhc.salesman_no = salesperson.salesman_no'
. ' AND single_dock.salesman_no = salesperson.salesman_no'
. ' AND printer.salesman_no = salesperson.salesman_no ) '
. ' WHERE salesperson.branch_no =001 LIMIT 0, 30 ';
$check_branch_res = mysqli_query($mysqli, $sql)
or die(mysqli_error($mysqli));
if (mysqli_num_rows($check_branch_res) < 1)
{
$content = "<p>There are no entries at this branch</p>";
}
else
{
$content = "
<table cellpadding=\"3\" cellspacing=\"2\" border=\"1\" width=\98%\">
<tr>
<th>Branch</th>
<th>Salesman No.</th>
<th>First Name</th>
<th>Last Name</th>
<th>HHC Asset No.</th>
<th>Single Dock Asset No.</th>
<th>Printer Asset No.</th>
</tr>";
while ($branch_info = mysqli_fetch_array($check_branch_res))
{
$bname = $branch_info['name'];
$id = $branch_info['salesman_no'];
$fname = $branch_info['first_name'];
$lname = $branch_info['last_name'];
$hhc_an = $branch_info['hhc_asset_no'];
$sdock_an = $branch_info['single_d_asset_no'];
$printer_an = $branch_info['printer_asset_no'];
$content = "
<tr>
<td align=\"center\">$bname<br></td>
<td align=\"center\">$id<br></td>
<td align=\"center\">$fname<br></td>
<td align=\"center\">$lname<br></td>
<td align=\"center\">$hhc_an<br></td>
<td align=\"center\">$sdock_an<br></td>
<td align=\"center\">$printer_an<br></td>
</tr>";
}
$content = "</table>";
}
?>
<html>
<head>
<title>Antrim Equipment List</title>
<link rel=StyleSheet href="main.css" type="text/css" media=all>
</head>
<body>
<?php echo $content;?> //this may be where the problem is happening
</body>
</html>
I think there may be a problem when the browser hits this line I've highlighted. The variable $content may not be saving the HTML code but I have no idea why. I'm sure it's something really simple but I just can't figure it.
Thanks for any help,
Jamie
PHP code bringin back blank page
Moderator: General Moderators
-
mickeyunderscore
- Forum Contributor
- Posts: 129
- Joined: Sat Jan 31, 2009 9:00 am
- Location: UK
Re: PHP code bringin back blank page
Problem seems to be that you keep overwriting the $content variable, so by the time it is output, it only contains '</table>'.
If you want to add additional information onto a variable, you need to use .=:
If you want to add additional information onto a variable, you need to use .=:
Code: Select all
$content = 'H';
$content .= 'e';
$content .= 'l';
$content .= 'l';
$content .= 'o';
echo $content; //Hello-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: PHP code bringin back blank page
Take a look at your statements that start and consider how many of them should be
A simple view source in your web browser should have identified this problem for you
Code: Select all
$content =Code: Select all
$content .=Re: PHP code bringin back blank page
Take a look at this example:
What happens when you run this script?
The result is:
It's not "red" and it's certainly not "redyellow"
This is because every time you define $content you're completely resetting its value.
If you'd like to continue to add additional data to a variable, you use the concatenating assignment operator, it looks like this:
The result is:
Good luck.
Code: Select all
$content = "red";
$content = "yellow";
echo $content;The result is:
Code: Select all
yellowThis is because every time you define $content you're completely resetting its value.
If you'd like to continue to add additional data to a variable, you use the concatenating assignment operator, it looks like this:
Code: Select all
$content = "red";
$content .= "yellow";
echo $content;Code: Select all
redyellowRe: PHP code bringin back blank page
Duh! I knew it would be something stupid. Many thanksfor the replies, I changed the code as suggested and it worked nicely!
Jamie
Jamie