Weird problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jidospod
Forum Commoner
Posts: 33
Joined: Tue Jun 11, 2002 1:05 am

Weird problem

Post by jidospod »

Database chat_users
Warning: mysql_list_tables(): supplied argument is not a valid MySQL-Link resource in /home/jonas/public_html/chat/list.php on line 24

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/jonas/public_html/chat/list.php on line 26

Any idea why its telling me those are not valid MySQL-Link resources? I am running.. (checks)

mysql Ver 11.15 Distrib 3.23.45, for pc-linux-gnu (i686)


All I am trying to do is list ALL usernames in that database.. *sigh* Can anyone show me a working way to LIST all data in that database and one for searching and displaying just specific data? (we'll use 'username' as an example) because Im getting this error and I dont know why!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

first try to find the error yourself by adding "or die(mysql_error())"s to your script
i.e.

Code: Select all

$dbConn = mysql_connect(...) or die(mysql_error());
mysql_select_db($dbName, $dbConn) or die(mysql_error());
$result = mysql_query($query, $dbConn) or die(mysql_error());
jidospod
Forum Commoner
Posts: 33
Joined: Tue Jun 11, 2002 1:05 am

Post by jidospod »

Yeah I fixed it, I had a small typo..

Another problem though.

I have an apply function and a login function in my ONE php file (yeah Im only using one), anywho both those functions are HTML output with forms, how can I make apply post to user_reg() and login post to user_login()

I've tried almost everything, I know it is EASY if I use multiple files, but that isnt an option at this time.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

not quite sure, I got the point but

Code: Select all

<input type="submit" name="mode" value="register" /> <br/>
<input type="submit" name="mode" value="login" />
you get the same values as with type="hidden" or "text" but only the data from the submit-button that has been activated, so the script can decide what to do.
jidospod
Forum Commoner
Posts: 33
Joined: Tue Jun 11, 2002 1:05 am

Post by jidospod »

volka wrote:not quite sure, I got the point but

Code: Select all

<input type="submit" name="mode" value="register" /> <br/>
<input type="submit" name="mode" value="login" />
you get the same values as with type="hidden" or "text" but only the data from the submit-button that has been activated, so the script can decide what to do.
Ok I'm using case's and having login html called with ?id=login, and reg ?=apply (those both go to html forms)

Now I need those forms to be able to be submitted (to the same php file they are being called from) and load up the proper functions, for example login form might call user_login function, and registration form might call user_registration, now I cant seem to get it to work properly.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<html><body>
<?php
if (!isset($_POST&#1111;'mode']) || !isset($_POST&#1111;'uname']))
&#123; ?>
	<form method="post">
	user name: <input type="text" name="uname"/><br/>
	<input type="submit" name="mode" value="login"/>
	<input type="submit" name="mode" value="register"/><br/>
	</form>
<?php
&#125;
else
&#123;
	switch($_POST&#1111;'mode'])
	&#123;
	case 'login':
		print('login procedure for '.$_POST&#1111;'uname']);
		break;
	case 'register':
		print('registration procedure for '.$_POST&#1111;'uname']);
		break;
	default:
		print('malformed request');
		break;
	&#125;
&#125;
?>
</body></html>
jidospod
Forum Commoner
Posts: 33
Joined: Tue Jun 11, 2002 1:05 am

Post by jidospod »

Thanks vodka, works, but I get an error when trying to send to proper fucntion Im using:

user_login('.$_POST['username']','.$_POST['password']');

I get the error:

Parse error: parse error, unexpected T_STRING in /home/canadian/public_html/csys/csys.php on line 20


Not certain how I should pass it to user_login(); which requires username and password. I've tried a few ways now.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

should be

Code: Select all

user_login($_POST&#1111;'username'], $_POST&#1111;'password']);
you don't need the quotes; the variables already represent strings.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

first the simple answer:
user_login($_POST['username'], $_POST['password']);

---
a string literal starts with either " or ' and ends with the same character
"valid" 'valid' "invalid' 'invalid"

if unquoted the start-stop-sign within the literal marks the end of it
"this is unvalid because " is within the literal"
The parser finds ", parses this is invalid because as literal (ending with ") and than finds is which has no meaning at this location -> error (unexpected T_STRING).

if starting the string with " you may use ' freely within i.e. "I'm happy to be valid ;)" and ' "me" too '

"this is valid because \" is quoted within the literal"
\ turns off the special meaning of some characters.

The operator . concatenates two strings
$a = "b" . "c" -> $a == "ab"
$a = "b" . "c" . "d" -> "b" . ("c" . "d") -> "b" . ("cd") -> "b" . "cd" == "bcd"


if a literal is marked with " (instead of ') the contents is parsed and all variables (starting with $) are replaced by their values.
$var = 'test';
$a = "this is a $var" -> a$ == "this is a test"

If the contents of an array field shall be replaced the array must be marked for the parser with {} (i.e. "posted username is {$_POST['username']}")

There's no need for something like $a="$variable" or $a="{$_POST['username']}" since you may directly assign a value to variable i.e. $a=$variable; or $a=$_POST['username'];

Same goes with functions:
doFunc("$variable", "{$_POST['username']}"); -> doFunc($variable, $_POST['username']);

The only reason I can image why someone uses doFunc("$variable") is to assure the parameter will be a string and not empty (maybe of length 0 but not NULL)
jidospod
Forum Commoner
Posts: 33
Joined: Tue Jun 11, 2002 1:05 am

Post by jidospod »

Main code:
if (isset($_POST['mode']) || isset($_POST['username'])) {
switch($_POST['mode'])
{
case 'login':
user_login($_POST['username'], $_POST['password']);
break;
case 'register':
print('registration procedure not updated '.$_POST['username']);
break;
default:
break;
}
} else {


/* put all the register stuff and login stuff into their own function */

$id = $_GET['id'];

switch($id){
case "login":
login();
break;
case "apply":
if (!CSYS_APPLY) { echo '<b>Sorry, we are not currently Accepting appli$
} else { apply(); }
break;
default:
login();
}
}
Now the actual login procedure is:
function user_login($username,$password) {
global $feedback;
if (!$username || !$password) {
$feedback .= ' ERROR - Missing user name or password ';
return false;
} else {
$username=strtolower($username);
$password=strtolower($password);
$sql="SELECT * FROM csys_users WHERE username='$username' AND password='$password';
$result=db_query($sql);
if (!$result || db_numrows($result) < 1){
$feedback .= ' ERROR - User not found or password incorect';
return false;
} else {
if (db_result($result,0,'is_active') == '1') {
$feedback .= '1';
main_menu($username,$password);
} else {
$feedback .= '0';
return false;
}
}
}
}
I know it authenticates properly.. I tested it, but it wont send the user to the main menu, which it did before i combined them. What I get with this, is if a user tries to login, wether the login/password are correct or not it takes them to a blank page. Man I wish the admin really wanted this all in seperate files, I would have been done already without all these headaches.. and I hate bugging you guys ;) Any idea why main_menu doesnt seem to be getting called? this all functioned fine when I had it in seperate files, which is not an option for the admin so they are all in one file. Thanks in advance.
jidospod
Forum Commoner
Posts: 33
Joined: Tue Jun 11, 2002 1:05 am

Post by jidospod »

Apparently when I use the $_POST stuff, and it sends to user_login, if I echo it, it is the proper username/passwd, but it wont match it there in the SQL function.. any ideas why? I've played with this for a few hours now, and I think Im going to go lay down and let this headache run its course
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Change this:

Code: Select all

$sql="SELECT * FROM csys_users WHERE username='$username' AND password='$password';
To this:

Code: Select all

$sql="SELECT * FROM csys_users WHERE username='$username' AND password='$password'";
I don't know if that's your only problem, since I have only worked with mysql databases.
jidospod
Forum Commoner
Posts: 33
Joined: Tue Jun 11, 2002 1:05 am

Post by jidospod »

Thanks for the reply, noticed it right before I ran out the door for work and fixed it, sorry I didnt have time to post that I resolved the issue :)

*runs from you* hehe ;)

Thanks a lot for all your help.. I've never made a PHP Script of this magnitude and kept it ALL in one file (aside from templates.php, database.php and config.php). :) It's quite a different angle on PHP this way, learning new stuff, always a great sign :)
Post Reply