MySQL查询获取数组不起作用

时间:2017-10-20 07:11:34

标签: php mysql fetch

我有一些MySQL查询,但其中一个不起作用(“Number 1”)。另一个查询(“数字2”)工作正常。但我必须多次重复“2号”,超过100个。这很难。我没有得到查询“1号”工作。怎么了?

“1号”

<?php
require_once ("../config/config.db.inc.php");

$sdd_db_host = DB_HOSTNAME;
$sdd_db_name = DB_DATABASE;
$sdd_db_user = DB_USERNAME;
$sdd_db_pass = DB_PASSWORD;
mysql_connect($sdd_db_host,$sdd_db_user,$sdd_db_pass);
mysql_select_db($sdd_db_name);

mysql_query ("set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");
mysql_query ("SET NAMES utf8");
$menu = mysql_fetch_array(mysql_query("SELECT * `tbl_customers`"));

echo $menu['finskaya_razdel']; ?>

什么都没发生,但它应该! ['finskaya_razdel']不仅仅是一个......

“2号”

<?php
require_once ("../config/config.db.inc.php");

$sdd_db_host = DB_HOSTNAME;
$sdd_db_name = DB_DATABASE;
$sdd_db_user = DB_USERNAME;
$sdd_db_pass = DB_PASSWORD;
mysql_connect($sdd_db_host,$sdd_db_user,$sdd_db_pass);
mysql_select_db($sdd_db_name);

mysql_query ("set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");
mysql_query ("SET NAMES utf8");

$menu = mysql_query("SELECT finskaya_razdel FROM `tbl_customers` WHERE customers_id='1008' or customers_id='8'");
$menu_res = mysql_result($menu, 0);

echo $menu_res; ?>

此查询有效。但它很疯狂......我有超过100个查询要做。数字1更“华丽”。我做错了什么?

1 个答案:

答案 0 :(得分:1)

  1. msql_fetch_*一次提取一行。你必须每次循环并应用它。
  2. 要使用列名,您必须使用mysql_fetch_assoc(使用mysql_fetch_array,您必须使用索引号。)
  3. 因此...

    $menu = mysql_query("SELECT * FROM tbl_customers");
    
    while ($row = mysql_fetch_assoc($menu))
        echo $row["finskaya_razdel"];
    

    ...或者如果你需要在php多维数组中得到结果......

    $menu = mysql_query("SELECT * FROM tbl_customers");
    
    $results = array();
    while ($row = mysql_fetch_assoc($menu))
        $results[] = $row;
    

    注意 :@BarclickFloresVelasquez是对的。你应该转移到mysqli或PDO