php - way to check for if sql row exists?

464

my problem is this

i am fetching a mysql row via this

                $sql_istorrenthere = $this->query_silent("SELECT media_type
FROM " . DB_PREFIX . "auction_media WHERE
                auction_id='" . $item_details['auction_id'] . "'"); 
$row = mysql_fetch_array($sql_istorrenthere);

and then calling it with this

if ($row['media_type'] == 4) 
{

        $display_output = GMSG_TORRENT;}
        else
        {
        $display_output = GMSG_NOTORRENT;
        }
}

however, media_type has multiple values, (1,2,3,4) how to write it so that it checks if 4 exists? because now i believe it is checking if media_type equals 4 and that is false, which is giving me the wrong display_output

202

Answer

Solution:

You can use mysql_num_rows to determine if any rows were returned, and this works by adding a search condition in your query adding " AND media_type = 4" to the end

if(mysql_num_rows($sql_istorrenthere)) {

} else {

}

// You can loop through records by doing the following, this prints out every media type :)
while ($row = mysql_fetch_array($sql_istorrenthere)) {
    echo $row['media_type'] . '<br />';
}
902

Answer

Solution:

You can just add on "AND media_type = '4'" to your query. But you really should use paramaterized queries.

Once your query has "AND media_type = '4'" you can check RowCount.

661

Answer

Solution:

There are probably better ways, but here's one idea.

$media_type_ids = explode(',', $row['media_type']);
if (array_search(4, $media_type_ids) !== FALSE) {
    // found
}

It could be possible to even do this in-situ in the database query ... potentially.

730

Answer

Solution:

// Comment the next line after you get what it is
@print ("Value of media type is: >>>".$row['media_type']."<<<"); // Line to be commented

if (isset($row['media_type']) && $row['media_type'] == 4) {
  $display_output = GMSG_TORRENT;
}
else {
  $display_output = GMSG_NOTORRENT;
}

To fetch all media types:

<?php
$sql_istorrenthere = $this->query_silent("SELECT media_type FROM " . DB_PREFIX . "auction_media"); 
while ($row = mysql_fetch_array($sql_istorrenthere)) {
// Comment the next line after you get what it is
  @print ("Value of media type is: >>>".$row['media_type']."<<<"); // Line to be commented

  if (isset($row['media_type']) && $row['media_type'] == 4) {
    $display_output = GMSG_TORRENT;
  }
  else {
    $display_output = GMSG_NOTORRENT;
  }

}

People are also looking for solutions to the problem: php - what's wrong with this SQL statement causing column count doesn't match value count at row 1?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.