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?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
  • 2
    Your query failed. I assume you're missing quotes around the variables. – Fabrício Matté May 26 '13 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 – Phil Cross May 26 '13 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 – Lamloumi Afif May 26 '13 at 21:31
  • @PhilCross i add `echo $connect;` the result is `Resource id #7` – Lamloumi Afif May 26 '13 at 21:33
  • Then like @FabrícioMatté said, try this: `$query = "SELECT tax_id FROM master where name ='" . $name . "'"` – Phil Cross May 26 '13 at 21:34
  • 2
    `$result = mysql_query($qry) or die(mysql_error());` may be informative for the next times. – Fabrício Matté May 26 '13 at 21:37
  • @PhilCross i did this `$qry = "SELECT tax_id FROM master where name ="'.$name.'" and latin ="'.$latin.';` but the result is another error `Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/admin/public_html/hitlist/include/fg_membersite.php on line 443` – Lamloumi Afif May 26 '13 at 21:38
  • @Lamloumi I have posted an answer. The code in your comment has an error, so i've copied and edited your existing code with the minor changes – Phil Cross May 26 '13 at 21:40
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Barmar May 26 '13 at 21:42
  • 3
    Why are you fetching in a `while` loop if you're going to return during the first iteration? – Barmar May 26 '13 at 21:43

2 Answers2

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

Phil Cross
  • 9,017
  • 12
  • 50
  • 84
  • 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 Afif May 26 '13 at 21:50
  • @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: http://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. – Phil Cross May 27 '13 at 13:10
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'];
    }
}
The Alpha
  • 143,660
  • 29
  • 287
  • 307