I've googled and tried several things all to no avail. I'm taking some classes in PHP and MySQL and I'm trying to add alternating color to the rows in array results. So far nothing has worked. I'm not sure where to put it. This is the code I have:
//assemble the sql query string to insert a record
$query="SELECT * FROM Vehicles WHERE buyer IS NOT NULL ORDER BY StockID asc";
//open a connection and select the database
$conn=mysql_connect($host, $username, $password);
@mysql_select_db($database, $conn) or die ("Unable to select $database database");
//Execute the sql query
if($result=mysql_query($query, $conn)){
$rows=mysql_numrows($result);
echo"<p>There are $rows cars in the system</p>";
?>
<table border="0" align="center" border="0" cellspacing="0" style="margin:auto; border-spacing:2px 1px; border-collapse:separate; border:ridge 5px; border-color:#669999;">
<tr>
<td style="background-color:#92DEDC">Stock ID</td>
<td style="background-color:#92DEDC">Reg Number</td>
<td style="background-color:#92DEDC">Make</td>
<td style="background-color:#92DEDC">Model</td>
<td style="background-color:#92DEDC">Year</td>
<td style="background-color:#92DEDC">Cost Price</td>
<td style="background-color:#92DEDC">EmpID</td>
<td style="background-color:#92DEDC">Sell Price</td>
<td style="background-color:#92DEDC">Seller</td>
<td style="background-color:#92DEDC">Buyer</td>
<td style="background-color:#92DEDC">Admin Tasks</td>
</tr>
<?php
$i=0;
while($i<$rows){
$recordArray=mysql_fetch_array($result);//fetch and display a record
echo "<tr bgcolor='$bgcolor'>
<td >$recordArray[StockID]</td>
<td>$recordArray[Rego_No]</td>
<td>$recordArray[Make]</td>
<td>$recordArray[Model]</td>
<td>$recordArray[Year]</td>
<td>$recordArray[Cost_Price]</td>
<td>$recordArray[employeeid]</td>
<td>$recordArray[Sale_Price]</td>
<td>$recordArray[emp_name]</td>
<td>$recordArray[Buyer]</td>
<td>
<a href='edit-car.php?StockID=$recordArray[StockID] & emp_name=$recordArray[emp_name]'>Edit</a>
</td>
</tr>";
$i++;
}else{
echo "Something went wrong. MySQL reports <b>", mysql_error(),"</b>";
}
?>
</table>
<?php
mysql_close($conn);
?>
</body></html>
Any assistance would be greatly appreciated. We are only a week into the course and I'm just trying to dress up the results a little.
mysql_numrowsshould bemysql_num_rows.. also thesemysql_*is deprecated as ofPHP 5.5.0and not recommended by php communtiy. Prefer usingPDOormysqlito avoid issues like mysql injection