Jump to content

date_format issue


mjurmann

Recommended Posts

Hey there. I'm trying to get the date_format in my SQL query, and then convert the date from the SQL query to PHP. The conversion works fine, and I get the correct information, but I've added a few LIKE arguments to me query, and I seem to be getting the error

 

"Warning: sprintf(): Too few arguments in /home/mjurmann/www/temp/test/htms/search-results.php on line 248

Query was empty"

 

Here is the relevent chunk of code:

 

$var = $_POST['site_search'];
$trimmed = trim($var); 
$trimmed_array = explode("+",$trimmed);
foreach ($trimmed_array as $trimm){
$query = sprintf("SELECT *, date_format(date_posted, '%%m/%%d/%%Y %%r') FROM news WHERE title LIKE '%$trimm%' OR article LIKE '%$trimm%' ORDER BY date_posted DESC", $trimm,$trimm); 
$rs = mysql_query ($query) OR DIE(mysql_error());
$row_num_links_main = mysql_num_rows($rs);
$row= mysql_fetch_array ($rs);

}

 

Can someone tell me what arguements I'm passing incorrectly? Thank you much in advance.

Link to comment
Share on other sites

Hi I think because you have put a % after the %$trimm it is expecting an argument for that one too, so try this

 

this is assuming $trimm is a string not a number

<?php
$var = $_POST['site_search'];
$trimmed = trim($var); 
$trimmed_array = explode("+",$trimmed);
foreach ($trimmed_array as $trimm){
$query = sprintf("SELECT *, date_format(date_posted, '%%m/%%d/%%Y %%r') FROM news WHERE title LIKE %s OR article LIKE %s ORDER BY date_posted DESC", $trimm,$trimm); 
$rs = mysql_query ($query) OR DIE(mysql_error());
$row_num_links_main = mysql_num_rows($rs);
$row= mysql_fetch_array ($rs);

}
?>

 

Link to comment
Share on other sites

Paul, I tried that, but the problem doesn't lie within LIKE '%$trimm%' OR article LIKE '%$trimm%' - that works fine.

 

The problem lies within the arguments at the end : $trimm,$trimm); I don't know if I'm putting the right information in there.

 

