Reputation: 33
I have a page that takes an SKU from a database and creates a page. Example URL: http://example.com/index.php?sku=1234567
When I load a URL like this, it shows nothing - not even the table which I output with echo
. Below is my code:
$sku = $_GET['sku'];
$result = mysqli_query($conn, "SELECT productname, price, producturl, productimg, productdesc, sku FROM table WHERE sku=" . $sku);
while ($row = mysqli_fetch_array($result)) {
echo '<h3>test</h3>';
echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><h4>'.$row["sku"].'</h4></td>
<td><h3>'.$row["productname"].'</h3></td>
<td rowspan="2"><img src="'.$row["productimg"].'" width="100%" alt="productimg"/></td>
</tr>
<tr>
<td colspan="2" rowspan="2"><p>'.$row["productdesc"].'</p></td>
</tr>
<tr>
<td><a class="button" href="'.$row["producturl"].'">View Product</a> <a class="alert button" href="">No Match</a> <a class="alert button" href="">Match</a></td>
</tr>
</table>';
}
I have connected to my database and have the <?php
and ?>
tags in there. I have noticed while playing around with it that if I remove this line:
while ($row = mysqli_fetch_array($result)) {
and also remove the closing }
, it works but does not display any data - just the table. I am not sure what is going on here.
Upvotes: 0
Views: 72
Reputation: 33
I have managed to solve the issues that i have been having i had to remove the i from mysqli, but i have used the same piece of code on another site so it may be something to do with the server or database maybe. here is the code though'
<?php
$sku = $_GET['sku'];
$objConnect = mysql_connect("host address","username","password") or die(mysql_error() . 'this is true death...');
$objDB = mysql_select_db("database");
$result = 'SELECT sku, productname, price, producturl, productimg, productdesc FROM table1 WHERE sku="' . $sku . '"';
$result = mysql_query($result);
while ($row = mysql_fetch_array($result)) {
echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><h4>'.$row["sku"].'</h4></td>
<td><h3>'.$row["productname"].'</h3></td>
<td rowspan="2" width="30%"><img src="'.$row["productimg"].'" width="100%" alt="productimg"/></td>
</tr>
<tr>
<td colspan="2" rowspan="2"><p>'.$row["productdesc"].'</p></td>
</tr>
<tr>
<td><a class="button" href="'.$row["producturl"].'">View Product</a> <a class="alert button" href="">No Match</a> <a class="alert button" href="">Match</a></td>
</tr>
</table>';
}
?>
Upvotes: 0
Reputation: 2378
Put $sku inside quotes.
<?php
$sku = $_GET['sku'];
$result = mysqli_query($conn, "SELECT productname, price, producturl, productimg, productdesc, sku FROM table WHERE sku = $sku");
while ($row = mysqli_fetch_array($result)) {
echo '<h3>test</h3>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><h4>'.$row["sku"].'</h4></td>
<td><h3>'.$row["productname"].'</h3></td>
<td rowspan="2"><img src="'.$row["productimg"].'" width="100%" alt="productimg"/></td>
</tr>
<tr>
<td colspan="2" rowspan="2"><p>'.$row["productdesc"].'</p></td>
</tr>
<tr>
<td><a class="button" href="'.$row["producturl"].'">View Product</a> <a class="alert button" href="">No Match</a> <a class="alert button" href="">Match</a></td>
</tr>
</table>';
}
?>
Upvotes: 0
Reputation: 33501
Simple. your mysqli_query
call returns no records. Either no records are found, or there is an error. Make your code a little more robust.
$sku = $_GET['sku'];
if ($result = mysqli_query($conn, ...)) {
if (mysqli_num_rows($result) == 0) {
echo "no skus found";
} else {
while ($row = mysqli_fetch_array($result)) {
echo '<h3>test</h3>';
...
}
}
} else {
echo "something went wrong: ".mysqli_error();
}
(As a side note, please use parametrised queries, you are opening yourself up to SQL injection now. MySQLi is no magic bullet against this, you still have to validate / sanitize input.)
Upvotes: 2
Reputation: 4875
Display mysqli error on fault:
if (!mysqli_query($link, $sql)) {
printf("Errormessage: %s\n", mysqli_error($link));
}
Upvotes: 0