Jump to content

[SOLVED] sprintf


Ninjakreborn

Recommended Posts

http://us.php.net/sprintf

Can someone please explain this to me.

I saw a format I never saw before, it looked like the following.

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO books (author, name, link, category) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['author'], "text"),
				   GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['link'], "text"),
                       GetSQLValueString($_POST['description'], "text"),
                       GetSQLValueString($_POST['holdem'], "int"),
				   GetSQLValueString($_POST['razz'], "int"),
                       GetSQLValueString($_POST['omaha'], "int"),
                       GetSQLValueString($_POST['omahahilo'], "int"),
                       GetSQLValueString($_POST['stud'], "int"),
				   GetSQLValueString($_POST['studhilo'], "int"),
                       GetSQLValueString($_POST['draw'], "int"),
                       GetSQLValueString($_POST['other'], "int"),
                       GetSQLValueString($_POST['tournaments'], "int"),
				   GetSQLValueString($_POST['limit'], "int"),
                       GetSQLValueString($_POST['nolimit'], "int"),
                       GetSQLValueString($_POST['potlimit'], "int"),
                       GetSQLValueString($_POST['year'], "text"),
				   GetSQLValueString($_POST['all'], "int"));
				   
  $Result1 = mysql_query($insertSQL,$dbc) or die(mysql_error());
}
?>

I understand most of that.  It's a query it's just formatted a way I never noticed.

Can someone exlpain the part about sprintf() and what it's doing.

I don't understand it's purposes.

Link to comment
https://forums.phpfreaks.com/topic/45555-solved-sprintf/
Share on other sites

Its simple. The first argument of sprintf takes a string with placeholders in it. each subsequent argument will be placed within those placeholders. eg;

 

echo sprintf("hello %s, who are you?","foo");

 

As you can see in the manual entry for sprintf there are many different types (formats) of placeholders available.

 

Using sprintf for queries is particuly usefull, the placeholder %s for instance will only accept a string. This make cleaning your input pretty simple.

Link to comment
https://forums.phpfreaks.com/topic/45555-solved-sprintf/#findComment-221181
Share on other sites

So it's basically along the same line's as search and replace, but in a standardized format.

For example

So the first argument is going to be the original message, then the second argument basically find's/replaces them into the slots.

Is it subsequent for each one (in order).

sprintf("Hello, today we %s are going to %s and fall into the %s", "going", "try", "water");

That will basically create

Hello, today we are going to try and fall into water.  If this is true then I understand it's use.  However I still don't understand it's purpose, you mentioned "convenient" meaning "suited to your comfort or purpose or needs, easy to reach" which would mean it's an easier way to do queries.

When I looked over the programming from this other developer and saw this, I was wondering what it was and now I understand.  However I don't fully understand the different in using this over standard queries, or using this over OOP for standard queries.

Is it mostly just related to personal style, or are there other reason's why this is considered "convenient".  Thank you so far for the advice.

Link to comment
https://forums.phpfreaks.com/topic/45555-solved-sprintf/#findComment-221231
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.