Jump to content

Recommended Posts

I've recently started learning OOP, and I have a rather simple problem, yet I don't know how to solve it. Obviously I'm using a certain aspect of OOP wrong:

 

<?php

include "connect.php";

//All classes placed and called from here

class database {

public function query($x) {

$query = mysql_query($x);
print_r($query);
$result = mysql_fetch_array($query);

}

}

//Test above query
$database = new database();
$database->query("select * from `users` where `username` = 'admin'");
print $result[0];
print $result[1];

?>

 

It gives me an sql error regarding line 13, so it seems it's not parsing the sql query I'm using the $database->query().

 

Thanks for any help in advance.

Link to comment
https://forums.phpfreaks.com/topic/85234-solved-sql-error-with-classesfunctions/
Share on other sites

Without seeing the error message I can only guess the problem:

 

mysql_query  ( string $query  [' date= resource $link_identifier  ] )

 

link_identifier

 

    The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level warning is generated.

 

You need to pass the link_identifier (from $link = mysql_connect()) to your class.

Its basically a scope issue, your database class isn't looking outside of itself for the link_identifier

Ah I see. So does this mean I'm going to have place a new DB connect in the class? Wouldn't this mean every time I run the class, it will start a new DB connection?

 

This is my error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in .../classes.php on line 13

I would place everything inside your class:

 

<?php
#Get rid of this and move it into class
#include "connect.php";

//All classes placed and called from here

class database {

var $cnx;

public function database ( $host, $user, $pass, $db ) {
  //Run your connect info here
  $this->cnx = mysql_connect($host,$user,$pass);
  mysql_select_db($db,$this->cnx);

}

public function query($x) {

$query = mysql_query($x,$this->cnx);
print_r($query);
$result = mysql_fetch_array($query);
return $result;
}

}

//Test above query
$database = new database('hostname','user','password','database');
$result = $database->query("select * from `users` where `username` = 'admin'");
print $result[0];
print $result[1];

?>

in your connect.php:

 

$CNX = mysql_connect(...);

 

in your class:

 

public function database ( ) {
  global $CNX;
  $this->cnx = $CNX;
}

 

BUT...the better way is to keep it like i posted originally, and initiate the class once. So, I would put this in your connect.php:

 

$DB= new database('hostname','user','password','database');

 

and then in your script, use the same $DB object over and over:

 

print_r($DB->query("select * from `users` where `username` = 'admin'"));
print_r($DB->query("select * from `users` where `username` != 'admin'"));

OK I've implemented the code as you've shown, yet I still get an SQL resource error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in .../connect.php on line 31

 

Connect.php

<?php

//Class db connection
class database {

var $cnx;

public function database ( $host, $user, $pass, $db ) {
  //Run connect info here
  $this->cnx = mysql_connect($host,$user,$pass);
  mysql_select_db($db,$this->cnx);
}

public function query($x) {

$query = mysql_query($x,$this->cnx);
$result = mysql_fetch_array($query); //This is line 31
return $result;
}

}

$db = new database('localhost','***','***','***');

?>

 

The script using the class above:

<?php

include "connect.php";

//All classes placed and called from here

//Test query function in database class
print_r($db->query("select * from `users` where `username` = 'admin'"));

?>

The connection resource argument is optional in most mysql functions, leaving it out is a simpler option (unless your planning on using multiple database servers) as the mysql_* functions are smart enough to simply use the current open connection.

 

The problem you are experiencing happens when your query has failed, since your function has no error handling at all. Your function should look more like....

 

<?php

public function query($sql) {

  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      return mysql_fetch_assoc($result);
    }
  }
  return false;
}

?>

 

However, having shown you that. This function is only usefull for retrieving a single record.

OK when I implement error handling it gives me this very strange error:

 

Error: Table 'civ_table.users' doesn't exist

 

I don't have a clue where the .users has come from :/

print_r($db->query("select * from `users` where `username` = 'admin'"));

 

If your database name is civ_table (weird name for a database though), then it all makes sense.

OK Here's the code:

 

<?php
#Connect.php

//Class db connection
class database {

var $cnx;

public function database ( $host, $user, $pass, $db ) {
  //Run connect info here
  $this->cnx = mysql_connect($host,$user,$pass);
  mysql_select_db($db,$this->cnx);
}

public function query($x) {

$query = mysql_query($x,$this->cnx) or die('SQL: '.$x.'<br />Error: '.mysql_error());
$result = mysql_fetch_array($query);
return $result;
}

}

$db = new database('localhost','civ_admin','password','civ_db');
?>

 

<?php
#Calling code - classes.php

include "connect.php";

//All classes placed and called from here

//Test query function in database class
print_r($db);
print_r($db->query("select * from `users` where `username` = 'admin'"));

?>

 

The DB structure is nothing special. It's meant to call the table `users` within the `civ_db` database, and retrieve the row where `user` = `admin`.

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.