mjurmann Posted February 16, 2007 Share Posted February 16, 2007 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 https://forums.phpfreaks.com/topic/38780-date_format-issue/ Share on other sites More sharing options...
paul2463 Posted February 16, 2007 Share Posted February 16, 2007 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 https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186366 Share on other sites More sharing options...
mjurmann Posted February 16, 2007 Author Share Posted February 16, 2007 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 https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186400 Share on other sites More sharing options...
paul2463 Posted February 16, 2007 Share Posted February 16, 2007 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 https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186417 Share on other sites More sharing options...
mjurmann Posted February 16, 2007 Author Share Posted February 16, 2007 And I have 2 arguements at the end - so where am I going wrong? Link to comment https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186419 Share on other sites More sharing options...
mjurmann Posted February 16, 2007 Author Share Posted February 16, 2007 And I was saying that when I use %s instead of '%$trimm%' the query does not return anything, so using '%$trimm%' is correct, its just the two arguments at the end that might be wrong... Link to comment https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186421 Share on other sites More sharing options...
kenrbnsn Posted February 16, 2007 Share Posted February 16, 2007 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 https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186424 Share on other sites More sharing options...
mjurmann Posted February 16, 2007 Author Share Posted February 16, 2007 Simple - if I don't use sprintf, no information will be echoed out Link to comment https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186441 Share on other sites More sharing options...
mjurmann Posted February 16, 2007 Author Share Posted February 16, 2007 Anyone? I'm sure the answer is simple! I can't seem to find the right answer on the boards or Google... Link to comment https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186456 Share on other sites More sharing options...
mjurmann Posted February 16, 2007 Author Share Posted February 16, 2007 Anyone have a solution to this? I only have 2 hours left until I have to have this done Link to comment https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186613 Share on other sites More sharing options...
kenrbnsn Posted February 16, 2007 Share Posted February 16, 2007 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 https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186620 Share on other sites More sharing options...
mjurmann Posted February 16, 2007 Author Share Posted February 16, 2007 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 https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186651 Share on other sites More sharing options...
kenrbnsn Posted February 17, 2007 Share Posted February 17, 2007 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 https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-186805 Share on other sites More sharing options...
mjurmann Posted February 17, 2007 Author Share Posted February 17, 2007 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 https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-187580 Share on other sites More sharing options...
kenrbnsn Posted February 18, 2007 Share Posted February 18, 2007 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 https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-187641 Share on other sites More sharing options...
mjurmann Posted February 18, 2007 Author Share Posted February 18, 2007 ken - thank you so much!!!!!! Link to comment https://forums.phpfreaks.com/topic/38780-date_format-issue/#findComment-187804 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.