Page 1 of 1

PHP Question

Posted: Wed Sep 17, 2003 5:47 pm
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

Posted: Wed Sep 17, 2003 6:07 pm
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.");
}
?>

Posted: Wed Sep 17, 2003 6:19 pm
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]; 
}

Posted: Wed Sep 17, 2003 6:43 pm
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?

Posted: Wed Sep 17, 2003 7:33 pm
by Akwebby
fixed

Posted: Thu Sep 18, 2003 9:59 am
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.