简体   繁体   中英

Query not showing results

I am doing this query. It doesn't show me the result which in $products.

code:

$sql = "SELECT s*
     FROM Client_agent_approvals caa
     INNER JOIN Subscriptions s ON s.client_id = caa.client_id
     WHERE caa.agent_id = '$id'
     AND s.agent_id <> '$id'";
$result = mysql_query($sql);

$query = mysql_query($sql) or die("Error: " . mysql_error());

if ($result == "") {
    echo "";
}
echo "";


$rows = mysql_num_rows($result);

if ($rows == 0) {
    print("");
} elseif ($rows > 0) {
    while ($row = mysql_fetch_array($query)) {

        $product = $row['product'];

        print("Products they have are: $product");
    }
}

You have s* . I think you mean s.* . You should also use do... while for query iteration:

 $row = mysql_fetch_array($query);

 if(!$row) 
 {
     print('no rows');// a more useful message
 }
 else
 {
    do {

        $product = $row['product'];

         print("Products they have are: $product");
     }
     while($row = mysql_fetch_array($query));

 }

mysql_num_rows actually counts all of the values returned, this does not. You can almost say that it is doing twice the work for the same result.

Of course, all of this assumes that there were one or more rows returned to begin with. Can you confirm that you should be getting something by running this in MySQL directly?

if you have edited your code after the comment that was just posted, that was not what was meant by "indentation". you can indent the statements inside loop's and if's to make it more readable.

try using print_r on $rows and $row before doing anything with them. it may help you see what is going on with those variables. http://php.net/manual/en/function.print-r.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM