2

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.

2
  • var_dump($resultset); gives you what? Also if just starting out you should use PDO or mysqli. Commented Apr 9, 2017 at 22:18
  • Don't forget to select which answer was the correct one (if one of them was correct) to help out other users with similar issues. If non of the answers were correct, please tell us how you solved it. Commented Apr 9, 2017 at 22:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I wasn't aware that they were case sensitive and the table is actually "customer" in lower case so I have fixed that. Thanks! That said it didn't seem to fix my issue.
1

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

2 Comments

Thanks for the feedback, I had actually been using mysqli_ functions, but changed this up following a tutorial and never adjusted them. I will certainly continue that going forward. I've updated the code referenced and it looks like it put me in the right direction. I'm now getting this error " Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\report.php on line 19 Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\report.php on line 20 No results." I'm confused as the table has 20 separate entries and 5 fields.
Hi, ok so if you have switched to mysqli_ functions then you have to read the documentations. Because mysqli_query expects the first parameter to be the mysqli link. So you have to type mysqli_query($link, "SELECT * FROM Customer"); but then you have to make sure you don't have any mysql_ functions left, only mysqli_

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.