PDA

View Full Version : Encasing PHP in a <td> tag



e1seix
07-12-2007, 05:43 PM
I'll explain. Have managed to get my php pages the way I want them to individually, but obviously I want to expand the pages to encompass link menus etc. so if my php code is...

<?php
// Make a MySQL Connection
mysql_connect("blah...", "blah...", "blah...") or die(mysql_error());
mysql_select_db("blah") or die(mysql_error());

$product_sku = $_GET['product_sku'];

// Get a specific result from the "example" table

$result = mysql_query("SELECT * FROM shaving WHERE product_sku=$product_sku AND DEF='T05'") or die(mysql_error());

echo "<table border='0' cellpadding='5' cellspacing='0' width='100%'>";
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td bgcolor='#ffff00'><p class='BRAPRO'>";
echo $row ['BRA']. " | ". $row['PRO'];
echo "</p></td></tr>";
}

echo "</table>";

?>

... why am i getting error messages when i try to do this...

<html>
<head></head>
<body>

<table>
<tr>
<td>

<?php
// Make a MySQL Connection
mysql_connect("blah...", "blah...", "blah...") or die(mysql_error());
mysql_select_db("blah") or die(mysql_error());

$product_sku = $_GET['product_sku'];

// Get a specific result from the "example" table

$result = mysql_query("SELECT * FROM shaving WHERE product_sku=$product_sku AND DEF='T05'") or die(mysql_error());

echo "<table border='0' cellpadding='5' cellspacing='0' width='100%'>";
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td bgcolor='#ffff00'><p class='BRAPRO'>";
echo $row ['BRA']. " | ". $row['PRO'];
echo "</p></td></tr>";
}

echo "</table>";

?>


<td>
</tr>
</table>

</body>
</html>

... can someone show me how?

djr33
07-12-2007, 05:47 PM
The only thing I notice is the <td> at the end of the code, which should be </td>, closing the cell that was holding all of that.

If there error is related to mysql, your database may not match the queries, but that's impossible to know just from the code.

What is the error you are getting?

e1seix
07-12-2007, 05:51 PM
sorry, the <td> tag is closed at the end. i just scrawled that quickly and missed it out. it's giving me a syntax error about 'AND DEF='t05"

note that it brings up that exact error when in fact it is coded AND DEF='T05'" correctly.

this code works alone when in it's own php page etc., but it just fails when i try to put it into the <td> tag.

e1seix
07-12-2007, 05:57 PM
strange as it sounds, it's just worked when i have built the table around the code, but it fails when i copy and paste the php code into the existing table code.

i don't get it. i occasionally get similar error messages always around the WHERE, ORDER BY or AND tags in requesting the info from the database.

can you tell me is this a common thing in php?

superjadex12
07-13-2007, 10:47 AM
I sometimes get those errors too. I don't know the exact cause but it sometimes helps when i put single quotes around any variables in the query. In this case you could try putting single quotes around the $product_sku .
Seems to me that mysql could use a little help in defining PRECISELY what it is to be looking for (by use of the quotes)

on a similiar note, it is always better to use single quotes in your echo statements. I notice you have it backwards. single quotes DO NOT parse the code contained within so for regular html this works faster. What you are doing right now is unecessarily parsing tons of regular html code...

GeneralB
07-13-2007, 05:11 PM
I'm new to this forum (and to php) and still haven't seen the link to create my own thread which I think would be more appropriate ... here it goes

Multiple Pages

How do I call a single file to multiple pages? I want to be able to change my links from one file that I've call column_left.php (hope that's right) to a lot of different html pages. How do I do this?

mwinter
07-13-2007, 06:02 PM
$product_sku = $_GET['product_sku'];

// Get a specific result from the "example" table

$result = mysql_query("SELECT * FROM shaving WHERE product_sku=$product_sku AND DEF='T05'") or die(mysql_error());


I hope that you don't do exactly that: you're exposing your database to injection attacks. Call the mysql_real_escape_string (http://uk.php.net/manual/en/function.mysql-real-escape-string.php) function and insert the returned value into the query. I also hope you don't really send MySQL error messages to the client upon failure: that can also expose your database to attacks by revealing the mechanics of your site and database. Moreover, it looks rubbish to end users. Fail gracefully, instead.

As for the actual problem, what does an actual query contain? That is, what is the value(s) of $product_sku in your tests? Is the resulting query actually legal?





echo "<table border='0' cellpadding='5' cellspacing='0' width='100%'>";
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td bgcolor='#ffff00'><p class='BRAPRO'>";
echo $row ['BRA']. " | ". $row['PRO'];
echo "</p></td></tr>";
}


Drop out of PHP when outputting significant amounts of plain text:



<table border="0" cellpadding="5" cellspacing="0" width="100%">
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr><td bgcolor="#ffff00"><p class="BRAPRO"><?php echo "{$row['BRA']} | {$row['PRO']}"; ?></p></td></tr>
<?php
}
?>
</table>

Preferably use CSS, rather than HTML attributes, to style documents, too.