0

I have a php function of selection from mySql database:

function Master_file($name, $latin ){

  $HOST_DB ="localhost";

  $NAME_DB="nom";
  $USER_DB ="utilisaeur";
  $PWD_DB="K3Pud1";
  $connect = mysql_connect($HOST_DB,$USER_DB,$PWD_DB);
  $db=mysql_select_db($NAME_DB);


  $qry = "SELECT tax_id FROM  master where name =".$name." and latin =".$latin;
  echo $qry;
  $result = mysql_query($qry);

  while ($Res_user = mysql_fetch_assoc($result) ) {

    return $Res_user['tax_id'];
  }
}

an error is shown Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/admin/public_html/hitlist/include/fg_membersite.php on line 446 and the line is while ($Res_user = mysql_fetch_assoc($result)

So what is the problem ? How can i fix it?

10
  • 2
    Your query failed. I assume you're missing quotes around the variables. Commented May 26, 2013 at 21:26
  • 1
    Is the database connection valid? It could be your not actually connected to the database yet, as you don't have any error catching, you may not know about it Commented May 26, 2013 at 21:27
  • @FabrícioMatté i change the query to $qry = "SELECT tax_id` FROM master where name =".$name." and latin =".$latin;` but the same result Commented May 26, 2013 at 21:31
  • 2
    $result = mysql_query($qry) or die(mysql_error()); may be informative for the next times. Commented May 26, 2013 at 21:37
  • 3
    Why are you fetching in a while loop if you're going to return during the first iteration? Commented May 26, 2013 at 21:43

2 Answers 2

1

Try this

function Master_file($name, $latin ){

    $dsn = 'mysql:host=localhost;dbname=nom';
    $username = 'utilisaeur';
    $password = 'K3Pud1';

    try {
        $db = new PDO($dsn, $username, $password);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        echo $e->getMessage();
        exit;
    }


    $result = $db->prepare("SELECT tax_id FROM  master where name =:name");
    $result->bindValue(':name', $name);
    $result->execute();

    foreach($result->fetchAll(PDO::FETCH_ASSOC) as $row){
        echo $Res_user['tax_id'] . '<br />';
    }
}

EDIT The function above has just been updated to use PDO, display any errors, and output the tax_id value to the browser

Sign up to request clarification or add additional context in comments.

2 Comments

nice it works but the same error is repeat only 30 times but before your answer it is 400 rows contains Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in. i added a message under the while condition and it is shown one time. so how can i avoid this error completely?
@lamloumi I have updated the function. I have made several changes, which include using PDO instead of mysql functions. PDO is a lot better to use, check out: php.net/pdo also, I have made a couple other changes as well, so if you want to give it a go and see if it works.
0

You may try this, since your returning here return $Res_user['tax_id']; so I think you need a single row instead

function Master_file($name, $latin ){

    $HOST_DB ="localhost";
    $NAME_DB="nom";
    $USER_DB ="utilisaeur";
    $PWD_DB="K3Pud1";

    $connect = mysql_connect($HOST_DB,$USER_DB,$PWD_DB);
    if (!$connect) {
        die("Could not connect: " . mysql_error());
    }

    $db=mysql_select_db($NAME_DB, $connect);
    if (!$db) {
        die ("Can't use " . $NAME_DB . " : " . mysql_error());
    }

    $qry = "SELECT tax_id FROM  master where name ='" . $name . "' and latin = '" . $latin . "'";
    $result = mysql_query($qry);
    if( $result ){
        $row = mysql_fetch_assoc($result);
        return $row['tax_id'];
    }
}

Comments

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.