Note that this is on a Linux machine.
I'M just starting to learn PHP and MySQL. I wrote the following php program that executes from an html form. Note that at this point the date from the html form is not being used to query the MySQL database. At this point I'M simply using PHP to display the data entered into the html form to validate that the PHP script is getting the variables from the form, that part is working fine. The second part of the PHP script I'M trying to display a few rows from a simple MySQL database that I've just created. I don't know if my code is bade or if there is something else I need to do in order to link PHP and or Apache to MySQL. Any ideas?

<?php

// get variable from html form
$fName = $_POST['fName'];
$lName = $_POST['lName'];
$phoneNumber = $_POST['phoneNumber'];
$address = $_POST['address'];
$zip = $_POST['zip'];
$state = $_POST['state'];

echo $fName." ";
echo $lName."</p>";
echo $phoneNumber."</p>";
echo $address."</p>";
echo $zip."</p>";
echo $state."</p>";

// Create connection
$con=mysqli_connect("localhost","lrngsql","xyz","bank");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con, "SELECT fname, lname FROM person");

while($row = mysqli_fetch_array($result))
{
    echo $row['fname'] . " " . $row['lname'];
    echo "<br />";
}


?>

Recommended Answers

All 14 Replies

Does it come back blank? with an error?

Throw this in your loop and see if that shows anything.

echo "<pre>";
print_r($row);
echo "</pre>";

Are you getting any error?Please write the error also what you are getting.

You must add die to check if the statement executed and where exactly is the error.Instead of

 $result = mysqli_query($con, "SELECT fname, lname FROM person");

Use it

  $result = mysqli_query($con, "SELECT fname, lname FROM person")
    or die("query error");

Similarly at other mysql methods.

There was no error on the page to start with. Everything that I enter into the html form is displaying on the PHP page just fine. But nothing is happening with the MySQL partion of the PHP script which is below the PHP code that displays the data entered form the form.

Pixelsoul, I add the tree lines but it didn't change anything, the page is still only displaying data fro the form and nothing from my MySQL database.

IIM or LLM, the or di function is causing the page to be soldd white displaying nothing at all, not even the data from the form that's been displaying just fine.

It's actually:

$result = mysqli_query($con, "SELECT fname, lname FROM person")or die(mysqli_error($con));

And you might throw this right under your query just to see if it comes back with any rows affected:

$row_cnt = mysqli_num_rows($result);
printf("Result set has %d rows.\n", $row_cnt);

I just made all the changes and there is still nothing showing up from mysql. The PHP above is still dispalying the html form variables just fine. Below is my html form so you can see what's activating the script. Also, is there somthing else I might need to do in order to link PHP and MySQL or Apache and MySQL?

<html>
<head>
<title>Test</title>
</head>

<body>
<form action="bail.php" method="POST">
<form>
<table width="25%">

<tr> <td>First Name:</td> 
     <td align="right"> <input type="text" name="fName"></td></tr>

<tr> <td>Last Name:</td>
     <td align="right"> <input type="text" name="lName"></td></tr>

<tr> <td>Phone Number:</td>
     <td align="right"> <input type="text" name="phoneNumber"</td></tr>

<tr> <td>Address:</td>
     <td align="right"> <input type="text" name="address"</td></tr>

<tr> <td>Zip code:</td>
     <td align="right"> <input name="zip"></td></tr>

