Solved

Php my sql try to connect and query

Posted on 2014-02-04
6
494 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 109

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 109

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 109

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

809 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