SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question PHP: results missing some values with SQL query

    Hi there,

    I am new to PHP ans MySQL. Please bear with me.

    I have a simple PHP script to display the result of an SQL query. The results shows empty
    for some fields, not others, even if all fields
    have valid value. For example, I have total of
    80 records, 10 of them show empty for the firstname and lastname fields. I know that these
    10 records all have valid values in the firstname and lastname fields. Any idea what's going on?
    Is the problems with my PHP code or with mySQL
    configuration ( when I used UNIX command 'mysql' for the same query, I got correct results)?

    I have enclosed the part of the script for your reference. Please help me.

    <?php

    $dbcnx = mysql_connect("localhost", "root", "myfakepswd");
    mysql_select_db("seminardb");

    // The basic SELECT statement
    $select = "SELECT * ";
    $from = " FROM seminars";
    $where = " WHERE id > 0";

    if ($aid != "") { // An author is selected
    $where .= " AND ID=$aid";
    }

    if ($firstname != "") { // firstname is selected
    $where .= " AND firstname LIKE '%$firstname'";
    }

    if ($lastname != "") { // lastname is selected
    $where .= " AND lastname LIKE '%$lastname%'";
    }

    if ($absid != "") { // An abstract is selected
    $where .= " AND id=$absid";
    }

    if ($searchtext != "") { // Some search text was specified
    $where .= " AND title LIKE '%$searchtext%'";
    }

    $where .=" order by datestr desc";

    ?>
    <table bgcolor="#000000" width=450 cellpadding=3 cellspacing=1 align="center" border="0">

    <?php
    $entries = mysql_query($select . $from . $where);
    if (!$entries) {
    echo("</table>");
    echo("<p>Error retrieving seminars from database!<br />".
    "Error: " . mysql_error() . "</p>");
    exit();
    }

    $i=0;
    while ($entry = mysql_fetch_array($entries)) {
    $id = $entry["id"];
    $sol=htmlspecialchars($entry["solute"]);
    $first=htmlspecialchars($entry["firstname"]);
    $last=htmlspecialchars($entry["lastname"]);
    $middle=htmlspecialchars($entry["middlename"]);
    $position=htmlspecialchars($entry["position"]);
    $affil=htmlspecialchars($entry["affiliation"]);
    $seminartitle = htmlspecialchars($entry["title"]);
    $datestr=htmlspecialchars($entry["datestr"]);
    $timestr=htmlspecialchars($entry["timestr"]);
    $location=htmlspecialchars($entry["location"]);
    $abs=htmlspecialchars($entry["abstract"]);

    list($year,$month,$day)=sscanf($datestr,"%d-%d-%d");

    $jday=GregorianToJD($month, $day, $year);
    $mthname=jdmonthname($jday, 0);
    $wkday=jddayofweek($jday, 2);

    $text="<p>$wkday, $mthname $day, $year, ";
    if ( ! strstr($timestr, '10:') ) {
    $text .="<font color=\"#ff0000\">$timestr </font>, "." $location</p>";
    } else {
    $text .="$timestr, "." $location</p>";

    }
    if ( !($sol) ) {
    $text="$text"."<p>$sol"." $first"." $middle"." $last,";
    } else {
    $text="$text"."<p>$firstname"." $middlename"." $lastname,";
    }

    $text="$text"." $affil</p>";
    if ( $abs ) {

    $text="$text"."<p><a href='semi.php?absid=$id'>$seminartitle</a></p>";
    } else {
    $text="$text"."<p>$seminartitle</p>";
    }

    if ( $absid == $id ) {
    $abs=chop($abs)."<br>";
    $text .= "<p>$abs</p>";
    };

    echo("<tr valign='baseline'> ");
    if ( $i ) {
    echo("<td bgcolor='#eeeeee'>$text</td>\n");
    $i=0;
    } else {
    echo("<td bgcolor='#eeeeff'>$text</td>\n");
    $i=1;
    }
    echo("</tr>\n");
    }
    ?>

    </table>

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firstly, if you post your code in [ php ] [ /php ] tags (without the spaces) it will make it a lot easier to read

    Try changing:

    PHP Code:
    $where .= " AND firstname LIKE '%$firstname'"
    To:

    PHP Code:
    $where .= " AND firstname = '$firstname'"
    and do the same for lastname

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •