Jump to content

Classes and sql queries not working.


Gazan

Recommended Posts

Hey there.

 

I can't make my script work.. I'm using the db class for connecting, and the query class for doing the sql queries. but when i output the code below, it returns:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\test.php on line 20

 

However, if i do the sql queries without using classes it works perfect, of course.

 

Heres my source (Notice: It has nothing to do with the missing password in the connect, 'cause i don't use pass for my offline server..):

 

<?php

class db {

function db_connect() {
	mysql_connect("localhost", "root");
	mysql_select_db("underskrift");
	}
}

class query {

function sql_query($sql) {
	global $db;
	$db->db_connect();
	mysql_query($sql) or die(mysql_error());
	}

function sql_row($result) {
	mysql_fetch_array($result);
	}
}

$db = new db;
$query = new query;

$sql = 'SELECT * FROM underskrifter';
$result = $query->sql_query($sql);

while($row = $query->sql_row($result)) {

echo $row['underskrift_navn'];

}

?>

Link to comment
https://forums.phpfreaks.com/topic/143615-classes-and-sql-queries-not-working/
Share on other sites

Even though you don't use a password you should still leave it in blank;

 

mysql_connect("localhost", "root", "");

 

try this see what happens;

 

<?php

class db {
   
   function db_connect() {
      mysql_connect("localhost", "root", "") or die(mysql_error());
      mysql_select_db("underskrift") or die(mysql_erro());
      }
}

class query {
   
   function sql_query($sql, $db) {
      $db->db_connect();
      mysql_query($sql) or die(mysql_error());
      }
   
   function sql_row($result) {
      mysql_fetch_array($result);
      }
}

$db = new db;
$query = new query;

$sql = 'SELECT * FROM underskrifter';
$result = $query->sql_query($sql, $db);

while($row = $query->sql_row($result)) {
   
   echo $row['underskrift_navn'];
   
}

?>

Nope.. Still not working. The code you posted just returns the same error, just line 19 instead of 20.

 

EDIT:

 

I know, this was just made pretty quickly, and im new to OOP.

 

I tried this out instead, but didn't work either.

 

<?php

class query {

function sql_connect() {
	mysql_connect("localhost", "root", "");
	mysql_select_db("underskrift");
}
   
   function sql_query($sql) {
      $this->sql_connect();
      mysql_query($sql) or die(mysql_error());
      }
   
   function sql_row($result) {
      mysql_fetch_array($result);
      }
}

$query = new query;

$sql = 'SELECT * FROM underskrifter';
$result = $query->sql_query($sql);

while($row = $query->sql_row($result)) {
   
   echo $row['underskrift_navn'];
   
}

?>

I agree with thorpe, however:

class query {

function sql_query($sql) 
{
	global $db;
	$db->db_connect();
	$query = mysql_query($sql) or die(mysql_error());
	return $query;
}

function sql_row($result) 
{
	return mysql_fetch_array($result);
}
}

 

Works.

 

You need to return the results from the functions.

 

 

how about this.....

 

 

<?php

class db {
   
   function db_connect() {
      mysql_connect("localhost", "root", "") or die(mysql_error());
      mysql_select_db("underskrift") or die(mysql_erro());
      }
}

class query {
   
   function sql_query($sql, $db) {
      $db->db_connect();
      return mysql_query($sql) or die(mysql_error());
      }
   
   function sql_row($result) {
      return mysql_fetch_array($result);
      }
}

$db = new db;
$query = new query;

$sql = 'SELECT * FROM underskrifter';
$result = $query->sql_query($sql, $db);

while($row = $query->sql_row($result)) {
   
   echo $row['underskrift_navn'];
   
}

?>

 

@ thorpe..

he does connect inside the second class

 

@ Gazan...

Thorpe is right, that's a bad way of doing this. The whole thing should be one class, and using globals is not a good way to deal with state

 

 

EDIT: what Mikedean said!

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.