Can someone please help me? I have a deadline today... ahh :(

Link to comment
Share on other sites

the number of arguments at the end should match the number of single % signs in the string, %s being a string variable %u is an integer etc, if you place a % sign in front of another % sign it means for printf() to ignore it. so in your string

"SELECT *, date_format(date_posted, '%%m/%%d/%%Y %%r') FROM news WHERE title LIKE %s OR article LIKE %s ORDER BY date_posted DESC"

 

you only have two single % signs therefore the number of arguments should be 2, which you have

Link to comment
Share on other sites

Why us sprintf() here at all? Just create a query containing the needed values:

<?php
$query = "SELECT *, date_format(date_posted, '%m/%d/%Y %r') FROM news WHERE title LIKE '%$trimm%' OR article LIKE '%$trimm%' ORDER BY date_posted DESC";
?>

 

Ken

Link to comment
Share on other sites

Simple - if I don't use sprintf, no information will be echoed out

 

No information is echoed because you're not echoing anything in the code snippet you gave.

 

<?php
<?php
$var = $_POST['site_search'];
$trimmed = trim($var); 
$trimmed_array = explode("+",$trimmed);
foreach ($trimmed_array as $trimm){
        $query = "SELECT *, date_format(date_posted, '%m/%d/%Y %r') FROM news WHERE title LIKE '%$trimm%' OR article LIKE '%$trimm%' ORDER BY date_posted DESC";
$rs = mysql_query ($query) OR DIE("There was a problem with the query <pre>$query</pre><br>" . mysql_error());
$row_num_links_main = mysql_num_rows($rs);
if ($row_num_links_main > 0 ) { // added check if there are any rows
              $row= mysql_fetch_array ($rs);
              echo '<pre>' . print_r($row,true) . '</pre>';  //added echo of information
        }
}
?>

 

See what I added. Try this.

 

Also, this forum is not the best place to get quick answers when you're working under a deadline.

 

Ken

Link to comment
Share on other sites

I know that there was no info echoed in the snippet I gave, but I didn't include that in the post. I do have it echoed out, which is what I have below. No data is echoing out for the Date Posted though.....its blank.

 

<ul>
					<?php do { ?>
					  <li><span class="content_left_body_subsection_holder_left">
					  
					  <?php if ($row['thumbURL'] == null) {

			echo "<img src=\"../images/default_video.jpg\" width=\"60\" height=\"60\" border=\"1\" alt=\"GameArgus Thumb\"/>";} else { ?>
					  
					  <img src="../cms/htms/news_images/thumbs/<?php echo $row['thumbURL']; ?>" height="60" width="60" border="2" /> <?php } ?> </span><span class="content_left_body_subsection_holder_right">
			        <p><a href="news-page.php?id=<?php echo $row['id']; ?>&&authorId=<?php echo $row['authorId']; ?>"><?php echo $row['title']; ?></a></p> <p class="smallText11"><?php echo $row['teaser']; ?></p><p class="smallText11orange">Posted  <?php echo $row["date_format(date_posted, '%m/%d/%Y %r')"]; ?></p></span><img src="../images/subsection_left_col_line.jpg" /></li>
					  <?php } while ($row = mysql_fetch_assoc($rs)); ?></ul>

Link to comment
Share on other sites

Try the following:

<?php
$var = $_POST['site_search'];
$trimmed = trim($var); 
$trimmed_array = explode("+",$trimmed);
foreach ($trimmed_array as $trimm){
        $query = "SELECT *, date_format(date_posted, '%m/%d/%Y %r') as date_formated FROM news WHERE title LIKE '%$trimm%' OR article LIKE '%$trimm%' ORDER BY date_posted DESC";
echo 'Query is: ' . $query . '<br />';  // debug statement
$rs = mysql_query ($query) OR DIE("There was a problem with the query <pre>$query</pre><br />" . mysql_error());
$row_num_links_main = mysql_num_rows($rs);
echo 'Number of rows found: ' . $row_num_links_main . '<br />'; // debug statement
if ($row_num_links_main > 0 ) { // added check if there are any rows
              while ($row = mysql_fetch_array ($rs)) {
                      echo '<ul>';
	      echo '<li><span class="content_left_body_subsection_holder_left">';
	      if ($row['thumbURL'] == null) {
	            echo '<img src="../images/default_video.jpg" width="60" height="60" border="1" alt="GameArgus Thumb"/>';
	      } else
	            echo '<img src="../cms/htms/news_images/thumbs/' . $row['thumbURL'] .'" height="60" width="60" border="2" />';
	echo '</span><span class="content_left_body_subsection_holder_right">';
	echo '<p><a href="news-page.php?id=' . $row['id'] . '&authorId=' . $row['authorId'] .'"><' . $row['title'] . '</a></p> <p class="smallText11">' . $row['teaser'] . '</p><p class="smallText11orange">Posted  ' . $row["date_formated"] . '</p></span><img src="../images/subsection_left_col_line.jpg" /></li>' ;
	echo '</ul>';
        }
        }
}
?>

 

You'll notice I put in two debug statement and I changed the "do .... while" loop to a plaing "while" loop.

 

Ken

Link to comment
Share on other sites

Still the same issue - I'm getting info echoed back from the query- however, the date_posted is not parshing the date and I'm not seeing anything for the Date Posted: field in the layout.

 

I do this a lot in my site, and I use sprintf, so I know the problem lies within that. I just need to know what I'm doing wrong with that...

Link to comment
Share on other sites

How does your query look when you echo it?

 

Just because you've always used sprintf() in the past, doesn't mean you always need to use it. But if you insist on using it, try this:

<?php
$query = sprintf("SELECT *, date_format(date_posted, '%%m/%%d/%%Y %%r') FROM news WHERE title LIKE '%%%s%%' OR article LIKE %%%s%%' ORDER BY date_posted DESC", $trimm,$trimm);
?>

 

This assumes that the value of the variable "$trimm" does not contain the "%" characters.

 

Ken

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.