[英]get values from mysql column in php array
I've a database with several columns. 我有一个包含几列的数据库。 One of the columns is 'name' and stores the name of the companies in the database.
列之一是“名称”,并将公司名称存储在数据库中。 Another column ('id') assign a unique id number to each company.
另一列('id')为每个公司分配一个唯一的ID号。 The table has a minimum of 1 and a maximum of 4 companies.
该表的最小值为1,最大值为4。
I'd like to show the name of the companies in different places in a html document and therefore need to refer to $company_name_1, $company_name_2 etc. However, these variables should always be in the html document, irrespective of whether the companies are in the database. 我想在html文档中的不同位置显示公司名称,因此需要引用$ company_name_1,$ company_name_2等。但是,这些变量应始终在html文档中,无论公司是否位于数据库。 If not in the database, the value shown should be empty.
如果不在数据库中,则显示的值应为空。
How can I automatically define the names of the companies with an array and a loop? 如何使用数组和循环自动定义公司名称? I want to expand the database at a later stage hence manually defining the four company names is not an option.
我想在以后扩展数据库,因此手动定义四个公司名称不是一个选择。
Thanks! 谢谢!
$result = mysql_query($con, "SELECT name FROM companydetails");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row['name'];
}
And then I would like to use: 然后我想使用:
echo $storeArray[0];
to show the first company and 展示第一家公司
echo $storeArray[1];
to show the second company. 展示第二家公司。 Etc.
等等。
Before echo first check if the element exists in array. 在回显之前,首先检查元素是否存在于数组中。 This way there will be no error, something like this
这样就不会有错误,像这样
if(count($storeArray)>1)
echo $storeArray[1];
else echo "NA";
if(count($storeArray)>2)
echo $storeArray[2];
else echo "NA";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.