PHP Question

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
Akwebby
Forum Newbie
Posts: 15
Joined: Wed Sep 17, 2003 5:47 pm

PHP Question

Post by Akwebby »

alright i don't know if i can just copy and paste the code here but its worth a try...

global $default_dbname, $user_tablename;

$link_id = db_connect($default_dbname);
$query = "SELECT username FROM $user_tablename
WHERE userid = '$userid'
AND userpassword = password('$userpassword')";
$result = mysql_query($query);
if(!result) die(sql_error(error_message()));


if(!mysql_num_rows($result)) return 0;


else {
$query_data = mysql_fetch_row($result);
return $query_data[0];
}


For the line that states if(!mysql_num_rows($result)) return 0; I get the error stating that the argument sent is not a valid argument of that function.

Does anyone know how to fix this? I am stumped.

~Akwebby
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

If you're looking to end script execution in the if statement try this:

Code: Select all

<?php
if  (!mysql_num_rows($result)) {
die("No result found.");
}
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

1.

Code: Select all

<?php
if(!result) die(sql_error(error_message()));  // this is wrong in your script
if(!$result) die(sql_error(error_message())); //...as youre missing the $
?>
2.
Just some thoughts as I never write like this myself:

Code: Select all

if(!$result) die(sql_error(error_message())); 
if(!mysql_num_rows($result)) return 0; 
else { 
$query_data = mysql_fetch_row($result); 
return $query_data[0]; 
}
In this example, that else-statement... what IF-statement does that belong to really, when you ignore using {}'s in your code?

3.
Akwebby
Friendly note and advice... Use {}'s and indents. It will help you later on in your phpcoding life.

Code: Select all

// just example of one way to write code...
if (!$result) { 
    die(sql_error(error_message())); 
}
if (!mysql_num_rows($result)) { 
    return 0; 
} else { 
    $query_data = mysql_fetch_row($result); 
    return $query_data[0]; 
}
Akwebby
Forum Newbie
Posts: 15
Joined: Wed Sep 17, 2003 5:47 pm

Post by Akwebby »

Alright the copy and paste really screwed up readability before so here it is again...

global $default_dbname, $user_tablename2;

$link_id = db_connect($default_dbname);

$query = "SELECT username FROM $user_tablename2 WHERE userid = '$userid' AND userpassword = password('$userpassword')";

$result = mysql_query($query);

if(!$result) die(sql_error(error_message()));

if(!mysql_num_rows($result)) return 0;
else
{
$query_data = mysql_fetch_row($result);
return $query_data[0];
}

The emboldened line is where my question lies. I get an error stating that the supplied argument is not valid for the called function, but it most certainly is valid in my mind. Any thoughts?
Akwebby
Forum Newbie
Posts: 15
Joined: Wed Sep 17, 2003 5:47 pm

Post by Akwebby »

fixed
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

Just a tip, if you enclose your code on the forums with [syntax=php]and[/syntax] you will get it properly formatted, as it is in duff and JAMs post.
Post Reply