Link to home
Start Free TrialLog in
Avatar of MrTV
MrTVFlag for Thailand

asked on

Php my sql try to connect and query

when i run the code below it does not eork how can i fix it

Warning: mysql_query() expects parameter 1 to be string, object given in C:\wamp\www\asian\stockadjust\montlyconcludesioninvoice.php on line 25
Call Stack
#      Time      Memory      Function      Location
1      0.0110      252760      {main}( )      ..\montlyconcludesioninvoice.php:0
2      0.0198      291904      mysql_query ( )      ..\montlyconcludesioninvoice.php:25

( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\asian\stockadjust\montlyconcludesioninvoice.php on line 26




<?php
session_start();
header('Content-Type: text/html; charset=utf-8');
error_reporting(E_ALL);
include "./common_db.inc";
$register_script = "./register.php";



// Create connection
$con=mysqli_connect("$dbhost","$dbusername","$dbuserpassword","$default_dbname1");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
  
  echo "<h2>Category id and sum of total costs of purchases grouped by category id :</h2>";  
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";  
echo "<tr style='font-weight: bold;'>";  
echo "<td width='100' align='center'>Category id</td><td width='100' align='center'>Sum of total costs of purchases</td>";  

$result = mysql_query($con,"SELECT name,SUM(grandtotal)   FROM asianinvoice  GROUP BY name");  
while($row=mysql_fetch_array($result))  
{  
echo "<tr>";  
echo "<td align='center' width='200'>" . $row['name'] . "</td>";   
echo "<td align='center' width='200'>" . $row['SUM(grandtotal)'] . "</td>";  
echo "</tr>";  
}  
echo "</table>";  
?>  
</body>  
</html>  

Open in new window

SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Jagadishwor Dulal
Jagadishwor Dulal
Flag of Nepal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would recommend not using MySQL.  You can find the big red warning label on this page:
http://php.net/manual/en/function.mysql-connect.php

PHP 5.5 is the current release.  In other words, MySQL is already deprecated and on the way out.  So it's time to make the switch, which is not hard at all if you follow the directions in this article.
Ray's right, you're mixing two different methods.  You need to make it all 'mysqli' functions.
Avatar of MrTV

ASKER

hi Jagadishwor Dulal

I change the code to your suggestion it show

( ! ) Warning: mysql_query() expects parameter 1 to be string, resource given in C:\wamp\www\asian\stockadjust\montlyconcludesioninvoice.php on line 24
Call Stack
#      Time      Memory      Function      Location
1      0.0007      252760      {main}( )      ..\montlyconcludesioninvoice.php:0
2      0.0300      272376      mysql_query ( )      ..\montlyconcludesioninvoice.php:24

( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\asian\stockadjust\montlyconcludesioninvoice.php on line 25
Call Stack
#      Time      Memory      Function      Location
1      0.0007      252760      {main}( )      ..\montlyconcludesioninvoice.php:0
2      0.0304      272608      mysql_fetch_array ( )      ..\montlyconcludesioninvoice.php:25
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial