简体   繁体   中英

Query returning no results

This is the link the sends the $Row[pk_tId] to javascript:

    <a class=\"open-EditRow btn btn-primary btn-mini\" data-toggle=\"modal\" href=\"#myEditModal\" data-id=\"".$Row[pk_tId]."\" title=\"Edit this row\" \">Delete/Edit</a></td>";

This is the javascript that sends $Row[pk_tId] as groupId to the modal (on the same page):

   $(document).on("click", ".open-EditRow", function () {
     var myGroupId = $(this).data('id');
     $(".modal-body #groupId").val( myGroupId );
   });

This is the input field inside the modal that prints groupId:

   $id = "<input type=\"text\" name=\"groupId\" id=\"groupId\" value=\"\" />";

I echo out the $id:

   echo "Your id is: " . $id;

But when I try to select the $id to pull the record out of the database, it returns no records. The record is there. Here is the statement:

   $q = "SELECT * FROM restable WHERE pk_tId == '" . $id . "'";
   if (mysql_num_rows($q) == 0){
        echo "No results";
    }

The only thing I get is "No results." I have a steady connection to the database. Why isn't this statement returning anything? What am I missing? Please help.

You are not doing the actual query:

   $q = "SELECT * FROM restable WHERE pk_tId = '" . $id . "'";
   $query = mysql_query($q); // <- you forgot this line, $q is only a string
   if (mysql_num_rows($query ) === 0){
        echo "No results";
    }

The reason that the mysql_num_rows function still makes the condition, is because it returns false. 0 and false are the same if you compare with two equal signs. If you would have done this:

   if (mysql_num_rows($query ) === 0){ // This only fires when the functions return INT 0
   if (mysql_num_rows($query ) === false){ // This only fires when the function returns false (on error)

too clearify a bit more:

1==true   -> true
1===true  => false  (not the same type)
0==false  -> true
0===false -> false  (not the same type)
1=='1'    -> true
1==='1'   -> false (not the same type)
false=='false'  -> true
false==='false' -> false (not the same type)

You are not executing the query at any point:

$q = "SELECT * FROM restable WHERE pk_tId == '" . $id . "'";
$query = mysql_query($sql); // <-----------missing from your code
if (mysql_num_rows($query) == 0){ // <------notice the difference
    echo "No results";
}

mysql_query is deprecated now btw (i've seen people's head get ripped off on this site for sill using it haha!)

I saw your other question from above ;-)

$q = "SELECT * FROM restable WHERE pk_tId == '" . $id . "'";
$q = "SELECT COLUMN_NAME FROM restable WHERE pk_tId == '" . $id . "'"; // <--works with example below
$query = mysql_query($sql); // <-----------missing from your code
if (mysql_num_rows($query) == 0){ // <------notice the difference
    while($row = mysql_fetch_assoc($query)){
        echo '<input type="text" value="'.$row['COLUMN_NAME'].'">';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM