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

    Join Date
    Jul 2014
    Posts
    24
    Rep Power
    0

    Problems returning results from MySQL query


    Good Afternoon All,

    I'm hoping you can help, I have the following code:

    Code:
         <?php
            mysql_connect('localhost', 'root', 'StV1ncent!') or die(mysql_error());
            mysql_select_db("mysql_nfsdb") or die(mysql_error());
    
            $UID = mysql_real_escape_string($_POST["ID_Number"]);
            
            //$UID = (int)$_GET['ID_Number'];
            $query = "SELECT * FROM tbl_clients WHERE ID_Number =" . $UID . "";
            echo $query;
            echo "<br>";
            mysql_query($query) or die(mysql_error());
            //echo $query;
    
            if(mysql_num_rows($query)>=1){
        while($row = mysql_fetch_array($query)) {
            $IDNumber = $row['ID_Number'];
            $title = $row['Title'];
            $forename = $row['First Name'];
            $surname = $row['Surname'];
            $address1 = $row['Address 1'];
            $address2 = $row['Address 2'];
            $address3 = $row['Address 3'];
            $address4 = $row['Address 4'];
            $county = $row['County'];
            $postcode = $row['Postcode'];
            $company = $row['Company'];
            $phonenumber = $row['Phone Number'];
            $emailaddress = $row['E-Mail Address 1'];
            $notes = $row['Notes'];
        }
    ?>
            <br>
            <form action="updateclient.php" method="post">
    <input type="hidden" name="id" value="<?=$UID;?>">
    Title: <input type="text" name="ud_title" value="<?=$title?>"><br>
    Forename: <input type="text" name="ud_forename" value="<?=$forename?>"><br>
    Surname: <input type="text" name="ud_surname" value="<?=$surname?>"><br>
    Address 1: <input type="text" name="ud_address1" value="<?=$address1?>"><br>
    Address 2: <input type="text" name="ud_address2" value="<?=$address2?>"><br>
    Address 3: <input type="text" name="ud_address3" value="<?=$address3?>"><br>
    Address 4: <input type="text" name="ud_address4" value="<?=$address4?>"><br>
    County: <input type="text" name="ud_county" value="<?=$county?>"><br>
    Post Code: <input type="text" name="ud_postcode" value="<?=$postcode?>"><br>
    Company: <input type="text" name="ud_company" value="<?=$company?>"><br>
    Phone Number: <input type="text" name="ud_phonenumber" value="<?=$phonenumber?>"><br>
    Email Address: <input type="text" name="ud_emailaddress" value="<?=$emailaddress?>"><br>
    Notes: <input type="text" name="ud_notes" value="<?=$notes?>"><br>
    <input type="Submit">
    </form>
    <?php
    }else{
        echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
    }
            ?>
    And am finding that whatever options are chosen in the supplying drop-down list, It's dropping through to the "No Entry Found" exit clause.

    However, the echo of the $query variable produces:
    Code:
    SELECT * FROM tbl_clients WHERE ID_Number =125
    Which when I run that exactly into phpmyadmin, it works perfectly and returns the result.

    Any help would be appreciated as to why the PHP script isn't finding the result.

    Many Thanks
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,780
    Rep Power
    4536
    Warning: This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
    Your problem is that you're not saving the result from mysql_query, but you shouldn't be using this method in the first place.
    -- Cigars, whiskey and wild, wild women. --
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    92
    Rep Power
    5
    Having your development system setup with php's error_reporting set to E_ALL and display_errors set to ON, would have helped you find the problem with the result from the query/variables in your code (there would have been an error at the num rows statement about an incorrect parameter value.)

    When you switch to use either the php PDO extension (the best choice) or the php mysqli extension (not so good as it has a lot of inconsistencies and is harder to use), you need to use prepared queries when supplying values to sql query statements (you can research in the php.net documentation or on the web what prepared queries are) and you need to use exceptions to handle the database statement errors. Doing these things will actually simplify the sql query syntax and reduce the amount of php code you have to write.

    You also shouldn't write code that has line after line that's just copying variables to other variables. Just use the original variables. If you have a query that will at most match a single row, don't use a loop to retrieve the data. Just execute one fetch statement.
    Last edited by DSmabismad; August 24th, 2017 at 08:51 AM.

IMN logo majestic logo threadwatch logo seochat tools logo