Jump to content

Filling PHP Table


h0m3r0w
 Share

Go to solution Solved by jcbones,

Recommended Posts

Hello all,

 

I'm having a bit of a problem with my code here.

I'm trying to allow the user to enter a number (an office number) from a database, and based on that value, after submitting it, an html table will fill with the appropriate results (lastName, firstName, jobTitle). For whatever reason, though, I keep getting this error:

 

"Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in... <website name> <line number>"

 

It's very weird, because I'm doing this straight from notes / w3 and I'm still getting errors. I'm still new to this, so I'm sorry if I'm sounding completely inexperienced... well I am haha.

 

Anyway, here's the code:

<!DOCTYPE html >
<html lang ="en">

<head> <title> Homepage </title> </head>

<body bgcolor="gray">

<h1 align=middle> Welcome to the Site </h1>

<?php
DEFINE ('DB_USER', '*******);
DEFINE ('DB_PASSWORD', '******');
DEFINE ('DB_HOST','*****');
DEFINE ('DB_NAME','******');

$dbc = @mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die ('Could not connect to MySQL: '.mysqli_connect_error($dbc) );

mysqli_set_charset($dbc, 'utf8');

//$r = @mysqli_query($dbc, $q);

?>
<form action ="Homepage.php" method="get">
Enter Office Code:
<input type="text" name="oCode" id="OfficeCode" maxlength="15">
<input type="submit" name="formSubmit" value="Submit">
</form>
<?

$code = $_GET['oCode'];

$result = mysqli_query($dbc, "select lastName, firstName, jobTitle from Employees where OfficeCode='$code'");

echo "<table border='1'>";
echo "<tr>";
echo "<th>LastName</th>";
echo "<th>FirstName</th>";
echo "<th>Job Title</th>";
echo "</tr>";

while ($row = mysqli_fetch_array($result)) //this is where I'm getting the error
{
        echo "<tr>";
        echo "<td>" . $row['lastName'] . "</td>";
        echo "<td>" . $row['firstName']. "</td>";
        echo "<td>" . $row['jobTitle'] . "</td>";
        echo "</tr>";
}
echo "</table>";

mysqli_close($dbc);
?>


Any help would be great. Thank you!

Edited by h0m3r0w
Link to comment
Share on other sites

I think you have an error in your query. Try using mysqli_error() to find out what it is:

$q = "select lastName, firstName, jobTitle from Employees where OfficeCode='$code'"
if (!($result = mysqli_query($dbc, $q)))
    error_log(mysqli_error($dbc)."<br/>\nQuery: ".$q);
Link to comment
Share on other sites

I think I see the problem or part of the problem. It is telling you that the first parameter of your mysqli_query argument is incorrect, so the $dbc is not working. Look at this...

DEFINE ('DB_USER', '*******);
DEFINE ('DB_PASSWORD', '******');
DEFINE ('DB_HOST','*****');
DEFINE ('DB_NAME','******');

Your DEFINE DB_USER is missing a ' at the end of the ********.

 

See if that works!

Edited by computermax2328
Link to comment
Share on other sites

 

I think you have an error in your query. Try using mysqli_error() to find out what it is:

$q = "select lastName, firstName, jobTitle from Employees where OfficeCode='$code'"
if (!($result = mysqli_query($dbc, $q)))
    error_log(mysqli_error($dbc)."<br/>\nQuery: ".$q);

 

Here, I noticed that I'm a complete idiot. I forgot to select a specific database... When I connect to mySQL, I have a list of databases. While the one I hidden was indeed a database, it wasn't the correct one... So, now that I have that done, I'm still getting the exact same error for some reason...

 

I think I see the problem or part of the problem. It is telling you that the first parameter of your mysqli_query argument is incorrect, so the $dbc is not working. Look at this...

DEFINE ('DB_USER', '*******);
DEFINE ('DB_PASSWORD', '******');
DEFINE ('DB_HOST','*****');
DEFINE ('DB_NAME','******');

Your DEFINE DB_USER is missing a ' at the end of the ********.

 

See if that works!

 

Haha, sorry but that was a typo on my part. There actually was another single quote, I just made an error while trying to conceal some of the information. I'm no good at PHP, so I'm unsure as to how much code I should be revealing, since I'm connecting to my network and all that.

 

But all in all, I made a dumb error in connecting to the wrong database. But even with the correct database selected, I get an error. Well, I BELIEVE that I'm connected to the database. Do I need to use a "use ********" statement in the php? Where the **** is the name of one of the databases ("classicmodels") that I need to connect to?

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.