Précédent   Forum des professionnels en informatique > PHP > PHP & SGBD > PHP & MySQL
PHP & MySQL Forum d'entraide sur les fonctions MySQL avec PHP. Avant de poster -> FAQ MySQL, Cours MySQL et Sources MySQL. Pour les questions concernant le moteur MySQL plutôt que les fonctions PHP, merci d'utiliser le forum MySQL.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 08/06/2011, 10h14   #1
Futur Membre du Club
 
Inscription : décembre 2010
Messages : 74
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 74
Points : 15
Points : 15
Par défaut Notice: Undefined variable

Bonjour tout le monde.


J'ai deux petites erreurs dans le script suivant au niveau de la ligne 195.
Voilà les symptômes:

Le script est une classe php pour synchroniser 2 bases mysql.

Quand il y a une donnée à télécharger je n'ai pas de message d'erreur.

Par contre si je n'ai aucune nouvelle donnée à télécharger j'ai ces deux messages qui apparaissent concernant la ligne 195:

Citation:
Notice: Undefined variable: sin

et

Warning: implode() [function.implode]: Invalid arguments passed
Pourriez-vous m'aider à corriger ce problème ?

Merci beaucoup.

Voici le script.

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
    class DB
    {
        //Member Zone
        var $dataserver;
        var $database;
        var $result;
        var $row_result;
        var $error_name;
        var $error_number;
 
 
        //Methode Zone
        function DB()
        {
        }
 
        function DBDown()
        //database down
        {
            $this->error_number = mysql_errno();
            $this->error_name = mysql_error(); 
 
 
            if ($this->error_name != "")
            {
                print '<p></p>';
				print '<form name="formulaire" method="post" action="../index.php">
				<input type="submit" value="Ok"  style="height:50px; width:138px"/>
				</form>';
            }
            exit();
        }
 
        function Open($plocation,$puser,$ppassword,$pdb)
        //Open a connection to a MySQL Server
        {
            $this->database = $pdb;
            if(!$this->dataserver = mysql_pconnect($plocation, $puser, $ppassword))
            {
                DB::DBDown();
            }
        }
 
        function executeSQL($sql)
        //Query database
        {
            if (!$res=mysql_query($sql,$this->dataserver))
            {
                DB::DBDown();
            }
            $this->row_result=0;
            $this->result=array();
            //print "sql=".$sql."<br>";
            if ((strpos("_".$sql,'SELECT'))||(strpos("_".$sql,'SHOW'))||(strpos("_".$sql,'DESCRIBE'))||(strpos("_".$sql,'EXPLAIN')))
            {
                while ($this->result[]=mysql_fetch_array($res, MYSQL_NUM))
                {
                    $this->row_result++;
                }
            }
        }
 
        function cleanSQL($param)
        //Clean sql parameters
        {
            return mysql_real_escape_string($param,$this->dataserver);
        }
 
    }
 
    class SyncronizeDB
    {
        //Member Zone
        var $db_master;
        var $table_master;
        var $idx_master;
        var $col_master;
        var $db_slave;
        var $table_slave;
        var $idx_slave;
        var $col_slave;
 
        //Methode Zone
        function SyncronizeDB()
        //HelpdeskDB Constructor
        {
        }
 
 
        function masterColumns()
        {
            //return columns name
            $this->db_master->executeSQL("SHOW COLUMNS from ".$this->db_master->database.".".$this->table_master);
            $i = 0;
            $col = "";
            foreach ($this->db_master->result as $row)
            {
                if ($row != "")
                {            
                    if ($i == 1) $col .= ",";
                    $col .= $row[0];
                    if ($i == 0) $i = 1;
                }
            }
            return $col;
        }
 
        function masterAllIndexes()
        {
            //return all index value
            $this->db_master->executeSQL("SELECT ".$this->idx_master." from ".$this->db_master->database.".".$this->table_master);
        }
 
        function masterDataRows($sin)
        {
            // return data from master database
            $this->db_master->executeSQL("SELECT * from ".$this->db_master->database.".".$this->table_master." where ".$this->idx_master." in (".$sin.")");
        }
 
        function masterSet($mplocation, $mpuser, $mppassword, $mpdb, $mptable, $mpidx)
        //set master database
        {
            $this->table_master = $mptable;
            $this->idx_master = $mpidx;
            $this->db_master = new DB();
            $this->db_master->Open($mplocation,$mpuser,$mppassword,$mpdb);    
            $this->col_master = $this->masterColumns();
        }
 
        function slaveColumns()
        {
            //return columns name
            $this->db_slave->executeSQL("SHOW COLUMNS from ".$this->db_slave->database.".".$this->table_slave);
            $i = 0;
            $col = "";
            foreach ($this->db_slave->result as $row)
            {
                if ($row != "")
                {
                    if ($i == 1) $col .= ",";
                    $col .= $row[0];
                    if ($i == 0) $i = 1;
                }
            }
            return $col;
        }
 
        function slaveAllIndexes()
        {
            //return all index value
            $this->db_slave->executeSQL("SELECT ".$this->idx_slave." from ".$this->db_slave->database.".".$this->table_slave);
        }
 
        function slaveSet($mplocation, $mpuser, $mppassword, $mpdb, $mptable, $mpidx)
        //set slave database
        {
            $this->table_slave = $mptable;
            $this->idx_slave = $mpidx;            
            $this->db_slave = new DB();
            $this->db_slave->Open($mplocation,$mpuser,$mppassword,$mpdb);    
            $this->col_slave = $this->slaveColumns();            
        }    
 
        function slaveSyncronization()
        {
            $this->masterAllIndexes();
            $masterid=array();
            foreach ($this->db_master->result as $id)
            {
                $masterid[] = $id[0];
            }            
 
 
 
            $this->slaveAllIndexes();
            $slaveid=array();
            foreach ($this->db_slave->result as $id)
            {
                $slaveid[] = $id[0];
            }    
 
 
 
            $in = array_diff($masterid, $slaveid);
            echo "Envoi et t&eacute;l&eacute;chargement des donn&eacute;es: ";
 
 
 
			foreach($in as $value)
			{
  			$sin[] = "'" . mysql_real_escape_string($value) . "'";
			}
 
			$sin = implode(",", $sin);
 
			if ($sin == "")
 
			{
				echo "<b>aucune</b>";
                echo ".<br />";                
 
			}
 
            else
            {
                echo '<font color="#009900">';
				echo 'Succes';
				echo '</font>';
                echo ".<br />";                
                $this->masterDataRows($sin);
                $sql = "INSERT INTO ".$this->db_slave->database.".".$this->table_slave." (".$this->col_slave.") values ";
                $i = 0;
                foreach ($this->db_master->result as $row)
                {
                    if (count($row) > 1)
                    {
                        if ($i == 1) $sql .= ",";
                        $sql .= "(";
                        $j=0;
                        //print_r($row);
                        foreach ($row as $col)
                        {
                            if ($j == 1) $sql .= ",";
                            $col = $this->db_slave->cleanSQL($col);
                            $sql .= "'".$col."'";
                            if ($j == 0) $j = 1;
                        }
                        $sql .= ")";
                        if ($i == 0) $i = 1;
                    }
                }        
 
                $this->db_slave->executeSQL($sql);                
            }
 
            $out = array_diff($slaveid, $masterid);
            foreach($out as $value)
			{
  			$sout[] = "'" . mysql_real_escape_string($value) . "'";
			}
 
            $sout = implode(",", $out);
            if ($sout == "")
            {
                echo "<b>aucune</b>";
                echo ".<br />";                
            }
            else
            {
             $sql = "UPDATE ".$this->db_slave->database.".".$this->table_slave." SET ".$this->col_slave." where ".$this->table_slave.				".".$this->idx_slave." (".$sout.") ";
 
 
                echo ".<br />"; 
                $this->db_slave->executeSQL($sql);       
 
            }
        }
    }
 
