0

im currently trying to extract a table from my database (articles) and the table article and put it in an array but im not sure weather or not it wokred because i dont know how to print an array. i was following this link.

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";

<?

even the echo hello wont work and im not sure if i was supposed to put a name and quantity in:

$artic = $row['name'];
$amount = $row['quantity'];
2
  • you said that echo "hello"; is not working? did you have php server at your end or not? what server you are working? xampp,wamp or lamp? Commented Jul 1, 2015 at 17:52
  • There's no mysql_numrows function. mysqli_ and mysql_ functions are different APIs. What for is the ending <?? Commented Jul 1, 2015 at 17:52

1 Answer 1

1

You are mixing object oriented with procedural style. Your query and loop should look like this:

$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

Also your PHP closing tag is faulty - should be ?> or omitted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.