Jump to content

Zane

Administrators
  • Posts

    4,362
  • Joined

  • Last visited

  • Days Won

    11

Posts posted by Zane

  1. There is no possible way of doing this with a fetch function. They were created explicitly to fetch the data from the dataset. I suppose you could get away with using a for loop and use mysql_num_rows for your threshold, but that is just ridiculous.

     

    My advice to you is to not stray from the fetch approach. Since you say you plan to select different datasets and use them outside the loop, it is probably the best idea to create an array for each of your datasets... for instance

     

    $query = "SELECT * FROM labor_rates";
    $go[] = mysql_query($query, $connection);
    
    $query = "SELECT * FROM ed_scores";
    $go[] = mysql_query($query, $connection);
    
    $query = "SELECT * FROM someTable";
    $go[] = mysql_query($query, $connection);
    
    $laborRates = array(); $ed_scores = array(); $someTable = array();
    
    while($row = mysql_fetch_array($go[0])) $laborRates[] = $row;
    while($row = mysql_fetch_array($go[1])) $ed_scores[] = $row;
    while($row = mysql_fetch_array($go[2])) $someTable[] = $row;
    
    echo "<b>$someTable[3]['name']</b>"; 

    Then you can use a foreach to loop through those .. or you can access your data directly like I demonstrated at the end.

  2. The fact that I am using a maxlength=8 option in my text box would most likely keep anything from ever happening malicious?

     

    HTML is not security nor is Javascript. They are both client side languages, meaning they can be changed by the user as they see fit. There are plugins out there to do just that, they are also used as debugging tools. An example is Firebug for Firefox. With it, anyone can see where the data is going, which PHP files are being included, they can change the HTML .. in a live fashion, they can execute Javascript commands live as well. Do not fret though and consider Firebug a threat. It is a very very useful tool in web development.

     

    Psycho answered your question the best... though he didn't exactly elaborate on it the best, it is indeed the answer to your question

     

    Sanitize, filter, escape, sanitize, etcetera. YOU are in complete control of what goes into your database, the same way as you are in complete control of what foods you ingest. The key here is knowledge.

     

    If you want to make sure nothing bad gets into your system, you will have to code on the side of your system... known as server-side....AKA PHP. Here are a few PHP functions to start you off..

     

    http://www.php.net/mysql_real_escape_string

    http://www.php.net/trim

    http://www.php.net/filter_var

     

    IMO, you're pretty safe with just mysql_real_escape_string. It is the only function that actually sanitizes the input for database entry. The other two are simply a way to reject unwanted things... like extra spaces ... or a malformed email address.

  3. They all look compatible to me.. Quite a beefy (albeit expensive) computer though.

     

    Wouldn't have hurted to post what each link goes to by the way. The most important link is the motherboard and you have it as the fourth one down.

     

     

    You may want to get a CD/DVD drive as well if you want to put an OS on it. ;) Although I'm sure you omitted it because, well, any kind would be compatible... just pointing it out though.

  4. There's no reason to store whether a restaurant is open or closed when you already have the open and close times in there. Using SQL you can tell whether it is open or close with the TIME() BETWEEN syntax combined with a CASE statement.

     

     

    SELECT `r_ofrom` , `r_oto` , `rid` ,
    CASE WHEN (TIME(NOW()) BETWEEN r_ofrom AND r_oto) THEN true
    ELSE false
    END as is_open
    FROM `restaurant`

    Then you will not have to do a double query... and the open/close value will be in something like $row['is_open']

     

    Using a ternary statement, in PHP, you can display text or images to show its status...

    echo $row[´r_name´] . " is " . $row['is_open'] ? "open" : "closed";

  5. Yes, but only if you destroy the session after 15 minutes of inactivity. Otherwise, users would be declared as online/active until they click logout or they close the browser, then you would have to make sure any cookies are destroyed as well therefore they could be assumed online/active for quite some time... depending on the expiry settings you have for your sessions and cookies.

     

    Storing it in the database would allow you to create a list of who is online/active. Without the use of a database, you would have to populate some external file on every page load with a list of who is online or offline..

  6. The function only acts as a convenience, there is no real benefit other than that.  In my opinon, if you use extract you will loose track of where your variables come from.  When I am coding I prefer to know whether it is a POST, SESSION, COOKIE, SERVER or GET variable whenever and wherever I use it.... unless I am creating a new variable with arithmetic purposes....

     

    Such as 

    $e = pow(($_POST['m'] * $_POST['c']), 2);

  7. If you posted all of your code we could help you more with suggestions.. I do not see in your code anywhere where you are defining colors or setting the background.  Are you using CSS classes or what?..

     

    Looking at your very first snippet of code I would immediately suggest the use of a switch, but then you mention that arrays have helped you out more now... I am sure there is an easier solution to what you are doing becaue you are doing nothing more than checking a value....after some apparent arithmetic that you have decided not to show (which is probably best seeing as you are only asking help with the structure and trimming the code down) but I am guessing you already have that part down pat.

  8. Hmm.. well I guess you dont need to use is_null...it seems that when you put just the variable in the condition is checks for a non-null value or a true value.  Though I do still think you should implode with OR rather than AND, but you can figure that out on your own...

  9. In the code your posted earlier, you have your variables defaulting to null if they are not set, so it is null you should check for when you a creating your conditions..

     

    It also wouldn't hurt to just put all of your condition strings into an array and then implode them with AND... so you don't end up with a trailing AND. Actually, I think it would be better if you used OR, then again I am just writing this to be writing it....

    I thought that the following code meant if true do this. It appears I am wrong, how can I fix this?

     

                   if($search==1){
                    $sqlCondition = $sqlCondition." WHERE CDTitle LIKE '%$search%'";
                   }
    

     

    You would use the triple equals sign to check for true like that

    $search === 1

     

    But like I said earlier, you defaulted all of your variables to null if they are not set so none of them will ever be "true".  You need to use if(!is_null($variable))

     

    Here is how you would do it with an array

    // Create variable for your WHERE conditions
    $where = array();
    if(!is_null($search)) $where[] = "CDTitle LIKE '%$search%'";
    if(!is_null($searchCDID)) $where[] = "CDID LIKE '%$searchCDID%'";
    if(!is_null($searchPubID)) $where[] = "pubID LIKE '%$searchPubID%'";
    // etcetera etcetera
    
    $sql = "SELECT nmc_cd.CDID, nmc_cd.CDTitle, nmc_cd.CDYear, nmc_cd.CDPrice, nmc_cd.pubID, nmc_cd.catID, nmc_publisher.pubName, nmc_publisher.location, nmc_category.catDesc
              FROM nmc_cd
              INNER JOIN nmc_publisher ON nmc_cd.pubID = nmc_publisher.pubID
              INNER JOIN nmc_category ON nmc_cd.catID = nmc_category.catID";
    $sql .= " WHERE " . implode(" OR ", $where);
    mysql_query($sql) or die(mysql_error() . "\r\n" . $sql);
    

     

    It will also be important to check the size of the $where array before you even run the query, otherwise, you will get ALL the records.... or actually you will get an SQL error for the empty WHERE clause.

×
×
  • 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.