Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Having a Problem with basic mySQL and PHP

    I am using some book and web resources to make this. I am pretty confused. I would really appreciate if someone could fix my errors and make some suggestions.

    I am just trying to go into my local server and reprint the line of the database. I am almost positive the connection is correct.


    PHP Code:
    <?php
    // Create connection
    $con=mysql_connect("127.0.0.1","root","","cooop");

    // Check connection
    if (mysqli_connect_errno($con))
      {
      echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
      }
     else{
        echo 
    "NICE";
        }
    echo
    "<TABLE BORDER =1>
    <TR>
    <TH>SPEED</TH>
    <TH>HEIGHT</TH>
    <TH>CALF MUSCLE GIRTH</TH>
    </TR>"
    ;
    $db mysql_select_db("cooop",$con);
    $sql "SELECT SPEED, HEIGHT, CALF MUSCLE GIRTH FROM AFRICAN_AMERICANS";
    $sql_result mysql_query($sql,$con);
    while(
    $row mysql_fetch_array($sql_result)){
    $speed $row["SPEED"];
    $height $row["HEIGHT"];
    $calf $row["CALF_MUSCLE_GIRTH"];
    echo
    "<TR>
    <TD>$speed</TD>
    <TD>$height</TD>
    <TD>$calf</TD>"
    ;

    }
    echo 
    "</TABLE>";
    ?>

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,625
    Thanks
    0
    Thanked 645 Times in 635 Posts
    You appear to have a mix of mysql_ and mysqli_ calls.

    Replace the mysql_ ones with the mysqli_ equivalents and then perhaps you will get it to work. (The mysql_ calls are going to be removed in the next version of PHP so there's no point in using them any more for new code and anywhere currently using them should be working on replacing them).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    dekkahs (07-09-2013)

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    80
    Thanks
    5
    Thanked 12 Times in 12 Posts
    You had it pretty close and this is just a suggestion, but watch how you capitalize your variables and HTML tags:

    PHP Code:
    <?php
    /**
     * Connect to mysqli database:
     */
    $mysqli = new mysqli("127.0.0.1""root""""cooop");

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

    // Query the Table in the Database:
    $query "SELECT speed, height, calfMuscleGirth FROM africanAmericans";
    $result $mysqli->query($query);

    echo 
    '<table border="0">';
    echo 
    '<tr>';
    echo 
    '<th>SPEED</th>';
    echo 
    '<th>HEIGHT</th>';
    echo 
    '<th>CALF MUSCLE GIRTH</tr>';
    echo 
    '</tr>';

    /* while loop + fetch associative array */
    while ($row $result->fetch_array(MYSQLI_ASSOC)) {
        echo 
    '<tr>';
        echo 
    '<td>' $row['speed'] . '</td>';
        echo 
    '<td>' $row['height'] . '</td>';
        echo 
    '<td>' $row['calfMuscleGirth'] . '</td>';
        echo 
    '</tr>';
    }

    echo 
    '</table>';

    /* free result set */
    $result->free();

    /* close connection */
    $mysqli->close();
    ?>
    Last edited by Strider64; 07-09-2013 at 01:21 PM.
    True courage is about knowing not when to take a life, but when to spare one. PDO Tutorial

  • Users who have thanked Strider64 for this post:

    dekkahs (07-09-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •