Forum Moderators: coopster

Message Too Old, No Replies

Problem with my php code

         

skoff

6:28 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



Hi, this is my first post on this forum,
This is the error i get :
Parse error: parse error, unexpected T_WHILE in C:\Program Files\EasyPHP 2.0b1\www\test.php on line 18

line 18 = while ($row = mysql_fetch_array($result))

and this is the code of my test.php page :
<html>
<body>

<table>

<?php

$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';

if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");

$result = mysql_query("SELECT * FROM MEMBRE WHERE categorie = '" . $_GET['categorie'])

while ($row = mysql_fetch_array($result))
{
echo "<tr><td>" . $row["id"] . "</td><td>" . $row["nom"] . "</td><td>" . $row["age"] . "</td></tr>
}

mysql_close();

?>

</table>

</body>
</html>

eelixduppy

6:31 pm on Nov 16, 2008 (gmt 0)



Hello and Welcome to WebmasterWorld! :)

You forgot a semi-colon at the end of this line:


$result = mysql_query("SELECT * FROM MEMBRE WHERE categorie = '" . $_GET['categorie']);

skoff

6:33 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



man, that was fast... lol thanks but i have a new error now...
Parse error: parse error, unexpected $end in C:\Program Files\EasyPHP 2.0b1\www\test.php on line 30
and line 30 is : </html> it's my last line..? i dont get it

eelixduppy

6:35 pm on Nov 16, 2008 (gmt 0)



hehe, didn't see this one. You have this line malformed. It should be the following (with quotes and semi-colon added at the end):

echo "<tr><td>" . $row["id"] . "</td><td>" . $row["nom"] . "</td><td>" . $row["age"] . "</td></tr>";

skoff

6:39 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



i made the modification and now i get :
Notice: Undefined index: categorie in C:\Program Files\EasyPHP 2.0b1\www\test.php on line 16

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\EasyPHP 2.0b1\www\test.php on line 18

skoff

6:44 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



if its more simple to tell you what i want to do and then you give me the code then, i'll explain, want i want to do is when i click on the link named boys i want to have all the boys that are in my table (database) to appear in a table (on the page test.php) and if i click on the link girls i want to do the same.. and i think the link would look like this test.php?categorie=boys

boys and girls are in the column categorie

[edited by: skoff at 6:51 pm (utc) on Nov. 16, 2008]

eelixduppy

6:44 pm on Nov 16, 2008 (gmt 0)



Alright, sorry, there are more errors here. I'm kind of out of it so please excuse me. Your query has incorrect syntax (missing the quote) and therefore your query is causing a MySQL error messing up your mysql_fetch_array call. As for the undefined index, you have to check to make sure that the categorie variable is defined in the URL before you do anything with it. As one added bonus, you MUST escape for SQL injections otherwise your database is vulnerable to all sorts of attacks. The line then should look like this:


if(!isset [php.net]($_GET['categorie'])) {
echo 'Category not specified';
die;
}
$result = mysql_query("SELECT * FROM MEMBRE WHERE categorie = '" . mysql_real_escape_string [php.net]($_GET['categorie'])."'");

Definitely read up on mysql_real_escape_string() as it is a VERY important addition to this line. Good luck :)

skoff

6:58 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



if its more simple to tell you what i want to do and then you give me the code then, i'll explain. What i want to do is when i click on the link named boys i want to have all the boys that are in my table (database) to appear in a table (on the page test.php) and if i click on the link girls i want to do the same.. and i think the link would look like this test.php?categorie=boys

boys and girls are in the column categorie

eelixduppy

7:14 pm on Nov 16, 2008 (gmt 0)



>> if its more simple to tell you what i want to do and then you give me the code then, i'll explain.

If I did all the work for you you will not be learning. :) Try to do your best and if you run into any roadblocks I, or someone else, will be glad to help out.

skoff

7:16 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



yeah ok, but the code you just helped me with, is it ok for what i want to do?

skoff

7:18 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



when i go to my page test.php i get category not specified and when i write test.php?categorie=boys it's not working and i get Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\EasyPHP 2.0b1\www\test.php on line 22
line 22 is : while ($row = mysql_fetch_array($result))

my database name is test
my table name with the information is membre

i really dont understand...

[edited by: skoff at 7:20 pm (utc) on Nov. 16, 2008]

eelixduppy

7:20 pm on Nov 16, 2008 (gmt 0)



Alright. There is still something wrong with your query. Try changing the code for debugging:

$result = mysql_query("SELECT * FROM MEMBRE WHERE categorie = '" . mysql_real_escape_string($_GET['categorie'])."'") [b]or die(mysql_error())[/b];

What does this output?

skoff

7:25 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



when i go to page test.php i get category not specified and
when i go to page test.php?categorie=boys it says No database selected

eelixduppy

7:32 pm on Nov 16, 2008 (gmt 0)



ahh...yea, you should be doing that after you connect.

mysql_select_db("test");

skoff

7:36 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



THANKS A LOT DUDE ITS WORKING!

eelixduppy

7:37 pm on Nov 16, 2008 (gmt 0)



nice :)

skoff

7:41 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



now i want to know if its possible to modify the table where i see the informations like if i want a background color on it, put border etc.. is it possible?

skoff

7:49 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



nevermind lol i found it... i'm such an idiot XD

skoff

8:00 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



i want to know how can i show the column name in my table on my page?

eelixduppy

8:06 pm on Nov 16, 2008 (gmt 0)



Before the while loop you have to echo out that data.

skoff

8:13 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



and how do i do that? 8-)

skoff

12:19 am on Nov 17, 2008 (gmt 0)

10+ Year Member



nvm i found thanks a lot for your help eelixduppy! really appreciate it! ;)