Solved

Php my sql try to connect and query

Posted on 2014-02-04
6
486 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 83

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

895 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