2010-04-23 15 views
9

Eventuali duplicati:
“Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given” error while trying to create a php shopping cartmysql_fetch_array() si aspetta parametro 1 per essere problema di risorse

io non capisco, non vedo errori in questo codice, ma non c'è questo errore , per favore aiutatemi:
mysql_fetch_array() si aspetta parametro 1 per essere problema di risorse

<?php 

     $con = mysql_connect("localhost","root","nitoryolai123$%^"); 
    if (!$con) 
     { 
     die('Could not connect: ' . mysql_error()); 
     } 

    mysql_select_db("school", $con); 
     $result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']); 
    ?>  


          <?php while ($row = mysql_fetch_array($result)) { ?>    
            <table class="a" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#D3D3D3"> 
    <tr> 

    <form name="formcheck" method="get" action="updateact.php" onsubmit="return formCheck(this);"> 
    <td> 
    <table border="0" cellpadding="3" cellspacing="1" bgcolor=""> 
    <tr> 

    <td colspan="16" height="25" style="background:#5C915C; color:white; border:white 1px solid; text-align: left"><strong><font size="2">Update Students</td> 


    <tr> 
    <td width="30" height="35"><font size="2">*I D Number:</td> 
    <td width="30"><input name="idnum" onkeypress="return isNumberKey(event)" type="text" maxlength="5" id='numbers'/ value="<?php echo $_GET['id']; ?>"></td> 
    </tr> 

    <tr> 
    <td width="30" height="35"><font size="2">*Year:</td> 
    <td width="30"><input name="yr" onkeypress="return isNumberKey(event)" type="text" maxlength="5" id='numbers'/ value="<?php echo $row["YEAR"]; ?>"></td> 

<?php } ?> 

Sto solo cercando di caricare i dati nei moduli ma non so perché compare questo errore. Quale potrebbe essere l'errore qui?

+1

lo consiglio ad almeno fusione '$ _GET [ 'id']' a 'int': ' mysql_query (" SELECT * FROM student WHERE IDNO = ". (Int) $ _ OTTIENI ['id']);' – binaryLV

risposta

23

non si sta facendo il controllo degli errori dopo la chiamata a mysql_query:

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']); 
if (!$result) { // add this check. 
    die('Invalid query: ' . mysql_error()); 
} 

In caso mysql_query fallisce, restituisce false, un valore boolean. Quando si passa a questa funzione mysql_fetch_array (che si aspetta un mysql result object) otteniamo questo errore.

0

Assicurati che la query sia stata eseguita correttamente e che tu abbia ottenuto i risultati. È possibile verificare in questo modo:

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']) or die(mysql_error()); 


if (is_resource($result)) 
{ 
    // your while loop and fetch array function here.... 
} 
0

La causa più probabile è un errore nella mysql_query(). Hai controllato per assicurarsi che funzionasse? Emettere il valore di $result e mysql_error(). Potreste aver scritto male qualcosa, selezionate il database errato, hanno un problema di autorizzazioni, ecc Quindi:

$id = (int)$_GET['id']; // this also sanitizes it 
$sql = "SELECT * FROM student WHERE idno = $id"; 
$result = mysql_query($sql); 
if (!$result) { 
    die("Error running $sql: " . mysql_error()); 
} 

sanificazione $_GET['id'] è davvero importante. È possibile utilizzare mysql_real_escape_string() ma il relativo casting su un valore int è sufficiente per i numeri interi. Fondamentalmente si vuole evitare l'iniezione SQL.

0

Nel vostro database qual è il tipo di "IDNO"? Potrebbe essere necessario sfuggire alla sql qui:

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']); 
2
$id = intval($_GET['id']); 
$sql = "SELECT * FROM student WHERE IDNO=$id"; 
$result = mysql_query($sql) or trigger_error(mysql_error().$sql); 

sempre farlo in questo modo e vi dirà che cosa è sbagliato

0

Si utilizza questo:

mysql_fetch_array($result) 

Per ottenere l'errore che stai ricevendo, significa che $result non è una risorsa.


Nel codice, $result si ottiene in questo modo:

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']); 

Se la query SQL non riesce, $result non ci sarà una risorsa, ma un valore booleano - vedere mysql_query.

Suppongo ci sia un errore nella query SQL - così viene a mancare, mysql_query restituisce un valore booleano, e non una risorsa, e mysql_fetch_array non può lavorare su questo.


Si dovrebbe verificare se la query SQL restituisce un risultato oppure no:

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']); 
if ($result !== false) { 
    // use $result 
} else { 
    // an error has occured 
    echo mysql_error(); 
    die; // note : echoing the error message and dying 
      // is OK while developping, but not in production ! 
} 

Con questo, si dovrebbe ottenere un messaggio che indica l'errore che si è verificato durante l'esecuzione di query - questo dovrebbe aiutare cifra qual è il problema è ;-)


Inoltre, si dovrebbe fuggire i dati che stai mettendo nella query SQL, per evitare SQL injections!

Per esempio, qui, è necessario assicurarsi che $_GET['id'] contiene altro che un intero, utilizzando qualcosa di simile:

$result = mysql_query("SELECT * FROM student WHERE IDNO=" . intval($_GET['id'])); 

Oppure si dovrebbe verificare questo prima di tentare di eseguire la query, per visualizzare un bello messaggio di errore per l'utente.

2

dare una prova

$indo=$_GET['id']; 
$result = mysql_query("SELECT * FROM student WHERE IDNO='$indo'"); 

Penso che questo funziona ..

Problemi correlati