3

I'm developing a script to update my database with a CSV file ! I got this error when I run it

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/vinem/www/batch/batch_stock.php on line 47

this is the code in question :

<?php
/*---------------------CONNEXION MYSQL----------------------*/

$servername='';
$database_username='';
$database_password='';
$database_name='';

set_time_limit(1600);

$link = mysql_connect('', '', '');
if (!$link) {
    die('Connexion impossible : ' . mysql_error());
}
echo 'Connecté correctement';
mysql_close($link);


//$link = mysql_connect($servername,$database_username,$database_password) or die("Erreur de connexion au serveur"); //mysql_connect
//mysql_select_db($link, $database_name) or die("Erreur de connexion à la BDD"); //old: mysql_select_db

/*---------------------FIN CONNEXION MYSQL----------------------*/


/*---------------------RÉCUPÉRATION DU FICHIER STOCK et INITIALISATION----------------------*/

$fichier = file("../batch/export_solsys.csv");
$fp = fopen("../batch/export_solsys.csv","r");

$upc= "";
$stock="";
$ligne = 1; 

/*---------------------FIN RÉCUP----------------------*/


echo("Parcours du fichier...")."<br/>";
/*---------------------MISE A JOUR DU STOCK----------------------*/
while($tab=fgetcsv($fp,1000,';'))
{      
            $champs = count($tab);//nombre de champs dans la ligne en question  
            $ligne++;
            $upc = $tab[0];
            $stock = $tab[7];

      $batch ="UPDATE declinaison_stock_produit SET stock='4' where upc = '56939'";
      $requete = mysql_query($link, $batch);


             echo("Stock mis à jour  ");
             echo("Fin de l'éxécution du batch");

}
  • You are closing the $link right after opening it. Which is why it couldn't work afterwards. The [`mysql_query`](http://php.net/mysql_query) argument order was wrong as well. Also, read up on [PDO etc.](http://php.net/manual/en/mysqlinfo.api.choosing.php) – mario Aug 18 '15 at 13:42

1 Answers1

1

You open your connection which is fine, yet right after it you're closing the connection link. So that connection no longer exists.

Remove this,

mysql_close($link);

Edit 1

Your code is prone to SQL injection, you are still using MySQL even though it has been deprecated, you should use either MySQLi or PDO with prepared statements.

Script47
  • 14,230
  • 4
  • 45
  • 66