Jump to content

Connect to db from within a class


M.O.S. Studios

Recommended Posts

I'm trying to do a db query from within a class, but i keep getting an error saying that my db doesn't exist.

 

I tried making the db connection from within the class

 

I also tried making the connection before i called the class

 

both held the same result.

 

is there something that I'm missing?

 

 

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/207425-connect-to-db-from-within-a-class/
Share on other sites

<?php
CLASS member{

function login($username, $password){

	$q = mysql_query("SELECT * FROM `users` WHERE username = '".$username."' AND password = '".$password."'") or die (mysql_error());
	$r = mysql_num_rows($q);

	if ($r == 1){
		$_SESSION['logged'] = 1;
		RETURN TRUE;
	}else{
		RETURN FALSE;
	}
}
}
?>

 

 

$members = new member();
if($_POST['member_action']=='login'){
 include('link.php'); //link to a file that has the mysql_connect function in it
 $members->login($_POST['member_name'], $_POST['member_pass']);
 mysql_close($link);
}

<form action="*current url*" method="POST">
<div class='login'>
<div class='username'>
	Username:
</div>
<div class='username_input'>
	<input type="text" name="member_name">
</div>
<div class='password'>
	Password:
</div>
<div class='password_input'>
	<input type="password" name="member_pass">
</div>	
</div>

<div class='login_options'>
<div class='submit'><input type='submit' name='member_action' value='login'></div>
</div>
</form>

 

when ever i run the mysql_query i get this e_warning:

 

Table '*table name*' doesn't exist

 

weird???

any ideas

Table '*table name*' doesn't exist

 

That back-ticks around the table name in the query are not a problem.

 

The error is self explanatory, the table name you are using in the query doesn't exit in the database that you have selected.

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.