...

View Full Version : PHP new coder problem



Bazrazmataz
04-19-2004, 09:31 AM
I keep getting the error:
Warning: extract() expects first argument to be an array in c:\program files\apache group\apache\htdocs\test2.php on line 20

I cant figure out what is wrong can anyone help me?


<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$host = "Localhost";
$user="root";
$password="Duranies";
$database="MemberDirectory";
$connection = mysql_connect($host,$user,$password)
or die("The database is unavailable at the moment");
$db = mysql_select_db($database,$connection)
or die("DB is Unavailable");
$userEntry = "Secret";
$query = "SELECT password FROM Member WHERE loginName='gsmith'";
$result = mysql_query($query)
or die ("couldnt execute query.");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
extract($row);
if ($userEntry== $row['password'])
{
echo "Login Accepted<br>";
}
else
{
echo "invalid password<br>";
}
?>
</body>
</html>

firepages
04-19-2004, 10:17 AM
what do you get if you <?print_r($row);?> before the extract ? , e.g. is $row empty?

raf
04-19-2004, 11:57 AM
what do you get if you <?print_r($row);?> before the extract ? , e.g. is $row empty?
Hmm. I would suspect to get the error on the mysql_fetch_array() then.

Anyway, i don't understand the logic in your code. I'd replace


$userEntry = "Secret";
$query = "SELECT password FROM Member WHERE loginName='gsmith'";
$result = mysql_query($query)
or die ("couldnt execute query.");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
extract($row);
if ($userEntry== $row['password'])
{
....

by


$query = "SELECT count(*) as numcheck FROM Member WHERE loginName='gsmith' and password='Secret'";
$result = mysql_query($query) or die ("couldnt execute query.");
if (mysql_num_rows($result)!=1){
die ('Possible databaseproblem.');
} else {
$row = mysql_fetch_assoc($result);
if ($row['numcheck']==1){
...

Bazrazmataz
04-19-2004, 02:21 PM
I copyied the code exactly out of the textbook im so damm sick of it containing errors can anyone suggest a good book for someone looking to learn php & mysql?

Any chance you guys could actually explain in laymans terms the lines that are wrong and why! I can easily swap bad code for the lines you suggest but im no nearer to understanding why it didnt work ! Thanks

Bazrazmataz
04-19-2004, 02:41 PM
Im also getting a error message with this chunk of code:

Parse error: parse error in c:\program files\apache group\apache\htdocs\petsend.php on line 25


<html>
<head><title>Pet Catalog</head>
<body>
<?php
$user="root";
$host="localhost";
$password="Duranies";
$database="petCatalog";
$connection = mysql_connect($host,$user,$password)
or die("Couldnt connect to database");
$db=mysql_select_db($database,$connection)
or die("Couldnt connect to db");
$pettype="horse";
$query= "select * from pets where petType='$pettype'";
$result = mysql_query($query)
or die("Could not execute query");

echo "<h1>Horses</H>";
echo "<table cellspacing='15'>";
echo"<tr><td colspan='3'><hr></td></tr>;
while ($row = mysql_fetch_array($result))
{
extract($row);
$f_price = number_format($price,2);
echo"<tr>\n
<td>$petDescription</td>\n
<td align='right'>\$$f_price</td>\n
</tr>\n";
echo "<tr><td collspan='3'><hr></td></td>\n";
}
echo "</table>\n";
?>
</body>
</html>

raf
04-19-2004, 04:06 PM
I can understand your frustration, but PHP is a language that is very much alive and new functions are born and syntaxe-rules change.
So by the time you buy a book, it's bound to be outdated.
Also, 100 pieces of examplecode is not the same as an application with 100 pages of code. Each example is propably set up to be an independent piece of code, and therefore becomes a bit artifical and unefficient.
If you wan't to learn to code, then you also need to read a lott of code. And look up everything you don't understand in the manual or in forums like this one.

In the code you post, there is not a single line of code that is completely correct. (That is, is addition to being unefficient an not very logicaly structured.)
As i'll gladly demonstrate



/* These 4 variables out simply a waste of space.
- Unnescecary;
- Unefficient the stringvalues are surrounded in double quotes, which means they'll be unnescecary parsed
- Unsecure :
* your connectiondetails should not be inside your script, but inside a server side include with a php-extension, that is preferably stored in a directory that is not accesible from the web
* Unsecure because the variables keep this value throughout the scriptexecution. You should unset the variables after the connection is made.
$host = "Localhost";
$user="root";
$password="Duranies";
$database="MemberDirectory";
*/
/* Your connectinstring should be inside an include, and there, they should be made up like this
$connection = @mysql_connect('localhost','root','Duranies') or die('The database is unavailable at the moment');
$db = @mysql_select_db('MemberDirectory',$connection) or die('DB is Unavailable');
Notice the @ to suppres errormessage, that would leak sensitive infoabout your muSQL account and db.
Your incmludestament then is something like */
require('./36ghht5vvcx56vhg.php');
/* another pointless variable and some ilogic code that should be replaced . See below? The individual problems
$userEntry = "Secret"; ---> pointles, double quotes
$query = "SELECT password FROM Member WHERE loginName='gsmith'"; --> you don't need userdata so don't select it. Just do a count
$result = mysql_query($query) or die ("couldnt execute query."); --> use the linkidentifier inside themysql_query
$row = mysql_fetch_array($result,MYSQL_ASSOC); --> simply use the more elegant mysql_fetch_assoc()
--> you shoul also check the number of returned rows before starting to loop through the recordset.
extract($row); --> completely pointless since on the next line you use the $row-array !!
if ($userEntry== $row['password']) --> should be if ($userEntry== $password)
The alternative:*/
$query = "SELECT count(*) as numcheck FROM Member WHERE loginName='gsmith' and password='Secret'";
$result = mysql_query($query,$connection) or die ('couldnt execute query.');
if (mysql_num_rows($result)!=1){
die('Possible databaseproblem.');
} else {
$row = mysql_fetch_assoc($result);
if ($row['numcheck']==1){
echo 'Login Accepted<br />'; //These echo's were almost perfect
} else {
echo 'invalid password<br />';
}
}



About your latest post : what is line 25 ?

Bazrazmataz
04-20-2004, 10:14 AM
Wow thanks !

This is so damm hard ive decided to ditch this for dummies book and get something rite up 2 date ive kinda lost my faith in it but im determined to learn! You seem like you know your stuff reall well raf how long did it take u to get to grips with the language?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum