PHP login page errors

Ask about a PHP problem here.
Post Reply
alex123
Posts: 14
Joined: Thu Dec 22, 2011 6:35 am

PHP login page errors

Post 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).
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: PHP login page errors

Post 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 :)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
alex123
Posts: 14
Joined: Thu Dec 22, 2011 6:35 am

Re: PHP login page errors

Post 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.
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: PHP login page errors

Post by bowersbros »

in mysql, a comma means and.

so you would do
SELECT `id`,`username` FROM `users`
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
alex123
Posts: 14
Joined: Thu Dec 22, 2011 6:35 am

Re: PHP login page errors

Post by alex123 »

It is giving me the same error, both with and without the outer ' :/
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: PHP login page errors

Post 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
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
alex123
Posts: 14
Joined: Thu Dec 22, 2011 6:35 am

Re: PHP login page errors

Post 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.....
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: PHP login page errors

Post by bowersbros »

$result = mysql_query('SELECT `id`, `username` FROM `user`');
for inside the function
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
alex123
Posts: 14
Joined: Thu Dec 22, 2011 6:35 am

Re: PHP login page errors

Post 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;
}
?>
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: PHP login page errors

Post 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?
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: PHP login page errors

Post by bowersbros »

or you could keep the function as it is, and do $users = fetch_users();

then change the foreach loop accordingly.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: PHP login page errors

Post 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.
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: PHP login page errors

Post 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
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: PHP login page errors

Post 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.
alex123
Posts: 14
Joined: Thu Dec 22, 2011 6:35 am

Re: PHP login page errors

Post 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
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: PHP login page errors

Post by Temor »

what exactly are the error messages saying?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP login page errors

Post 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 :)
Image
alex123
Posts: 14
Joined: Thu Dec 22, 2011 6:35 am

Re: PHP login page errors

Post by alex123 »

Oh thanks. It say no database selected. And my database is called user
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: PHP login page errors

Post 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
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
alex123
Posts: 14
Joined: Thu Dec 22, 2011 6:35 am

Re: PHP login page errors

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP login page errors

Post 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.
Image
Post Reply