Page 1 of 1

PHP login page errors

Posted: Thu Dec 22, 2011 6:43 am
by alex123
Hi, I have been following these videos http://www.youtube.com/watch?NR=1&featu ... sgnKTuQeMw of yours and have been unable to get the code to work.
include('core/init.inc.php');

?>
<html>
<head>
<title>Registered Users</title>
</head>
<body>
	<div>
		<?php
		foreach (fetch_users()) as $user){
			?>
			<p>
				<a href=""><?php echo $user['username']; ?></a>
			</p>
			<?php
		}
		?>
	</div>
</body>
</html>
core/init.inc.php
<?php
session_start();

mysql_connect('localhost','root','1234');
mysql_select_db('youtube');

$path = dirname(__FILE__);

include("($path)/inc/user.inc.php");

$_SESSION['id']=1;
?>
core/inc/user.inc.php
<?php
//fetches all of the users from the table.
function fetch_users(){
	$result = mysql_query('SELECT 'id' as 'id', 'username' as 'username' FROM 'user'');
	
	$users = array();
//basically this is an array that goes through your database
	while (($row = mysql_fetch_assoc($result)) !== false){
		$users[] = $row;
	}
	
	return $users;
}
?>
user_list.php
<?php

include('core/init.inc.php');

?>
<html>
<head>
<title>Registered Users</title>
</head>
<body>
	<div>
		<?php
		foreach (fetch_users()) as $user){
			?>
			<p>
				<a href=""><?php echo $user['username']; ?></a>
			</p>
			<?php
		}
		?>
	</div>
</body>
</html>

When I open http://localhost/user_list.php, I am supposed to see usernames, but I get: Parse error: syntax error, unexpected ')' in C:\wamp\www\user_list.php on line 13.

my database is called 'youtube' and my table is called 'user': http://s1085.photobucket.com/albums/j43 ... titled.png

Thanks a lot for any help!! I really appreciate it because this will be the most complex part of my site. I know that if I can get this to work the rest will be fine (except for maybe the search).

Re: PHP login page errors

Posted: Thu Dec 22, 2011 7:40 am
by bowersbros
That error is incredibly useful..

It tells you whats wrong and where, how couldn't you find it?
foreach (fetch_users()) as $user)
There is an extra ) in there, as the error stated.

Find it :)

Re: PHP login page errors

Posted: Thu Dec 22, 2011 7:45 am
by alex123
lol pretty embarrassing. Thank you for that

Okay sorry I found some more errors on my own. For user_list.php I was supposed to add include('core/inc/user.inc.php'); instead. But what does "Parse error: syntax error, unexpected T_STRING in C:\wamp\www\core\inc\user.inc.php on line 4" mean with regard to $result = mysql_query('SELECT 'id' AND 'username' FROM 'user'');

I think it should be fine after this.

Re: PHP login page errors

Posted: Thu Dec 22, 2011 8:08 am
by bowersbros
in mysql, a comma means and.

so you would do
SELECT `id`,`username` FROM `users`

Re: PHP login page errors

Posted: Thu Dec 22, 2011 8:13 am
by alex123
It is giving me the same error, both with and without the outer ' :/

Re: PHP login page errors

Posted: Thu Dec 22, 2011 8:18 am
by bowersbros
Ah my bad, i didnt read it properly.

Your using quotes around the column names
mysql_query('SELECT `id,`username` FROM `users`');
Also, because you were using ' to open and close the string, using ' internally will effectively end that string, you would need to use " or concatonate variables into it.

The code above should work

Re: PHP login page errors

Posted: Thu Dec 22, 2011 8:23 am
by alex123
Still not working.. that and several other combinations I've tried seem to be performing an infinite loop. I'm getting a top of error messages that keep generating for the same line. Also, is there a difference between ' and the ` you are using (I believe it's the French apostrophe)? Mind you I've tried almost all types of syntax for that line. Could it be another issue? Many thanks

oh and using double quotes also performs an infinite loop for some reason. what on earth is going on.....

Re: PHP login page errors

Posted: Thu Dec 22, 2011 8:27 am
by bowersbros
$result = mysql_query('SELECT `id`, `username` FROM `user`');
for inside the function

Re: PHP login page errors

Posted: Thu Dec 22, 2011 8:39 am
by alex123
It gives a strange loop again. I tried pretty much every syntax now. I'm off to bed though. Again, thanks for the help. Here's where I'm at now:

user_list.php:
<?php

include('core/inc/user.inc.php');
?>
<html>
<head>
<title>Registered Users</title>
</head>
<body>
	<div>
		<?php
		foreach (fetch_users() as $user){
			?>
			<p>
				<a href=""><?php echo $user['username']; ?></a>
			</p>
			<?php
		}
		?>
	</div>
