Check if value exists In MySQL row

38,485

Solution 1

Your query should be:

SELECT `header` FROM `data` WHERE `var` = '$foo'

This will return all the headers with a var value of $foo.

$db = mysqli_connect('localhost', 'username', 'password', 'database');
if($query = mysqli_query($db, "SELECT `header` FROM `data` WHERE `var` = '$foo'")){
  while($row = mysqli_fetch_assoc($query)){
    echo $row['header'];
  }
  mysqli_free_result($query);
}

Solution 2

It's not difficult at all, If I understand correctly then this should help you.

// Query Variable / Contains you database query information
$results = $query;

// Loop through like so if the results are returned as an array
foreach($results as $result)
{
    if(!$result['var'])
        echo $result['header'];
}

// Loop through like so if the results are returned as an object
foreach($results as $result)
{
    if(!$result->var)
        echo $result->header;
}

Solution 3

first connect to the db

$query = mysql_query("SELECT var, header FROM data WHERE id='1'") or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
    if($foo == $row['var']){
        echo $row['header'];
    }
}

EDIT: changed equality statement based on your edit

Solution 4

are you asking if $foo matches any of the fields in data, or if $foo=some_field? Here for if you want $foo==var.

$foo='somevalue';
$query="SELECT id, var, header FROM `data` WHERE var='$foo'";
$result=mysqli_query($query);
if($result->num_rows==0) 
  $loc= 'http://google.com';//default value for when there is no row that matches $foo
}else{
  $row=$result->fetch_assoc(); //more than one row is useless since the first header('Location: x') command sends the browser to a new page and away from your script.
  $loc=$row['header'];

}
header ("Location: $loc);
exit;

ETA: since you've edited your question, it appears that you want to echo the header column if your search value matches your var column. The above won't work for that.

Share:
38,485
Trufa
Author by

Trufa

Existing code exerts a powerful influence. Its very presence argues that it is both correct and necessary.

Updated on May 15, 2020

Comments

  • Trufa
    Trufa about 4 years

    I have a php variable: $foo

    My MySQL table called data has the following structure:

    id    var    header
    
    1     zj3     http://google.com
    

    I would like to check if $foo is all ready in var row.

    If it is I would like to echo header ("http://google.com")

    How would you approach this?

    Thanks in advance, please ask if any clarification is needed!

  • Trufa
    Trufa over 13 years
    This makes sense, I'll try it out, thanks for the quick response!
  • Trufa
    Trufa over 13 years
    Yes, this is correct, I want to echo the header column if your search value matches your var column.
  • Trufa
    Trufa over 13 years
    So if I compare it to empty, I will know if the value exists? what will it return if it doesn't find any value?
  • gen_Eric
    gen_Eric over 13 years
    If it doesn't find anything, it'll return nothing ($query will be FALSE).
  • gen_Eric
    gen_Eric over 13 years
    @Trufa: What do you mean by 'So if I compare it to empty, I will know if the value exists'?
  • Trufa
    Trufa over 13 years
    I'm getting Parse error: syntax error, unexpected '{' in /home/xxx/public_html/xxx.com/index.php on line 11
  • Trufa
    Trufa over 13 years
    It says: "No database selected" why could this be?
  • Andrew
    Andrew over 13 years
    Add another ) before the { in the first line.
  • gen_Eric
    gen_Eric over 13 years
    Seems I missed a ). Should be ok now.
  • Trufa
    Trufa over 13 years
    Warning: mysqli_query() expects at least 2 parameters, sorry to bother!!
  • gen_Eric
    gen_Eric over 13 years
    @Trufa: You need to make sure you connect to the DB using mysql_connect first.
  • Trufa
    Trufa over 13 years
    @Rocket I am really sorrry I have to bother you again!! Parse error: syntax error, unexpected T_IF
  • gen_Eric
    gen_Eric over 13 years
    Oops, missed a ; after $db=mysqli_connect.
  • Trufa
    Trufa over 13 years
    Thank you, thank you!!! (+1 :) ) It's working right now, but I'm getting a warning: Warning: mysqli_result::fetch_assoc() [mysqli-result.fetch-assoc]: Couldn't fetch mysqli_result in ideas, sorry for all the inconvenience!!
  • Trufa
    Trufa over 13 years
    @Rocket any ideas on the warning? Again, sorry to bother!
  • gen_Eric
    gen_Eric over 13 years
    Change $query->fetch_assoc() to mysqli_fetch_assoc($result). See updated answer.
  • Trufa
    Trufa over 13 years
    @Rocket, I got a different one now: Warning: mysqli_fetch_assoc() [function.mysqli-fetch-assoc]: Couldn't fetch mysqli_result in I'm sorry I have to ask you everything, I have no idea what this errors mean, Thank you!!
  • gen_Eric
    gen_Eric over 13 years
    mysqli_free_result should be outside the while loop.
  • Trufa
    Trufa over 13 years
    @Rocket - Would hug you right now ;) All kidding aside, thanks for all your help and sorry for my ignorance!! Thanks again for the effort!
  • gen_Eric
    gen_Eric over 13 years
    @Trufa: I'll take that hug. It's ok, really, I'm here to help, and you're here to learn.
  • crh225
    crh225 about 11 years
    Should this be if ( == )
  • gen_Eric
    gen_Eric about 11 years
    @crh225: Actually, no. I'm not comparing $query to mysqli_query. I'm setting $query to mysqli_query, and checking the result of the assignment (=). If mysqli_query returns FALSE the if() won't run, otherwise it would.
  • Vulkan
    Vulkan about 9 years
    OK, I don't know why you people think this is correct but I tell you not only that is false but that is a dangerous answer...
  • gen_Eric
    gen_Eric about 9 years
    @user3737190: The only dangerous thing is the lack of escaping. This answer is 4 years old, so it's very out-of-date. The current answer would be to use prepared statements.