Reputation: 21
My apologies if this is redundant, I searched related topics, but could not find an answer in any of them. I am looking to run SQL queries against a database I have created and display the results on an HTML button click. I have started with a simple scenario where I am just looking select all entities from my "customer" table. I have created the following PHP code to accomplish this:
<?php
define('DB_NAME', 'mis630db');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$resultset = mysql_query("SELECT * FROM Customer");
if($resultset->num_rows !=0) {
while($rows = $resultset->fetch_assoc())
{
$idCustomer = $rows['idCustomer'];
$CustomerFName = $rows['CustomerFName'];
$CustomerLName = $rows['CustomerLName'];
$CustomerAddress = $rows['CustomerAddress'];
$CustomerPhone = $rows['CustomerPhone'];
$CustomerEmail = $rows['CustomerEmail'];
echo"<p> Customer ID: $idCustomer<br />Customer Name: $CustomerFName $CustomerLName<br />Customer Address: $CustomerAddress<br />Customer Phone: $CustomerPhone<br />Customer Email: $CustomerEmail</p>";
}
}
else{
echo "No results.";
}
?>
When I test that I receive this error:
" Notice: Trying to get property of non-object in C:\xampp\htdocs\report.php on line 20 No results."
I'm not sure why as it should be returning multiple results. Once that is sorted out I want to connect this to an HTML button to display the results, where I would have a few buttons that run various SQL queries I have created :
<html>
<head>
<title> Reports</title>
</head>
<body>
<div class="header">
<h1>Reports</h1>
</div>
<form action="report.php" method="post" />
<table>
<tr>
<td><input type="submit" value="Customers" /><td>
</tr>
</form>
</body>
</html>
Any insight would be much appreciated.
Thanks.
Upvotes: 2
Views: 1622
Reputation: 1267
Change this line:
if($resultset->num_rows !=0) {
to
if (mysql_num_rows($resultset) != 0) {
Because I don't think you can access a method called num_rows
directly from a mysql result object.
And change this line:
while($rows = $resultset->fetch_assoc())
to
while($rows = mysql_fetch_assoc($resultset))
Because I don't think you can access a method called fetch_assoc
directly from a mysql result object.
And over to another thing. Mysql_
functions are deprecated I would advice you to start using mysqli_
functions instead. http://php.net/manual/en/book.mysqli.php
Upvotes: 1
Reputation: 1497
Looks like the SQL query SELECT * FROM Customer
is failing and thus $resultset
is not an object --it probably is null
.
SQL tables, columns, etc are case sensitive. Are you sure your table is named Customer
and not customer
?
Please, note that the extension mysql
is deprecated since PHP 5.5 and has been removed in PHP 7. You should be using mysqli or PDO-MySQL instead.
Upvotes: 1