Jump to content

Recommended Posts

Thanks, thorpe. I now get this warning when I execute the script:

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource

 

mysql_select_db($this->db_name, $conn) or die(mysql_error());

  $results = mysql_query($sql, $conn) or die(mysql_error());

  $rows = mysql_fetch_object($results);

$sql comes from this function:

function _checkLogin($username, $password, $remember) {
  $db = new db();
  $sql = "SELECT * FROM Member WHERE username = '$username' AND password = '$password'";
  $result = $db->dbQuery($sql);
  if (is_object($result)) { 
   $this->_setSession($result, $remember);
   echo '1';
   return true; 
  } else { 
   $this->failed = true; 
   $this->_logout();
   echo '2';
   return false; 
  } 
}

 

$conn comes from the db_connect() below:

<?php
class db {
var $db_host = 'localhost'; 
var $db_user = 'root'; 
var $db_pass = ''; 
var $db_name = 'realfina_allcbreviews';

/* connects to the db */
function db_connect() { 
  $conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass) or die(mysql_error());
  return $conn;
}

/* This gets the info */
function dbQuery($sql) {
  $conn = $this->db_connect();
  if (!$conn) {
   die('Could not connect: ' . mysql_error());
  }
  mysql_select_db($this->db_name, $conn) or die(mysql_error());
  $results = mysql_query($sql, $conn) or die(mysql_error());
  $rows = mysql_fetch_object($results);
  return $rows;
}
}
?>

$sql is a local variable inside that function.  It doesn't exist outside that function unless you specify it as a global variable or else return it from whence the function was called (which I'm not seeing in your previous code calling that function in the first place).

 

edit:

 

or rather what I mean to say is, I don't really see a connection between $sql in that function and where you are trying to use it.  I mean, I see you trying to pass it as an argument to the function but I don't see it being returned or set as a global or anything in the function where it's set in the first place, so I don't really see how you are getting it from point A to point C from the presumably point B of calling the function.

That being said would you declare $sql as a global variable like this:

/* This gets the info */
function dbQuery($sql) {
  $conn = $this->db_connect();
  if (!$conn) {
   die('Could not connect: ' . mysql_error());
  }
  mysql_select_db($this->db_name, $conn) or die(mysql_error());
  $results = mysql_query($sql, $conn) or die(mysql_error());
  $rows = mysql_fetch_object($results);
  return global $rows;
}

 

or like this:

var global $sql;
function db_connect() { 
  $conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass) or die(mysql_error());
  return $conn;
}

/* This gets the info */
function dbQuery($sql) {
  $conn = $this->db_connect();
  if (!$conn) {
   die('Could not connect: ' . mysql_error());
  }
  mysql_select_db($this->db_name, $conn) or die(mysql_error());
  $results = mysql_query($sql, $conn) or die(mysql_error());
  $rows = mysql_fetch_object($results);
  return $rows;
}

No.

 

Besides, according to your code $sql is pased to your functions as an argument, there is no point in attempting to declare it as a global.

 

Thorpe if this is the case what did I not understand from Crayon Violent's response. I thought he said that I should declare $sql as global.

These snippets of code seam to be completely unrelated to each other, and your original question and follow-up. If your having understanding variable scope, read here, otherwise, in relation to....

 

Thanks, thorpe. I now get this warning when I execute the script:

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource

 

mysql_select_db($this->db_name, $conn) or die(mysql_error());

$results = mysql_query($sql, $conn) or die(mysql_error());

$rows = mysql_fetch_object($results);

 

Is that the relevent code relating to said error? And if so (assuming that is part of the dbQuery() method) can wee see where you call dbQuery()?

Hey guys thanks. Thorpe yes that the code that is related to the error, I figured out my problem, I was declaring the $db object as a null variable earlier in my script and that fixed that issue.

 

The other problem is that I can't get the dbQuery() method to return the results as an object. Basically it seems like its not returning any info. I am calling the dbQuery() method from the User class and the dbQuery() is located in the db class. I am of course including the file that holds the db class in the file that holds the User class.

Basically it seems like its not returning any info

 

Well, you never check. Also, even if it did return a result it would only ever return the first one. If you are expecting more than one record you would need to call mysql_fetch_object() again.

 

I'm not sure where you want to go from here so I'll just leave you to ponder for a bit. Are you expecting more than one result? If so, do you want dbQuery() to return an array of row objects?

Actually I check using print_r() I just didn't add it in the code snippet. When I do, nothing is printed to the screen.

 

Also I am expecting just one result.

 

do you want dbQuery() to return an array of row objects?

 

The answer is no, but it would be good to know how to do for the future.

If your only ever expecting one result, then you could simply use....

 

function dbQuery($sql) {
  $conn = $this->db_connect();
  if (!$conn) {
   die('Could not connect: ' . mysql_error());
  }
  mysql_select_db($this->db_name, $conn) or die(mysql_error());
  if ($result = mysql_query($sql, $conn)) {
    if (mysql_num_rows($result)) {
      return mysql_fetch_object($result);
    }
  }
  return false;
}

 

For multiple results, you would be best to store the result within a property within the same class (lets call it result), you would then create another method to access this property.

 

function dbQuery($sql) {
  $conn = $this->db_connect();
  if (!$conn) {
   die('Could not connect: ' . mysql_error());
  }
  mysql_select_db($this->db_name, $conn) or die(mysql_error());
  if ($result = mysql_query($sql, $conn)) {
    if (mysql_num_rows($result)) {
      $this->result = $result;
    }
  }
  return false;
}

function dbResult() {
  return $this->result;
}

 

Then, to use this class would be something like....

 

$db = new db;
if ($db->dbQuery("SELECT foo FROM bar")) {
  while ($row = $db->dbResult()) {
    echo $row->foo;
  }
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.