Jump to content

PHP an MySQL problem


ypkumar

Recommended Posts

its been a long time since i touched php and now i've learnt a bit of sql i decided to come back.. I dont really know where to post this question because this i think is both php and sql related. So please feel free to move it to the right section if i posted it in the wrong section. ta.

 

I need help with this small problem of mine.. I use xampp 1.7 (which i think is a pretty good package for beginners like me) and I've been trying to make a simple login page (which i did), and now I moved some of the code into a function and into a separate file and in a separate directory, and thats when the page started acting wierd :s

 

I have this login.php as

....
include 'assets/functions_include.php';
$usrtxt = $_POST['usrtxt'];
$pwdtxt = $_POST['pwdtxt'];
$query="select count(*) from str_users where usrname='".$usrtxt."'";
doconnectdb($query);
.....

and i have this in functions_include.php file in assets directory

$dbserver = "localhost";
$dbuser = "root"; 
$dbpass = "password";
$dbname = "str_db";
function doconnectdb($query)
{
$connectionattempt = mysql_connect($dbserver,$dbuser,$dbpass);
if(!($connectionattempt))
{
	die("failed to connect because.. ".mysql_error());
}
else
{
	$seldbattempt = mysql_select_db($dbname);
	if(!($seldbattempt))
	{
		die("couldnt find the database because.. ".mysql_error());
	}
	else
	{
		querythis($query);
	}
}
}

function querythis($query)
{
$query = mysql_query($query);

if(!($query))
{
	die("query failed because.. ".mysql_error());
}
else
{
	$count = mysql_num_rows($query);
	return $count;
}
}
?>

 

and from sql, i do have a database str_db which has 1 table

str_users 

details for str_users:
--------------------------------------------------------------------
  | usrid  | usrname  | usrpassword  | usrisauth  | usrauthcode  | 
--------------------------------------------------------------------

  |    1   |   admin  |   creative   |      0     |     NULL     |

--------------------------------------------------------------------


 

and the username and password provided for the database in PHP are correct.

 

 

before writing the code in this way, I had the code in 1 single page and everything worked fine!!

 

and when i partitioned the code into separate files and put them in different folders, i started receiving this error..

 

couldnt find the database because.. No database selected

 

can anyone point out my mistake here?

thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/145384-php-an-mysql-problem/
Share on other sites

Try putting all these variables inside the doconnectdb (in your functions_include.php) function:

 

$dbserver = "localhost";
$dbuser = "root";
$dbpass = "password";
$dbname = "str_db";

 

And for my understanding mysql_connect should not even connect since these variables are outside the function, this is a variable scope issue.

 

 

Link to comment
https://forums.phpfreaks.com/topic/145384-php-an-mysql-problem/#findComment-763229
Share on other sites

Separate your logic, connect / select db and query are two different things, (connect & select db usually are static, one time type calls), where as query access is used throughout the application. Also pass your db variables to the function that contains your connect / select db functions (good idea), or declare them global within that function (bad idea)!

Link to comment
https://forums.phpfreaks.com/topic/145384-php-an-mysql-problem/#findComment-763233
Share on other sites

hmm.... I've removed all the functions and rewrote everything... and funnily it worked!

 

I've moved the database variables into a file called database.inc and included that file in my php file.

 

and the code is something like this..

....
elseif(isset($_GET['un']))
{
include'database.inc';
.....

 

printf, can you please brief me the disadvantages of declaring the variables global that way?

 

thanks for your input guys, that really made my rusty brain move a bit :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/145384-php-an-mysql-problem/#findComment-764412
Share on other sites

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.