Solved

Creating PHP printf string with database variables

Posted on 2004-10-24
543 Views
Last Modified: 2013-12-12
I need to build into my currently functioning printf() string a variable pulled from my mySql table.

Here's the existing code:
<?php
$atsign="@";
$today = Date("Y-m-d");
$startdt = date('Y-m-d', strtotime('-5 days'));

$db = mysql_connect("localhost", "root");
mysql_select_db("fullinfo",$db);
$sql = "SELECT SUBSTRING_INDEX(wemail,'@',1) as 'leftemail',SUBSTRING_INDEX(wemail,'@',-1) as 'rightemail',SUBSTRING_INDEX(wemail,'.',-1) as 'dotcom',date_format(wdate,'%b %e') as 'thedate', DATE_FORMAT(wdate,'%Y-%M-%e') as passdate,wstate FROM w100 where wdate>='$startdt' and wdate<'$today' order by wdate desc";
$result = mysql_query($sql,$db) or die("Couldn`t execute query");

if ($myrow = mysql_fetch_array($result)) {

  do {

    printf("<tr><td height=24><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></td><td><a href='/cgi-bin/tixcheck.cgi?t=XXX target='_blank'><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></a></td></tr>\n", $myrow["thedate"], substr($myrow["leftemail"],0,1)."____$atsign".substr($myrow["rightemail"],0,1)."_____.".$myrow["dotcom"]);

  } while ($myrow = mysql_fetch_array($result));

}

?>

The above works correctly.  Now I need to replace the XXX in the a href string with $myrow["passdate"] variable pulled from my table.

How can this be done?
0
Question by:kkbenj
    8 Comments
     
    LVL 10

    Assisted Solution

    by:frugle
       printf("<tr><td height=24><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></td><td><a href='/cgi-bin/tixcheck.cgi?t=%s target='_blank'><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></a></td></tr>\n", $myrow["thedate"], $myrow["passdate"], substr($myrow["leftemail"],0,1)."____$atsign".substr($myrow["rightemail"],0,1)."_____.".$myrow["dotcom"]);


    unless I'm mistaken.

    Mike
    0
     
    LVL 7

    Expert Comment

    by:BenMorel
    It seems correct to me.
    Ben
    0
     

    Author Comment

    by:kkbenj
    I used this:
     printf("<tr><td height=24><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></td><td><a href='/cgi-bin/tixcheck.cgi?t=%s target='_blank'><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></a></td></tr>\n", $myrow["thedate"], $myrow["passdate"], substr($myrow["leftemail"],0,1)."____$atsign".substr($myrow["rightemail"],0,1)."_____.".$myrow["dotcom"]);

    and it gave an error:
    Parse error: parse error, unexpected T_STRING in ctop10.php on line 139 (this is the printf line)

     So I added the ' to make it:
        printf("<tr><td height=24><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></td><td><a href='/cgi-bin/tixcheck.cgi?t='%s target='_blank'><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></a></td></tr>\n", $myrow["thedate"], $myrow["passdate"], substr($myrow["leftemail"],0,1)."____$atsign".substr($myrow["rightemail"],0,1)."_____.".$myrow["dotcom"]);

    This got rid of the error, but the date is not part of the link URL.

    What am I missing?
    0
     
    LVL 7

    Accepted Solution

    by:
    This is correct :

    printf("<tr><td height=24><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></td><td><a href='/cgi-bin/tixcheck.cgi?t=%s' target='_blank'><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></a></td></tr>\n", $myrow["thedate"], $myrow["passdate"], substr($myrow["leftemail"],0,1)."____$atsign".substr($myrow["rightemail"],0,1)."_____.".$myrow["dotcom"]);

    It was : =%s'
    And not :f ='%s

    Regards,
    Ben
    0
     

    Author Comment

    by:kkbenj
    I split the points because the first solution was missing the single quotation that I needed.

    Thanks to both of you!
    0
     

    Author Comment

    by:kkbenj
    How can I add
    onmouseover="window.status=' ';return true;"
    to this string?
    0
     
    LVL 7

    Expert Comment

    by:BenMorel
    I think this is what you want :

    printf("<tr><td height=24><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></td><td><a href='/cgi-bin/tixcheck.cgi?t=%s' target='_blank' onmouseover=\"window.status=' ';return true;\"><font style='font-family:arial,helvetica,sans-serif;color: #335399;font-size: 12px;'>%s</font></a></td></tr>\n", $myrow["thedate"], $myrow["passdate"], substr($myrow["leftemail"],0,1)."____$atsign".substr($myrow["rightemail"],0,1)."_____.".$myrow["dotcom"]);

    (You need to replace " with \")
    0
     

    Author Comment

    by:kkbenj
    Perfect!

    You are fantastic!!!!!!!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    SQL injection vulnerabilities have been described as one of the most serious threats for Web applications. Web applications that are vulnerable to SQL injection may allow an attacker to gain complete access to their underlying databases. Because…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now