PHP and Mysqli getting value of a count(distinct()) query

前端 未结 3 861
后悔当初
后悔当初 2021-01-26 22:33

I have a db table in mysql called gsm, with 2 columns, LAC and NAME.

So I am trying to count how many different LAC are stored in the DB and retrieve a php value to furt

3条回答
  •  梦谈多话
    2021-01-26 22:53

    you can check with the below code , however its not tested from my end but i m sure that it would work.

     # Init the MySQL Connection
        if (!( $db = mysql_connect('localhost', 'root', '') ))
            die ('Failed to connect to MySQL Database Server - #'.mysql_errno ().': '.mysql_error ());
        if (!mysql_select_db('ram'))
            die ('Connected to Server, but Failed to Connect to Database - #'.mysql_errno ().': '.mysql_error ());
        # Prepare the SELECT Query
        $sql = "select count(distinct lac) as LAC from gsm ";
        if (!( $selectRes = mysql_query($sql) )) {
            echo 'Retrieval of data from Database Failed - #' . mysql_errno() . ': ' . mysql_error();
        } else if (mysql_num_rows($selectRes) == 0) {
    
            echo 'No Any Record Returned';
        } else {
            while ($row = mysql_fetch_assoc($selectRes)) {
                echo $row['LAC'];
            }
        }
    

提交回复
热议问题