user2236124
user2236124

Reputation:

Having Trouble Performing a Simple MySql Query

I'm very new to PHP and still learning the basics and I'm stuck on an issue I'm hoping someone could help me with.

I have the following code;

$MfgQuery = "SELECT `Mfg` FROM `categories` WHERE `ID` = 10";

if($MfgQueryRun = mysql_query($MfgQuery)) {

    if (mysql_num_rows($MfgQueryRun)==NULL){
        echo 'No Mfg Results Returned';
    } else {
    $Mfg = $MfgQueryRun['Mfg'];
    echo $Mfg;
}
} else {
echo 'Query Unsuccessful';

}

But for a reason unknown to me I keep getting 'No Mfg Results Returned' but if I copy and paste the query by itself into the MySQL box in PHPMyAdmin it returns a result properly.

What gives??

BTW, Here's exactly what I can insert into PHP MyAdmin's SQL box;

SELECT `Mfg` FROM `categories` WHERE `ID` = 10 

and this returns a result but using the code above, it does not.

If anyone wouldn't mind showing me where I went wrong I'd be most appreciative and I thank you in advance

Upvotes: 1

Views: 84

Answers (4)

nibra
nibra

Reputation: 4028

First, you should not use mysql_* functions, they are deprecated (outdated) as of PHP 5.5.0, and will be removed in the future. Use mysqli instead.

Second, you're not testing properly. PHP has no strong types, so a test '== null' does not always do, what you'll expect. See the PHP manual for comparision operators. To test against null, always use is_null().

Third, you're not fetching the data.

Using MySQLi, the code would look similar to this:

$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno != 0) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    die(); // or return, if in method or function context
}

$result = $mysqli->query("SELECT `Mfg` FROM `categories` WHERE `ID` = 10");
if ($result === false) {
    echo "Query failed: (" . $mysqli->errno . ") " . $mysqli->error;
    die(); // or return, if in method or function context
}

printf("Select returned %d rows.\n", $result->num_rows);

$row = $result->fetch_assoc();
echo $row['Msg'];

/* free result set */
$result->close();

Upvotes: 0

echo_Me
echo_Me

Reputation: 37253

try this

 $MfgQuery = "SELECT `Mfg` FROM `categories` WHERE `ID` = 10";

  $MfgQueryRun = mysql_query($MfgQuery) ;
  $row = mysql_fetch_array($MfgQueryRun) ;

   if (mysql_num_rows($MfgQueryRun)== 0 ){
    echo 'No Mfg Results Returned';
  } else {
   $Mfg = $row['Mfg'];
   echo $Mfg;

  }

Upvotes: 1

Code Lღver
Code Lღver

Reputation: 15593

if (mysql_num_rows($MfgQueryRun)){
        echo 'No Mfg Results Returned';
} else {
       $Mfg = $MfgQueryRun['Mfg'];
       echo $Mfg;
}

Try this remove the NULL in comparison. because mysql_num_rows returns numeric values.

Upvotes: 0

Miroslav Stopka
Miroslav Stopka

Reputation: 107

Try

$MfgQuery = "SELECT `Mfg` FROM `categories` WHERE `ID` = 10";
$MfgQueryRun = mysql_query($MfgQuery);

if($MfgQueryRun) {

    if (mysql_num_rows($MfgQueryRun) == 0){
        echo 'No Mfg Results Returned';
    } else {
    $Mfg = $MfgQueryRun['Mfg'];
    echo $Mfg;
}
} else {
echo 'Query Unsuccessful';

}

Upvotes: 0

Related Questions