%s在这里意味着什么?

时间:2011-04-16 13:43:23

标签: php mysql sql printf

%s是什么意思?

$sql = "SELECT * 
          FROM page_table 
         WHERE page_name = '%s' 
         LIMIT 1";

5 个答案:

答案 0 :(得分:9)

这是一个格式化字符串,其中%s是占位符。我怀疑$ sql被传递给sprintf以将其转换为真实的查询。例如:

$name = 'posts';
$sql = "SELECT * FROM page_table WHERE page_name = '%s' LIMIT 1";
$formattedSql = sprintf($sql, $name);

这将生成如下查询:

SELECT * FROM page_table WHERE page_name = 'posts' LIMIT 1

当你不想弄乱引号和双引号时,这非常有用。

答案 1 :(得分:1)

%s是在sprintf等函数中使用的占位符。查看手册以了解其他可能的占位符。

$sql = sprintf($sql, "Test");

这将用字符串“Test”替换%s。它还用于确保传递的参数实际上适合占位符。您可以使用%d作为数字占位符,但如果sprintf收到字符串,则会抱怨。

答案 2 :(得分:1)

我猜%s是sprintf的格式符号。

下一行代码可能是这样的:

$sqlquery = sprintf($sql, 'mySearchString');

真实查询将是:

  

SELECT * FROM page_table WHERE page_name ='mySearchString'LIMIT 1

答案 3 :(得分:0)

我们使用%s作为字符代码,因为ASCII 65是'A'所以直到91'Z'才能更好地理解你可以使用for循环从65到91开始在这个for循环打印结果中使用printf

for($i=65; $i<91; $i++){ printf('%s ', chr($i)); }

上面会打印

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

答案 4 :(得分:0)

 public class BuyerFaqsAdapter extends RecyclerView.Adapter<BuyerFaqsAdapter.MyViewHolder> {

private ArrayList<BuyerMemberShipFAQParent> brandList;

private Context context;
private int counter;


class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView tvans,tvfaq;
    public ImageView ivImage;

    MyViewHolder(View view) {
        super(view);
        tvfaq = (TextView) view.findViewById(R.id.row_layout_faqtv);
        tvans = (TextView) view.findViewById(R.id.row_layout_answertv);
        ivImage = (ImageView) view.findViewById(R.id.row_layout_ivarrow);
    }
}

public BuyerFaqsAdapter(final Context context, final ArrayList<BuyerMemberShipFAQParent> brandList) {
    this.brandList = brandList;
    this.context = context;

}

@Override
public BuyerFaqsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_layout_faq_expand_parent, parent, false);


    return new BuyerFaqsAdapter.MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final BuyerFaqsAdapter.MyViewHolder holder, final int position) {
    holder.tvfaq.setText(brandList.get(position).getFAQParentText());
    holder.tvans.setVisibility(View.GONE);
    holder.tvans.setText(brandList.get(position).getFAQAnswerText());
    holder.itemView.setId(position);
    counter = 1;
    holder.ivImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (counter == 1) {
                holder.tvans.setVisibility(View.VISIBLE);
                holder.ivImage.setImageResource(R.drawable.ic_arrow_faq_up);
                counter = 2;
            }
            else {
                holder.tvans.setVisibility(View.GONE);
                holder.ivImage.setImageResource(R.drawable.ic_arrow_faq);
                counter = 1;
            }
        }
    });
}

@Override
public int getItemCount() {
    return brandList.size();
}

会给你这样的东西。

$sql = "SELECT * 
          FROM page_table 
         WHERE page_name = '%s' 
         LIMIT 1";
$sql = sprintf($sql, "page");