5

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!

2
  • So, you want to return a row if a value is NOT in the table? I'm confused. Commented Jan 27, 2011 at 20:16
  • @Rocket Sorry for the HUGE typo!! Edited my question. Commented Jan 27, 2011 at 20:18

6 Answers 6

9

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);
}
Sign up to request clarification or add additional context in comments.

20 Comments

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?
If it doesn't find anything, it'll return nothing ($query will be FALSE).
@Trufa: What do you mean by 'So if I compare it to empty, I will know if the value exists'?
I'm getting Parse error: syntax error, unexpected '{' in /home/xxx/public_html/xxx.com/index.php on line 11
Add another ) before the { in the first line.
|
1

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

2 Comments

It says: "No database selected" why could this be?
@Trufa: You need to make sure you connect to the DB using mysql_connect first.
1

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;
}

Comments

1

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.

1 Comment

Yes, this is correct, I want to echo the header column if your search value matches your var column.
0

You just want to know if $var's value is anywhere in that column (any row(s))?

SELECT COUNT(id) FROM data WHERE var = ?;

The result will be the number of rows for which the field var contains the value of $var.

Comments

0

Here's a template for all the "does it exist" questions.

This is the only thing that actually worked for me so far and is not deprecated.

if ($query = mysqli_query($link, "SELECT header FROM data WHERE var = '$foo'")) {

        $header = mysqli_fetch_assoc($query);

        if ($header) {

            // The variable with value $foo exists.
        }
        else {

            // The variable with value $foo doesn't exist.
        }
    }
    else {

        // The query didn't execute for some reason. (Dammit Obama!)
    }

WARNING!

Even if the variable DOES NOT EXIST the comparison between $query and mysqli_query() will always return TRUE.

The only way --which happened to me-- for the comparison to return FALSE is because of a syntax error in your query.

I don't know why it worked for the guy who wrote the accepted answer, maybe it's an update or maybe he had a syntax error and was so confident that he didn't check if it could ever be TRUE.

Here's the comment someone made for correcting his syntax:

"Add another ) before the { in the first line"

So, the accepted answer is WRONG!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.