Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Php my sql try to connect and query

Posted on 2014-02-04
6
Medium Priority
?
522 Views
Last Modified: 2014-03-01
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

0
Comment
Question by:teera
6 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1336 total points
ID: 39832881
You have MySQL mixed up with MySQLi.  You want to make the conversion completely to object-oriented MySQLi.  This article will help you do that.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 15

Assisted Solution

by:Jagadishwor Dulal
Jagadishwor Dulal earned 664 total points
ID: 39832953
Or just use mysql like:
<?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=mysql_connect("$dbhost","$dbusername","$dbuserpassword") or die(mysql_error());

// Check connection
if($con){
	mysql_select_db("$defaultdbname", $con) or die(mysql_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

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39832970
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39833418
Ray's right, you're mixing two different methods.  You need to make it all 'mysqli' functions.
0
 

Author Comment

by:teera
ID: 39834458
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
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1336 total points
ID: 39835681
Please don't waste any more of your time on the MySQL extension.  Instead read the large red warning box on this man page.
http://us2.php.net/manual/en/function.mysql-connect.php

Then read this article carefully.  It has tested and working code examples that will show you exactly how to use a data base with PHP.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

If you're new to PHP and need some good learning resources to get a foundation, this article will guide you in the right direction.  If you start on the data base article and find yourself confused, stop and read this one first, then go back to the data base article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question