Solved

Php my sql try to connect and query

Posted on 2014-02-04
6
482 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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 334 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 166 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 108

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 82

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 108

Accepted Solution

by:
Ray Paseur earned 334 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now