You are here: Re: IF/Else using MySQL results « All PHP « IT news, forums, messages
Re: IF/Else using MySQL results

Posted by linda on 11/21/06 21:01

"Jerim79" <mylek@hotmail.com> wrote in message
news:1164123881.713122.220000@b28g2000cwb.googlegroups.com...
>
> 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
>> }
>
> It looks like I need the function $result-> to accomplish this.
> However, I am not sure how to use it in my case. Every attempt to use
> it results in an error. For instance:
>
> $result = $result->mysql_query($query) or die('Query failed: ' .
> mysql_error());
>


When i'm check the database for say username password from a form that's
been submitted i do it like this:

include_once 'db_connection.php';

$FNAME = mysql_real_escape_string($_POST['FNAME']);
$LNAME = mysql_real_escape_string($_POST['LNAME']);


$query = "SELECT FNAME AND LNAME FROM table WHERE FNAME ='$FNAME' AND LNAME
= '$LNAME'"; //there is a single quote ' and a double " quote here.
$result = mysql_query($query);

if (mysql_num_rows($result)>0) {
echo 'Echo Something here for not in database';
}else{
echo 'Echo Something here for a match.';
}

Though I must admit J.O. Aho's function example is probably better as it
will also give you a count of how many there are.

Best wishes,
Linda

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация