Jump to content

MySQL = No Results - Why?


timmah1

Recommended Posts

I'm getting no results from this statement, even though the variables are being passed.

[code]
if ( $_GET["op"] == "view" ){
include "dbConfig.php";

$q=mysql_query("select * from mail where UserTo='$_SESSION[valid_user]' AND mail_id='&id'")
or die ("Cannot View This Message");
while ($row=mysql_fetch_array($q)){
echo "$row[mail_id]";
}

}

[/code]
Can someone explain to me why?

Thanks
Link to comment
https://forums.phpfreaks.com/topic/33989-mysql-no-results-why/
Share on other sites

You dont have any call to session_start(). Also, complex variables (arrays) need to be surrounded by curly braces when within quotes, variables do NOT need to be within quotes and check your queries before trying to use there results.

[code]
<?php
session_start();
if (isset($_GET['op'])) {
  if ( $_GET['op'] == 'view' ) {
    include "dbConfig.php";
    if ($result = mysql_query("select * from mail where UserTo='{$_SESSION['valid_user']}' AND mail_id = '$id'")) {
      if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_array($result)) {
          echo $row['mail_id'].'<br />';
        }
      } else {
        echo 'No results found';
      }
    } else {
      echo mysql_error();
    }
  }
}
?>

Next. Where is $id comming from? Also, if your only expecting one result you do not need a while() loop.[/code]
Link to comment
https://forums.phpfreaks.com/topic/33989-mysql-no-results-why/#findComment-159736
Share on other sites

[code]
<?php
session_start();
if (isset($_GET['op']) && isset($_GET['id'])) {
  if ( $_GET['op'] == 'view' ) {
    include "dbConfig.php";
    if ($result = mysql_query("select * from mail where UserTo='{$_SESSION['valid_user']}' AND mail_id = '{$_GET['id']}'")) {
      if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_array($result)) {
          echo $row['mail_id'].'<br />';
        }
      } else {
        echo 'No results found';
      }
    } else {
      echo mysql_error();
    }
  }
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/33989-mysql-no-results-why/#findComment-159739
Share on other sites

Actually, you might need to switch these lines around...

[code=php:0]
if (isset($_GET['op']) && isset($_GET['id'])) {
  if ( $_GET['op'] == 'view' ) {
[/code]

to...

[code=php:0]
if ($_GET['op'] == 'view')) {
  if (isset($_GET['op']) && isset($_GET['id'])) {
[/code]

should make it alittle less problematic.
Link to comment
https://forums.phpfreaks.com/topic/33989-mysql-no-results-why/#findComment-159743
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.