Jump to content

[SOLVED] problem ahoy! SELECT statement again.


bioTINMAN

Recommended Posts

the select statement, given under, is throwing up problems. The $value is not being executed at the last statement (red colored one), even when there is no value to return. However, mysql_fetch_assoc gives out an error as expected.  I cant figure out where i went wrong.

 

 

<?php

 

include_once('connect.php');

 

$var=$HTTP_POST_VARS['search_choose'];

 

$enter=$HTTP_POST_VARS[$var];

 

$enter1=$var."='".$enter."'"; // this is so that i get a value like val='1234'

 

echo $var,$enter,$enter1;

 

 

if($var=='search_city' || $var=='search_city1')

{

 

$value=mysql_query("SELECT sub_number, name, address, country, email, Phone FROM personal WHERE city='$enter'");

 

}// if stops here

 

 

 

else

if($var=='sub_number' || $var=='name' || $var=='address' || $var=='country' ||$var=='state' || $var=='email' ||

 

$var=='Phone')

{

$value=mysql_query("SELECT sub_number, name, address, country, email, Phone FROM personal WHERE $enter1");

}// else if stops here

 

//checking whether the query has returned true or false

 

 

if(!$value) 

echo "query not found"; // there is no execution here

 

if(!$check_in=mysql_fetch_assoc($value))//this statement executes. why?

echo "nope, no results";

 

 

?>

Link to comment
Share on other sites

it works if i test the fetch assoc statement itself:

 

if(!$check_in=mysql_fetch_assoc($value))

echo "nope, no results";

 

but it doesnt work this way:

 

if(!$value)

echo "nope, no results";

 

and btw after i had to do a little modification to your statement:

 

$check_in=mysql_fetch_assoc($value);

if(count($check_in) <=1)

echo "nope, no results";

 

what is the 1 thing that is being returned?

Link to comment
Share on other sites

sorry it should be <0

1 is the first row returned by fetching your db records.. note that fetch array returns single row so if you need more rows or you expect more rows you need to loop your array fetc eg..

 

while($row=mysql_fetch_array(resourcevalue here)){}

Link to comment
Share on other sites

i didnt get you. Even if there are no results for that particular query, the if(!$value) gets executed.

 

and your statement, doesnt show anything when there is no result.

 

i mean to say, if i have a query like this one below:

 

$value=mysql_query("SELECT sub_number, name, address, country, email, Phone FROM personal WHERE city='london'");

 

but even though the database has no entry for london, its executing if($value) or even your (teng84) statements.

 

your statement is executed only when:

 

$check_in=mysql_fetch_assoc($value);

if(count($check_in) >0)

echo "nope, no results";

 

so its returning something, but the fact remains there is nothing to return! i am only able to make get teh correct reply with if(!$check_in=mysql_fetch_assoc($value))

 

another person on the net also has the same problem : http://www.webmasterworld.com/php/3444616.htm i have no idea whats wrong. do you think i have to do tweaks the mysql side? or is it plainly that im not getting what your trying to convey

Link to comment
Share on other sites

You want to check a couple of things...

 

Did your query succeed?

 

If so, did it contain any records?

 

These are two different things... an unsuccessful query dictates a MySQL connection problem, while an empty record set simply means nothing was found per your criteria (but the connection was successful).

 

So, let's create a string for the query, then test for a successful connection. Then we will see if any records are returned:

 

<?php
if ( $var == 'search_city' || $var == 'search_city1' ) {

    $sql = "SELECT `sub_number`, `name`, `address`, `country`, `email`, `Phone` FROM `personal` WHERE city='$enter'";
    if ( !$result=mysql_query($sql) ) { // did the connection with MySQL work?
        die('MySQL Error: ' . mysql_error());
    }
    if ( !mysql_num_rows($result) > 0 ) { // Is there one or more records?
        $nothing = 'No Records Found...';
    } else {
        // just throwing out records here... you would format your output in this section how you see fit.
        while ( $row = mysql_fetch_assoc($result) ) {
            echo "<pre>";
            print_r($row);
            echo "</pre><br>";
        }
    } 
}// if stops here

 

PhREEEk

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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