View Full Version : Warning: mysql_result() expects parameter 1 to be resource, object given in C:\xampp\
dasht
07-18-2013, 11:27 AM
Hello!
I am doing a project and developing a table using php and it is giving me an error as
"Warning: mysql_result() expects parameter 1 to be resource, object given in C:\xampp\"
please help to resolve this problem. Relevent code is given below
<?php
$con=mysqli_connect("localhost","root","","onlinegourmet");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//$ID = mysql_insert_id();
$sql="SELECT pname, price FROM product WHERE category = 'Bakery'";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
$rs = mysqli_query($con,$sql);
$count = mysqli_num_rows(mysqli_query($con,$sql));
echo '<table border="1" width="100%">';
for($i=0; $i<$count; $i++){
$name = mysql_result($rs, $i, "pname") or die (mysql_error());
$price = mysql_result($rs, $i, "price") or die (mysql_error());
echo "<tr>";
echo "<td>";
echo "<input name='Bakery[]' type='checkbox' value='$name'/>";
echo "</td>";
echo "<td>";
echo "$name";
echo "</td>";
echo "<td>";
echo "$price";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
i dont know how to correct that warning it is not showing name and prices please help. thanks in advance :(
dasht
07-19-2013, 07:13 AM
I have solved this problem i was using the combination of mysqli and mysql thats why it was giving me object instead of resource. if it can be helpfull for others then correct code is given below.
<?php
$con=mysql_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("onlinegourmet", $con );
$sql="SELECT pname, price FROM product WHERE category = 'Bakery'";
if (!mysql_query($sql, $con))
{
die('Error: ' . mysql_error($con));
}
$rs = mysql_query($sql, $con);
$count = mysql_num_rows($rs) or die (mysql_error());
echo '<table border="1" width="100%">';
for($i=0; $i<$count; $i++){
$name = mysql_result($rs, $i, "pname") or die (mysql_error());
$price = mysql_result($rs, $i, "price") or die (mysql_error());
echo "<tr>";
echo "<td>";
echo "<input name='Bakery[]' type='checkbox' value='$name'/>";
echo "</td>";
echo "<td>";
echo "$name";
echo "</td>";
echo "<td>";
echo "$price";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
Thanks :)
I would highly recommend using MySQLi (http://php.net/mysqli) instead on the mysql extension - mysql is deprecated and should not be used in new projects. MySQLi (or PDO) has been the recommended extension since 2004.
--------------------------------------------------
# If at all possible, you should avoid using the mysql_* functions. #
Existing code should be updated to avoid performance and security problems.
Warning
This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future.
Instead, either the mysqli (http://php.net/mysqli) or PDO_MySQL (http://php.net/PDO) extension should be used.
See also the MySQL API Overview (http://php.net/mysqlinfo.api.choosing) for further help while choosing a MySQL API.
dasht
07-19-2013, 05:16 PM
But mysqli_result() did not work. and with mysql_result() it give that above error. Is there an other function to get result except mysqli_result() ???????
The problem you were encountering was mixing the two extensions - they aren't interchangeable. But mysqli has a similar syntax. The main difference in this case is that you'd need to pass the connection when querying and retrieving the results:
<?php
$DB = mysqli_connect( 'database_host','username','password','database_name' );
$query = mysqli_query( $DB,"select whatever ..." );
if( $query ){
while( $row = mysqli_fetch_assoc( $result ) ){
// do your stuff
}
}
dasht
07-20-2013, 08:27 AM
Ok i will do it in this way.
I also want to ask some more from you.
Can we make just one dynamic page to show the detail of all products of different categories when user clicked that product then its information should be displayed on that page. IF YES? then can you give me any idea.
Two ideas that are coming in my mind are
1- In that page i write information and store its product and category name through some variables. But in this case i cant change other info except their name and category prices etc.
for example :
$name is a gourmet product of this $category and some information regarding prices....
but in this manner i cant enter specific information related to that product. Because ofcourse every product have different information
2- For this if i made some file of information for these products seprately and INCLUDE those in that page. But in this case i still have to make files then if NOT PAGES.......
Is there an other way? Then please suggest.... Thanks :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.