</body>
</html>
core/init.inc.php
<?php
session_start();

mysql_connect('localhost','root','1234');
mysql_select_db('youtube');

$path = dirname(__FILE__);

include("($path)/inc/user.inc.php");

$_SESSION['id']=1;
?>
core/init.inc.php (for which I've tried almost everything for line 4 in terms of syntax):
<?php
//fetches all of the users from the table.
function fetch_users(){
        $result = mysql_query('SELECT 'id', 'username' FROM 'user'');
       
        $users = array();
//basically this is an array that goes through your database
        while (($row = mysql_fetch_assoc($result)) !== false){
                $users[] = $row;
        }
       
        return $users;
}
?>
core/inc/user.inc.php
<?php
//fetches all of the users from the table.
function fetch_users(){
        $result = mysql_query('SELECT 'id', 'username' FROM 'user'');
       
        $users = array();
//basically this is an array that goes through your database
        while (($row = mysql_fetch_assoc($result)) !== false){
                $users[] = $row;
        }
       
        return $users;
}
?>

Re: PHP login page errors

Posted: Thu Dec 22, 2011 8:46 am
by bowersbros
You could try doing the function with a reference variable

Eg:
$users[];
function fetch_users(&$users){
// code here, dont return just make = $users;
}
After that, instead of foreach(fetch_users() as $user) you could do foreach( $users as $user) and see if that helps at all?

Re: PHP login page errors

Posted: Thu Dec 22, 2011 8:46 am
by bowersbros
or you could keep the function as it is, and do $users = fetch_users();

then change the foreach loop accordingly.

Re: PHP login page errors

Posted: Thu Dec 22, 2011 2:13 pm
by Temor
you're supposed to use backticks (`) in your SQL syntax. Semiquotes ( ' ) will read as a string, and you're trying to access a field in your database, not insert a string :)

Use backticks around id username and user.

Re: PHP login page errors

Posted: Thu Dec 22, 2011 3:32 pm
by bowersbros
Temor wrote:you're supposed to use backticks (`) in your SQL syntax. Semiquotes ( ' ) will read as a string, and you're trying to access a field in your database, not insert a string :)

Use backticks around id username and user.
Ive already mentioned that, they said it didnt work

Re: PHP login page errors

Posted: Thu Dec 22, 2011 3:52 pm
by Temor
bowersbros wrote:
Temor wrote:you're supposed to use backticks (`) in your SQL syntax. Semiquotes ( ' ) will read as a string, and you're trying to access a field in your database, not insert a string :)

Use backticks around id username and user.
Ive already mentioned that, they said it didnt work
He should still fix it though. It won't work as it is now.

/Edit: I'm thinking maybe he misunderstood and changed the qoutes wrapping the entire SQL query and not the ones inside the query.

Re: PHP login page errors

Posted: Thu Dec 22, 2011 8:59 pm
by alex123
$result = mysql_query('SELECT `id`, `username` FROM `user`');

gives me a ton of error messages and does a huge loop like I said. hmm what to do

Re: PHP login page errors

Posted: Thu Dec 22, 2011 9:06 pm
by Temor
what exactly are the error messages saying?

Re: PHP login page errors

Posted: Thu Dec 22, 2011 9:27 pm
by jacek
alex123 wrote:
$result = mysql_query('SELECT `id`, `username` FROM `user`');

gives me a ton of error messages and does a huge loop like I said. hmm what to do
Try adding a
die(mysql_error());
after the mysql_query() line, that should show you what is wrong with the SQL. my best guess is that your table is called users not user :)

Re: PHP login page errors

Posted: Thu Dec 22, 2011 9:47 pm
by alex123
Oh thanks. It say no database selected. And my database is called user

Re: PHP login page errors

Posted: Thu Dec 22, 2011 11:57 pm
by bowersbros
If your referring to what is held within the mysql_query() then that is your table.

To select a database, its mysql_select_db()

/edit because currently your using the database youtube, not user

Re: PHP login page errors

Posted: Fri Dec 23, 2011 12:05 am
by alex123
Thank you for the help. I got it to work by inserting
$link = mysql_connect('localhost','root','1234') or die('Cant connect to database');
	mysql_select_db('youtube');
on top. It was quite obvious now that I look back at it.. I might have copied his code wrong. Thanks again

Re: PHP login page errors

Posted: Fri Dec 23, 2011 4:34 pm
by jacek
Looks good, that should be done in the init.inc.php file already though ? Just want to make sure you are not connecting to the db inside the function ;)

also, there is no need to create the $link variable.