开发者

How do I fix this weird PHP/MySQL issue caused by if statement?

I'm a beginner to PHP/mySql. I'm writing a CMS and still in the early stage. I wrote a code to take the information from the mySql table and but it into a HTML table. The problem is , before applying a certain IF statement, the whole data in the mySql table is shown, which is what I want... But after applying that IF statement part of my data simply disappears! (the first raw to be specific) .. So here's my code that will be efficient for you to diagnose my issue .. If you needed additional pieces just tell me.

First of all, here's my HTML table BEFORE applying IF statement:

How do I fix this weird PHP/MySQL issue caused by if statement?

And here's AFTER :

How do I fix this weird PHP/MySQL issue caused by if statement?

Here's my code WITHOUT IF statement:

<?php
function make_table() {
    $myResult = get_ind_info_fam();
    $ind_field_count = mysql_num_fields($myResult) -1;

            // Table to show ALL individuals info.
            echo "<table>" ;

            // Table Headers:
            for ($i = 2 ; $i <= $ind_field_count ; $i++) {
                    $field_name = mysql_field_name($myResult, $i);
                    $field_name = ucwords($field_name);
                    if ($i ==2) {
                        $field_name = "Individual Name";
                    }
                    echo "<th>{$field_name}</th>";
    开发者_Python百科            }

            // Table Data :
                while ($myData = mysql_fetch_array($myResult)){
                    echo "<tr>";
                    for ($i = 2 ; $i <= $ind_field_count ; $i++) {
                        echo "<td>{$myData[$i]}</td>";
                    }
                    echo "</tr>";
                }
            echo "</table>" ;
  }
 ?>

And the Code WITH the IF statement :

<?php
function make_table() {   
     $myResult = get_ind_info_fam(); 
// Checking if a correct $_GET["fam"] is entered
//  (not out of range) so an empty table won't be shown :

    if (isset($myResult)) {

        if($myData = mysql_fetch_array($myResult)) {
            $ind_field_count = mysql_num_fields($myResult) -1;

            // Table to show ALL individuals info.
            echo "<table>" ;

            // Table Headers:
            for ($i = 2 ; $i <= $ind_field_count ; $i++) {
                    $field_name = mysql_field_name($myResult, $i);
                    $field_name = ucwords($field_name);
                    if ($i ==2) {
                        $field_name = "Individual Name";
                    }
                    echo "<th>{$field_name}</th>";
                }

            // Table Data :
                while ($myData = mysql_fetch_array($myResult)){
                    echo "<tr>";
                    for ($i = 2 ; $i <= $ind_field_count ; $i++) {
                        echo "<td>{$myData[$i]}</td>";
                    }
                    echo "</tr>";
                }
            echo "</table>" ;
        }
    }
   }
?>

And this is my get_ind_info_fam() function:

function get_ind_info_fam()
{
   //****Family name is clicked*****

    //This function gets Family ID from addresse bar
    //and extract individual info according to that ID by $_GET variable
    //
    //NOTE : this function is used in the article to show content of
    //       selected family name.

        if(is_numeric($_GET["fam"])) {
            $query ="SELECT * FROM individual
                        WHERE g_id={$_GET["fam"]}";
            $result = mysql_query($query);
             return $result ;
        }
}

I don't really know what exactly to tell you to get the full picture but if there is any thing that isn't clear enough let me know.


You're fetching your data rows in two places with the if() version:

if($myData = mysql_fetch_array($myResult)) {

and

while ($myData = mysql_fetch_array($myResult)){

The first row is thrown away, as you simply output the table header row without every outputting the row data.

As well, your initial isset() check will not do what you want. mysql_query() will return SOMETHING, regardless of how the query executed. If it fails, you get a boolean FALSE, otherwise you get a result set handle. That means isset() will always succeed, because that value is (big surprise) always set. What you want is:

$myResult = get_ind_info_fam(); 

if ($myResult === FALSE) {
   die(mysql_error());
}
if (mysql_num_rows($myResult) == 0) {
    echo "nothing found";
} else {
    .... print out the results ...
}


You don't have this in the one without the IF:

if($myData = mysql_fetch_array($myResult)) {

For reference, check right after your If statement.


It´s your second if statement: You are fetching the first row of the result set and you don´t do anything with it afterwards, so it disappears when you enter the while loop and get the second row.

It´s this line:

if($myData = mysql_fetch_array($myResult)) {
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