-5

I try to get list after query by using php drop down list. My code looks like this for now:

     $con = mysql_connect($server,$user_name,$password,$database);
     $db_found = mysql_select_db($database, $con);
     if ($db_found) {
     print "Database Found ";
     }
     else {
     print "Database NOT Found ";
     }

     $result = mysql_query($con,$db_found,"SELECT
     connection_log.user,count(*)  
     as 'connection_count', sum(TIMESTAMPDIFF(MINUTE,
     connection_log.logondate,connection_log.logoffdate)) 
     as `connection_time`
     FROM connection_log 
     INNER JOIN users 
     ON connection_log.user=users.user WHERE users.groups 
     LIKE '%cpg_sirket_dyo%' 
     and connection_log.logondate>='2015-09-01 00:00:00' 
     and connection_log.logoffdate<'2015-10-01 00:00:00' 
     group by connection_log.user 
     order by connection_count desc");
     while ($row = mysql_fetch_array($result)) {  
     echo $row['name']."<br />";  
     } 

But when I run this php I get some error like shown below:

Database Found PHP Warning: mysql_query() expects at most 2 parameters, 3 >given in /var/www/html/test.php on line 22 PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, null >given in /var/www/html/test.php on line 23

Have you any idea?

TFC
  • 65
  • 1
  • 11

4 Answers4

2

mysql_query

mysql_query ( string $query [, resource $link_identifier = NULL ] )

Only need two parameter needed

1)Your query.

2)The MySQL connection

You just remove your database name inside your mysql_query also swap position of query and connection

 $result = mysql_query("SELECT....",$con);

Use backtick in column and table name instead of quotes

Correct your qyery with

     SELECT
     connection_log.user,count(*)  
     as `connection_count`, sum(TIMESTAMPDIFF(MINUTE,
     connection_log.logondate,connection_log.logoffdate)) 
     as `connection_time`
     FROM connection_log 
     INNER JOIN users 
     ON connection_log.user=users.user WHERE users.groups 
     LIKE '%cpg_sirket_dyo%' 
     and connection_log.logondate>='2015-09-01 00:00:00' 
     and connection_log.logoffdate<'2015-10-01 00:00:00' 
     group by connection_log.user 
     order by connection_count desc

Note:- mysql is deprecated instead use mysqli or PDO

Saty
  • 22,443
  • 7
  • 33
  • 51
1

You need to remove this $db_found out of your function :

http://php.net/manual/en/function.mysql-query.php

$result = mysql_query($yourQuery,$con);

Also try using mysqli instead of mysql which is deprecated.

MySQL vs MySQLi when using PHP

Community
  • 1
  • 1
Nirnae
  • 1,315
  • 11
  • 23
1

Have a look at http://php.net/manual/en/function.mysql-query.php

There are only 2 parameters, the query and (optinal) the connection, so you have to use:

$result = mysql_query("SELECT ...", $con);
tino.codes
  • 1,512
  • 1
  • 12
  • 23
  • If i got the sarcasm correct, I've just wanted do make this clear for the people who read this afterwards. – pmayer Nov 05 '15 at 15:11
1

Use below code,

$con = mysql_connect($server,$user_name,$password);
     $db_found = mysql_select_db($database);
     if ($db_found) {
     print "Database Found ";
     }
     else {
     print "Database NOT Found ";
     }

     $result = mysql_query("YOUR QUERY",$con);
     while ($row = mysql_fetch_array($result)) {  
     echo $row['name']."<br />";  
     }
Vallabh Bothre
  • 621
  • 5
  • 8