|
Posted by J.O. Aho on 11/21/06 15:56
Jerim79 wrote:
> I am working on a script that will query a database for a FNAME/LNAME
> combo. If it finds the combo, I need it to do one set of instructions.
> If it doesn't find it, I need it to do something else. What I can't
> figure out is what variable to check against. Here is what I have for
> the relevant part of the script:
>
> $connection = mysql_connect($hostname, $username, $password);
> mysql_select_db($databasename) or die ("Cannot connect to database" .
> mysql_error());
> $query="SELECT FNAME, LNAME FROM (table) WHERE FNAME=('$FName') AND
> NAME=('$LName')";
> $result = mysql_query($query) or die('Query failed: ' . mysql_error());
>
> mysql_close($connection);
>
> I tried checking the value of $result to see if it changed depending on
> whether or not it found the selection. It does not. I just can't figure
> out what to use for:
>
> if (some expression){
> perform this code
> }
> else{
> do this
> }
>
In case you want to do it as functions, here is a small suggestion that you
can play with.
--- testscript.php ---
function isUser($fname,$lname) {
/* create the query to the database */
$query="SELECT FNAME, LNAME FROM table WHERE FNAME='$fname' AND NAME='$lname'";
/* execute the query */
$result=mysql_query($query);
/* ask how many rows the query generated and return that value */
return mysql_num_rows($result);
}
function doIfUser($fname,$lname) {
if($num=isUser($fname,$lname)) {
if($num>1) {
echo "There are $num users with that name in the database";
} else {
echo "Yes, there is a such user in the database";
}
} else {
echo "There aren't any such user in the database";
}
}
include_once("file_with_hostname_username_password.php");
$link = mysql_connect($hostname, $username, $password);
/* we assume we got the first/last name from a form , thats wehy $_POST[] */
doIfUser($_POST['firstname'],$_POST['lastname'])
mysql_close($link);
--- eof ---
//Aho
[Back to original message]
|