<tr> <td>Sate: <select name="sate">

    <option value="NONE"> Select</option>
    <option value="AL"> AL - Alabama</option>
    <option value="AK"> AK - Alaska</option>
    <option value="AZ"> AZ - Arizona</option>
    <option value="AR"> AR - Arkansas</option>
    <option value="CA"> CA - Califonria</option>
    <option value="CO"> CO - Colorado</option>
    <option value="CT"> CT - Connecticut</option>
    <option value="DE"> DE - Delaware</option>
    <option value="FL"> FL - Florida</option>
    <option value="GA"> GA - Georgia</option>
    <option value="HI"> HI - Hawaii</option>
    <option value="ID"> ID - Idaho</option>
    <option value="IL"> IL - Illinois</option>
    <option value="IN"> IN - Indiana</option>
    <option value="IA"> IA - Iowa</option>
    <option value="KS"> KS - Kansas</option>
    <option value="KY"> KY - Kentucky</option>
    <option value="LA"> LA - Louisiana</option>
    <option value="ME"> ME - Maine</option>
    <option value="MD"> MD - Maryland</option>
    <option value="MA"> MA - Massachusetts</option>
    <option value="MI"> MI - Michigan</option>
    <option value="MN"> MN - Minnesota</option>
    <option value="MS"> MS - Mississippi</option>
    <option value="MO"> MO - Missouri</option>
    <option value="MT"> MT - Montana</option>
    <option value="NE"> NE - Nebraska</option>
    <option value="NV"> NV - Nevada</option>
    <option value="NH"> NH - New Hampshire</option>
    <option value="NJ"> NJ - New Jersey</option>
    <option value="NM"> NM - New Mexico</option>
    <option value="NY"> NY - New York</option>
    <option value="NC"> NC - North Carolina</option>
    <option value="ND"> ND - North Dakota</option>
    <option value="OH"> OH - Ohio</option>
    <option value="OK"> OK - Oklahoma</option>
    <option value="OR"> OR - Oregon</option>
    <option value="PA"> PA - Pennsylvania</option>
    <option value="RI"> RI - Rhode Island</option>
    <option value="SC"> SC - South Carolina</option>
    <option value="SD"> SD - South Dakota</option>
    <option value="TN"> TN - Tennessee</option>
    <option value="TX"> TX - Texas</option>
    <option value="UT"> UT - Utah</option>
    <option value="VT"> VT - Vermont</option>
    <option value="VA"> VA - Virginia</option>
    <option value="WA"> WA - Washington</option>
    <option value="WV"> WV - West Virginia</option>
    <option value="WI"> WI - Wisconsin</option>
    <option value="WY"> WY - Wyoming</option>
    </select></td><td><input type="submit"></td></tr>
</form>
</table>
</body>
</html>

I tested your code with out any issues at all (yes I went though the trouble setting up a table).
You are positive that there is actually data in your database table, right?

Do you have phpmyadmin or something else installed? Run the query against the database and see what results get returned.

SELECT fname, lname FROM person

mysql -u lrngsql -p

Above is the command I typed to log into MySQL. Entered my password 'xyz' then I typed use bank;

Whichin MySQL I typed 'select fname from person' and itreturned two names, William & Susan, so the database is there and it is populated. Could there be something else I need to do in order to link apache to MySQL, or PHP to MySQL?

Well since I am not finding any problems with in your code, I can only assume there is something going on environmentally with your server setup.

-Is PHP and MySQL installed on the same server?
-What version of PHP are you running?
-What version MySQL are you running?
-Check the php info and make sure support for mysqli is enabled on the PHP install.

-Set your PHP error reporting to all
Stick these lines in the top of your code to make sure that all errors are being reported.

 error_reporting(E_ALL);
 ini_set("display_errors", 1);

Also, you could try running something like below to see if it returns information about the installation on the server. Just for a conifmation that it is actually making a connection. Just change the server connection info.

<?php
$link = mysqli_connect("localhost", "my_user", "my_password");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* print server version */
printf("Server version: %s\n", mysqli_get_server_info($link));

/* close connection */
mysqli_close($link);
?>

Just to throw in my dime's worth, I noticed you have an extra <form> opening tag on line 8 of the above code, that will negate the line 7 tag with the correct action and method in, won't it?

I would think that it could possibly but it appears that it uses the first open <form> tag on the page because it sounds like it is submitting and passing the data from the form fields correctly.

be sure to include:

error_reporting(E_ALL);

at the top of your page to ensure error reporting is turned on.

also, i notice in your HTML for the Phone Number and E-Mail Address that the input boxes are not closed.

      <tr> <td>Phone Number:</td>
        <td align="right"> <input type="text" name="phoneNumber"</td></tr>
        <tr> <td>Address:</td>
        <td align="right"> <input type="text" name="address"</td></tr>

should be

<tr> <td>Phone Number:</td>
<td align="right"> <input type="text" name="phoneNumber"></td></tr>
<tr> <td>Address:</td>
<td align="right"> <input type="text" name="address"></td></tr>

The problem was here:

echo $fName." ";
echo $lName."</p>";
echo $phoneNumber."</p>";
echo $address."</p>";
echo $zip."</p>";
echo $state."</p>";

All I had to do was change all the "</p>"s to "<br />", then the MySQL query started working just fine.

Well, that makes sense. I was testing your database query code. Not some code echoing out on the page. All though, I still don't know how those echo statements would affect the query.

Glad you got it working none the less.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.