2012-10-24 27 views
5

Sono nuovo di PHP e sto provando a creare un sito Web utilizzando PHP. Ho localhost per testare il risultato e ho phpmyadmin già installato sul sito.mysqli_query, mysqli_fetch_array e while loop

Quello che sto cercando di fare ora, è quello di elencare il contenuto della mia tabella "proprietà" dal database "portale" e riempire una tabella con i risultati.

Sto usando mysqli_query, mysqli_fetch_array e mentre ciclo. Sto ottenendo il seguente errore:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\falcon\portal\forms\edit listing.php on line 15

session_start(); 
require_once "connect_to_mysql.php"; // where i store username and password to access my db. 

$sqlCommand = "SELECT * property FROM portal"; // dbname: portal - table: propery 
$query = mysqli_query($myConnection, $sqlCommand); 

$Displayproperty = ''; 
while ($row = mysqli_fetch_array($query)) 
$id = $row["pid"]; 
$title = $row["ptitle"]; 
$area = $row["parea"]; 
$city = $row["pcity"]; 
$Displayproperty .= '<table width="500" border="0" cellspacing="0" cellpadding="1"> 
<tr> 
<td>' . $id . '</td> 
<td>' . $title . '</td> 
<td>' . $area . '</td> 
<td>' . $city . '</td> 
<td><a href="forms.php?pid=' . $id . '">Upload images</a><br /></td> 
</tr> 
</table>'; 

risposta

2

Sostituire la query con questo. Assicurati di aver aggiunto questa linea prima.

$db = mysql_select_db('portal'); 

$sqlCommand = "SELECT * FROM property"; 
+0

Grazie. L'errore è scomparso ma non visualizzo alcun dato. – Omar

+0

Buono. Dopo la query, digita immediatamente questa riga e controlla quante righe ottieni. echo $ totalRows = mysql_num_rows ($ query); –

+0

Anche dopo parentesi non hai parentesi graffe ($ row = mysqli_fetch_array ($ query)). Dovrebbe essere while ($ row = mysqli_fetch_array ($ query)) {e quindi terminarlo dopo il tag table, '; } –

1

Il problema è un errore di sintassi nell'istruzione SQL, che sta causando mysqli_query() a restituire false.

SELECT * property FROM portal non è valido SQL.

Si dovrebbe sempre controllare per assicurarsi mysqli_query restituisce un risultato valido con un costrutto come:

$result = mysqli_query($myConnection, $sqlCommand); 
if(! $result) { 
    die("SQL Error: " . mysqli_error($myConnection)); 
} 

// use result here..... 
1

l'istruzione SQL

SELECT * property FROM portal 

non è corretto SQL, quindi la query non ottiene eseguito. Prova a rimuovere la parola property per ottenere alcuni risultati.

2

Dovrebbe essere

$sqlCommand = "SELECT * FROM portal.property"; /* Database_Name.Table_Name */ 

O semplicemente utilizzare

$sqlCommand = "SELECT * FROM property"; 
+0

Vuoi dire 'portal.property' :-P –

+0

tabella "Proprietà" dal database "portale" .. isn 'it it other it around – eis

+0

@RocketHazmat haha ​​che confondeva thnx: p –

4

Vostri criteri è sbagliato, così dopo

$query = mysqli_query($myConnection, $sqlCommand); 

$ query è falso. Ecco perché ottieni l'errore.

la query SQL corretta è:

SELECT * FROM portal.property 

Se è necessario specificare il nome del database. Inoltre, prima di fare:

while ($row = mysqli_fetch_array($query)) 

Si dovrebbe verificare che $ query esiste

if(!empty($query) { 
while ($row = mysqli_fetch_array($query)) { 
... 
1

è necessario connettersi prima al portale DB utilizzando:

$myConnection = new mysqli("localhost", "user", "password", "database"); 

Quindi eseguire:

$mysqli->query("SELECT * FROM property"); // This will run the query on portal database. 

Se si desidera eseguire una query semplice tabella di proprietà del portale è possibile utilizzare:

$mysqli->query("SELECT * FROM portal.property"); 

o

mysqli_query("SELECT * FROM portal.property");