Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 27th, 2008, 10:04 PM
Authorized User
 
Join Date: Sep 2003
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Default Wood from the trrees

I am having a problem with a field a mysql table that can be any single letter or a Null ie: nothing entered
The field default is Not Null and is called sufix

My code at present looks like this

Code:
<?php
$tble = 'mytable;
$modnum =  30201;
$suff = '';  // NULL but can be A or B 

include_once"../includes/My_conn.php";
   $connect = mysqli_connect($host,$account,$password) OR DIE("Error !! Unable to connect to database");
    $db = mysqli_select_db($connect,"$dbname") OR DIE( "Unable to select database ");
    $db = "SELECT * FROM $tble WHERE model = $modnum ";
   if (!$suff) {
             $db .= "AND sufix = '$suff'";
    }
    if ($result = mysqli_query($connect,$db)) {  
        if (mysqli_num_rows($result)) {
                while ($row = mysqli_fetch_assoc($result)){

?>
<table>
-
-
-
-
</table>

<?php
}
}
}
?>

As is. It finds the record with the null as expected
By it goes wrong when I put in A or B when it then shows all three records

I need it to just show the one selected by $suff
 if I change this line
   if (!$suff) {

to

   if ($suff) {

then it finds the A or B but prints all 3 when I select NULL

Please help is needed

Thanks
Roy..
 
Old March 3rd, 2008, 12:58 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Forgive me if I understand incorrectly, but basically you're trying to retrieve rows from a table that match a certain model number and suffix?

If this is correct, why are you conditionally including the suffix in the query? Why not remove the if() clause completely and just add the suffix specification directly to the query like this...

Code:
$db = "SELECT * FROM $tble WHERE model = $modnum AND sufix = '$suff'";
Is this what you are after?

hth
-snib









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.