Results 1 to 6 of 6

Thread: Warning: mysql_result() expects parameter 1 to be resource, object given in C:\xampp\

  1. #1
    Join Date
    May 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Warning: mysql_result() expects parameter 1 to be resource, object given in C:\xampp\

    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 Code:
    <?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
    Last edited by Beverleyh; 07-18-2013 at 12:39 PM. Reason: formatting

  2. #2
    Join Date
    May 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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 Code:
    <?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
    Last edited by keyboard; 07-19-2013 at 07:23 AM. Reason: Format: Php Tags [php][/php]

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I would highly recommend using 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.
    Quote Originally Posted by php.net (official PHP website)
    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 or PDO_MySQL extension should be used.
    See also the MySQL API Overview for further help while choosing a MySQL API.

  4. #4
    Join Date
    May 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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() ???????

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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 Code:
    <?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
        
    }
    }

  6. #6
    Join Date
    May 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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

Similar Threads

  1. Resolved mysql_query expects parameter 2 to be resource
    By Priyo in forum PHP
    Replies: 2
    Last Post: 02-21-2012, 11:20 PM
  2. Warning: rand() expects parameter 1
    By mysql in forum MySQL and other databases
    Replies: 1
    Last Post: 11-14-2011, 03:53 PM
  3. Replies: 0
    Last Post: 01-04-2011, 01:17 PM
  4. Replies: 1
    Last Post: 09-15-2009, 08:37 PM
  5. Replies: 33
    Last Post: 04-14-2009, 08:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •