0

I use this PHP code to get value from my DB.

My problem is that i got null result.

I check for !empty($result) and for mysql_num_rows($result) but still i got null result.

I tested exactly the same query i use in my code on my phpmyadmin and it working.

The echo from $response["SQL"] is "good".

Here is my PHP code:

$result = mysql_query("SELECT * FROM workouts_wall WHERE workout_name = 'WO13' AND user = 'tomer2'") or die (mysql_error());


// mysql inserting a new row
 if (!empty($result))
 {
    if (mysql_num_rows($result) > 0) 
    {
        $response["SQL"] = "good";
    }
    else
    {
        $response["SQL"] = "bad";
    }
}

else    
{
    $response["SQL"] = "bad";
}   


$response["is null?"] = $result;
    // echoing JSON response
    echo json_encode($response);

Solved

I added this line to fix it:

$row = mysql_fetch_array($result);
9
  • Have you checked for any errors? mysql_error() may shed some light on your issue. Commented Jul 23, 2013 at 19:34
  • mysql_error() doesnt show any errors. Commented Jul 23, 2013 at 19:34
  • Ha, yeah I just saw the or die section at the end... Commented Jul 23, 2013 at 19:35
  • Have you connected to the db? BTW, you should be using either mysqli or PDO - the mysql functions are not secure. Commented Jul 23, 2013 at 19:35
  • 2
    how come $response["SQL"] is "good" if you are getting null? Commented Jul 23, 2013 at 19:38

4 Answers 4

1

You can't just return the mysql_query results. You must fetch the data.

$data = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    $data[] = $row;

echo(json_encode($data));

//EDIT: Also a good idea when you are done...
mysql_free_result($result);
Sign up to request clarification or add additional context in comments.

3 Comments

when i try to get values from my result i gets null. If i do $result["workout_name"] and echo it i get null value
You can't do $result["workout_name"], you need to fetch the row from your data first, and then do something like echo $row["workout_name"]
I tried $row = mysql_fetch_array($result)) $response["check"] = $row["workout_name"]; and i still got null for my echo
0

The mysql_query() function returns either a resource or a boolean. If your query is successful (as it probably is in this case), you're still only assigning the MySQL resource to your $response variable. You need to actually use something like mysql_fetch_array() or mysql_fetch_assoc() to extract the returned data.

Regarding not using the mysql_*() functions: Why shouldn't I use mysql_* functions in PHP?

Comments

0

try this. hope it work:

$result = mysql_query("SELECT * FROM workouts_wall WHERE workout_name = 'WO13' AND user = 'tomer2'") or die (mysql_error());


    // mysql inserting a new row
     if (!empty(mysql_fetch_array($result)))
     {
        if (mysql_num_rows(($result) > 0) 
        {
            $response["SQL"] = "good";
        }
        else
        {
              $response["SQL"] = "bad";
        }
    }

    else    
    {
        $response["SQL"] = "bad";
    }   


    $response["is null?"] = $result;
        // echoing JSON response
        echo json_encode($response);

Comments

0

A mysql result which has no rows is NOT empty. It's a standard mysql result handle which simply happens to actually contain no rows. It's not an error, it's not an empty string, it's not an empty array... it's a standard result handle, like any other result handle.

Your code should be

$result = mysql_query($sql) or die(mysql_error()); // catch the REAL errors
if (mysql_num_rows($result) == 0) { 
   ... result set is empty
}

1 Comment

Sorry, brainfart on my part. Should've been mysql_num_rows, not count.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.