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
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'];
   
}

?>

Link to comment
Share on other sites

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'];
   
}

?>

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

Hmm. All your examples just keeps giving me the same error..

 

EDIT:

 

Thanks everyone! got it to work. Especially thanks Mikedean, second try on your example did it!

And yeah, - i'll put the connection inside the query class. - Thanks for the tip :)

Link to comment
Share on other sites

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.