?>
legrandse est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/06/2011, 10h26   #2
Membre éprouvé
 
Avatar de Nheo_
 
Homme
Étudiant
Inscription : avril 2011
Messages : 317
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : avril 2011
Messages : 317
Points : 405
Points : 405
Bonjour,

Si aucune donnée n'est envoyée, ta variable $sin est vide lorsque tu fais $sin = implode(",", $sin);.

Il te suffit de tester si la variable est vide ou non, en utilisant ton code :

Code :
1
2
3
4
5
6
7
8
9
10
if ($sin == "")
{
     echo "<b>aucune</b>";
     echo ".<br />";                
 
}
else
{
     $sin = implode(",", $sin);
}
Nheo_ est déconnecté   Envoyer un message privé Réponse avec citation 01
Vieux 08/06/2011, 10h50   #3
Futur Membre du Club
 
Inscription : décembre 2010
Messages : 74
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 74
Points : 15
Points : 15
Hello,


Je te remercie pour ta réponse rapide.

En effet, cette solution corrige le Warning: implode() [function.implode]: Invalid arguments passed

Mais j'ai toujours le: Notice: Undefined variable: sin

Faut-il initialiser la variable ? si oui de quelle manière ?

Merci.
legrandse est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/06/2011, 10h51   #4
Modérateur
 
Inscription : septembre 2010
Messages : 7 103
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 103
Points : 8 466
Points : 8 466
c'est

__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 08/06/2011, 11h41   #5
Futur Membre du Club
 
Inscription : décembre 2010
Messages : 74
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 74
Points : 15
Points : 15
Je viens de modifier les lignes pour que cela devienne:

Code :
1
2
3
4
5
6
7
8
9
10
if (!empty($sin))
{
     echo "<b>aucune</b>";
     echo ".<br />";                
 
}
else
{
     $sin = implode(",", $sin);
}
Pour une raison que j'ignore, la ligne avec if (!empty($sin)) renvoie aucune données alors qu'il devait y en avoir.

Si je remet if ($sin == "") alors il trouve bien les nouvelles données et synchronise.

Que faire pour résoudre ceci ?

Merci.
legrandse est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/06/2011, 11h47   #6
Membre éprouvé
 
Avatar de Nheo_
 
Homme
Étudiant
Inscription : avril 2011
Messages : 317
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : avril 2011
Messages : 317
Points : 405
Points : 405
Ce ne serait pas plutôt if(empty($sin)) (sans le !) ?
Nheo_ est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/06/2011, 11h55   #7
Modérateur
 
Inscription : septembre 2010
Messages : 7 103
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 103
Points : 8 466
Points : 8 466
Citation:
Envoyé par Nheo_ Voir le message
Ce ne serait pas plutôt if(empty($sin)) (sans le !) ?
oui c'est ca
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/06/2011, 12h01   #8
Futur Membre du Club
 
Inscription : décembre 2010
Messages : 74
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 74
Points : 15
Points : 15
Ok çà à l'air de fonctionner sans le "!".


Merci beaucoup.
legrandse est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 20h48.


 
 
 
 
Partenaires

Hébergement Web