1

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.

Prix
  • 19,417
  • 15
  • 73
  • 132
HoodCoderMan
  • 103
  • 7
  • 26
  • 4
    Well, you're not running your query, for a start - you need to call `mysql_query()` to pass it to the database. – andrewsi Aug 13 '13 at 19:00

2 Answers2

1

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)
Martijn
  • 15,791
  • 4
  • 36
  • 68
0

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'].'">';
    }
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • What should I use then? – HoodCoderMan Aug 13 '13 at 19:09
  • @JohnBeasley Check out PDO – StephenTG Aug 13 '13 at 19:12
  • TL;DR: check out mysqli_* because it should be a super simple transition........I personally took the time to learn and now prefer PDO but I've seen people suggest the mysqli_* extension. Seems pretty much the same as the regular mysql_* but something is more secure about it, idk (i skipped it and use PDO now because I connect to both Oracle and MySQL in my projects). – MonkeyZeus Aug 13 '13 at 19:12
  • Thanks. I'm still testing, but I'm very close to making this work. – HoodCoderMan Aug 13 '13 at 19:29