...

View Full Version : connection problem with connection class



dizyn
11-11-2010, 09:17 PM
I am having issues with connect
I see following errors:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/myfolder/public_html/myfolder/config.php on line 58

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/myfolder/public_html/myfolder/config.php on line 45

here is my code:



<?php

// site info
define('HTTP_PATH', 'http://go/twi.tter.me/');
define('URL_SHFT', 1);
define('FS_PATH', dirname(__FILE__) . '/');

// database info
define('DB_PATH', 'localhost');
define('DB_USER', 'login');
define('DB_PASS', 'pass');
define('DB_NAME', 'db');


// database class

class MySqlStuff {

public $connection = null;
public $database = null;

public function __construct() {

// $this->connection = mysql_connect(DB_PATH, DB_USER, DB_PASS);
// $this->database = mysql_select_db(DB_NAME);
$this->connection = mysql_connect("localhost", "login", "pass");
$this->database = mysql_select_db("mydb");

return true;

}

public function Query($sql) {

$result = mysql_query($sql);
return $result;

}

public function FetchArray($sql) {

$result = $this->Query($sql);
$array = array();

while($row = mysql_fetch_assoc($result)) {

$array[] = $row;

}

return $array;

}

public function FetchSArray($sql) {

$result = $this->Query($sql);
$array = mysql_fetch_assoc($result);

return $array;

}

public function InsertID() {

return mysql_insert_id();

}

public function NumberRows($sql) {

$result = $this->Query($sql);
return mysql_num_rows($result);

}

}

// connect to database
$mysqlstuff = new MySqlStuff();

?>

poyzn
11-11-2010, 11:16 PM
it's not errors, it's warnings, you can switch them off with error_reporting
you've commented right db connection strings in construct function
try to replace your construct function with next:


public function __construct() {

mysql_connect(DB_PATH, DB_USER, DB_PASS);
mysql_select_db(DB_NAME);

}

or may be you just have empty result for your request

dniwebdesign
11-12-2010, 03:00 AM
It is basically telling you that the $result isn't valid.... double check that you are sending the FetchSArray a valid MySQL statement and that the MySQL statment produces no errors.

public function Query($sql) {
$result = mysql_query($sql) or $error = mysql_error();
if($result) {
return $result;
}
else {
echo '<strong>MySQL Error: </strong> '.$error;
return false;
}
}
The above addition to your code will check if the $result is valid and if not will display an on screen message of what the MySQL error is.

Dormilich
11-12-2010, 08:52 AM
if you want to use classes, think about using MySQLi (http://php.net/mysqli) or PDO (http://php.net/pdo). those are already class based and are not outdated like the mysql_* functions.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum