2015-07-01 251 views
0

即時試圖從我的數據庫(文章)和表文章中提取一張表,並將其放入數組中,但即時通訊不知道天氣與否因爲我不知道如何打印數組。我正在關注這個鏈接。從數據庫中提取數組php

http://phpscriptarray.com/php-arrays-tutorials-tour/how-to-extract-mysql-database-data-into-php-array-variable.php

<?php 

$servername = "localhost"; 
$username = "username"; 
$password = "password"; 

// create connection 
$conn = new mysqli($servername, $username, $password); 
// check connection 
if ($conn->connect_error){ 
    die("connection failed: " . $conn->connect_error); 
} 
// connect to DB 
$db_selected = mysqli_select_db('article', $conn); 
if (!$db_selected){ 
    die("can't use article : " .mysqli_error()); 
} 
// extract databases table to PHP array 
$query = "SELECT * FROM `articles`"; 
$result = mysqli_query($query); 
$number = mysql_numrows($result); 
$article_array = array(); 
$x = 0 
while($x < $number) 
{ 
    $row = mysqli_fetch_array($result); 
    $artic = $row['name']; 
    $amount = $row['quantity']; 
    $article_array[$artic] = $amount; 
    $x++; 
} 
echo count($article_array); 
//echo "hello"; 

<? 

即使回聲你好不會工作和IM不知道如果我應該把一個名稱和數量:

$artic = $row['name']; 
$amount = $row['quantity']; 
+0

你說'echo「hello」;'不工作?你有沒有php服務器?你在工作什麼服務器? xampp,wamp還是lamp? –

+0

沒有'mysql_numrows'函數。 mysqli_和mysql_函數是**不同的** API。什麼是結尾'<?'? –

回答

1

你是混合過程的風格面向對象。您的查詢和循環應該是這樣的:

$query = "SELECT * FROM `articles`";  
$result = $conn->query($query); 
$article_array = array(); 
while($row = $result->fetch_array(MYSQLI_ASSOC)){ 
    $artic = $row['name']; 
    $amount = $row['quantity']; 
    $article_array[$artic] = $amount; 
} 

http://php.net/manual/en/mysqli.query.php

而且你的PHP關閉標籤是錯誤的 - 應該是?>或省略。

+0

哇,真的很好,謝謝你! – salsachips44