RobinTheHoodie
RobinTheHoodie

Reputation: 11

Displaying MySQL Data Using PHP

I have recently started programing for fun so sorry if the resolution to this problem is something simple.

I have a mysql table that has customer sales tickets stored in it. What I am trying to do is get it so that you can refine the tickets shown by a drop down list. I am able to populate the drop down list from the mysql table but when I click submit to filter out and display by the customer name selected. I get the following error:-

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\result.php on line 16

Below are the two PHP pages that i use. The first page is where the drop down is populated.

<?php
//db connection 
mysql_connect("********","********","********");
mysql_select_db("salesys");

//query
$sql=mysql_query("SELECT DISTINCT CustomerName FROM ticket ORDER BY CustomerName ASC");
if(mysql_num_rows($sql)){

$select= '<select name="select">';  
while($rs=mysql_fetch_array($sql)){
      $select.='<option value="'.$rs['CustomerName'].'">'.$rs['CustomerName'].'</option>';
  }
}

$select.='</select>';

?>  

<form name="input" action="result.php" method="POST">
<?php echo $select; ?>
<input type="submit" value="Submit">
</form>

And below is the second page.

<?php
$customerName = $_POST['select'];
echo "Showing Results For: ", $customerName;
echo "<BR>";
echo "<BR>";

$con=mysqli_connect("localhost","root","********","********");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM ticket WHERE CustomerName = $customerName");

while($row = mysqli_fetch_array($result))
  {
  echo $row['TicketID'] . " | " . $row['CustomerName'] . " | " . $row['BriefDesc'];
  echo "<br>";
    echo "<br>";
  }

mysqli_close($con);
?>

Any help would much be appreciated.

Upvotes: 0

Views: 134

Answers (2)

Asenar
Asenar

Reputation: 7010

You have to check the query is correct before while($row = mysqli_fetch_array($result)) to help you to debug your code.

You can check if $result is valid or not. See below that simple code (to use only for debug purpose, because it's not safe to display mysql errors in production )

In you case, the error is probably than $customerName is a string, and have to be surrounded by quotes

$customerName = '"'.mysqli_escape_string($con, $customerName).'"';
$result = mysqli_query($con, "SELECT * FROM ticket WHERE CustomerName = $customerName");
if (!$result)
{
  echo mysqli_error(); 
}

EDIT: in production, you should not display errors unless some required conditions validated. If your site has a login system, you can display errors only if you (and not anyone else) is logged in.

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157838

If you have recently started programing, you have to learn how to program first. Before you start with any particular language or API.

One of essential programmer's abilities is a skill in using google. It is not that hard: just copy and paste the error message you have (not only this one but every error you will get) into google search bar. You will find hundreds of people who already faced the same problem and solved them somehow. and most likely you will find an answer. It is really simple yet helpful, yet quite essential.

BTW, the only proper answer was in the comments: use prepared statements.

Upvotes: 1

Related Questions