Thread: PHP and MySQL

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9

    PHP and MySQL


    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. In 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 MySQL or Apache and MySQL. Any ideas?

    PHP Code:
    <?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 />";
    }


    ?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    722
    Rep Power
    7
    Place this before your while() loop and see if it offers a correct response.

    PHP Code:
    echo "Return row count: " mysqli_num_rows($result) . " rows"
  4. #3
  5. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,556
    Rep Power
    2338
    Alternatively, accept and embrace the fact that SQL queries can and do fail from time to time and handle this.

    php Code:
     
    $result = mysqli_query($con, "SELECT fname, lname FROM person");
    if (!$result)
    {
        echo "MySQL Error: " . mysqli_error($con) . "\n";
    }
    else
    {
        while($row = mysqli_fetch_array($result))
        {
            echo $row['fname'] . " " . $row['lname'];
            echo "<br />";
        }
    } 
    }
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!

IMN logo majestic logo threadwatch logo seochat tools logo