Jump to content

[SOLVED] Detect failed select


Jaguar

Recommended Posts

I want to insert a new mime type if the mime type does not exist or I want to get the existing mime type ID. I know this is a common problem, but I can't figure it out. I've tried using mysql_num_rows, but it causes an error.

 

$mime_type = mysql_escape_string($_FILES['file']['type']);

$result = mysql_query("SELECT id, mime_type FROM mime_types WHERE mime_type = $mime_type");

if( !$result )
{	
  mysql_query("INSERT INTO mime_types (id, mime_type) VALUES ('', '$mime_type')");
  $mime_type_id = mysql_insert_id();
}
else
{
  $row = mysql_fetch_assoc($result);
  $mime_type_id = $row['id'];
}

Link to comment
Share on other sites

<?php

  $mime_type = mysql_escape_string($_FILES['file']['type']);
  if ($result = mysql_query("SELECT id, mime_type FROM mime_types WHERE mime_type = $mime_type")) {
    if (!mysql_num_rows($result)) {
      mysql_query("INSERT INTO mime_types (id, mime_type) VALUES ('', '$mime_type')");
      $mime_type_id = mysql_insert_id();
    } else {
      $row = mysql_fetch_assoc($result);
      $mime_type_id = $row['id'];
    }
  }

?>

Link to comment
Share on other sites

Ok I figured a way. I think the problem was that I forgot quotes around WHERE mime_type = '$mime_type'

 

$result = mysql_query("SELECT id, mime_type FROM mime_types WHERE mime_type = '$mime_type' LIMIT 1");

if( mysql_num_rows($result) === 1 )
{
  $row = mysql_fetch_assoc($result);
  $thumbnail_mime_type_id = $row['id'];
}
else
{
  mysql_query("INSERT INTO mime_types (id, mime_type) VALUES ('', '$mime_type')");
  $mime_type_id = mysql_insert_id();
}

Link to comment
Share on other sites

You should be checking for errors, especially while debugging.  In this way you can detect and echo problems.

 

<?php

$query = "SELECT id, mime_type FROM mime_types WHERE mime_type = '$mime_type' LIMIT 1";
$result = mysql_query($query);
// Check for errors here and display any plus the query if there are:
if (mysql_errno()) die 'Error: ',mysql_error(),"<br>\n$query";

// I like mysql_result() for returning a single value; it saves a step or two:
if ($result) $thumbnail_mime_type_id = mysql_result($result,0);
else
{
// If your id column is AUTO_INCREMENT, you don't need to specify an id non-value
  mysql_query("INSERT INTO mime_types (mime_type) VALUES ('$mime_type')");
  $mime_type_id = mysql_insert_id();
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.