SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face help with converting from using mysql to using mysqli_query.

    hello everyone

    i am fairly new to php and welcome the help that you guys give to new comers.

    i have two questions;

    QUESTION ONE


    i am trying to convert from using mysql to mysqli

    the FUNCTION below works perfectly when used with mysql. but when i converted it to mysqli i keep getting the following error message;

    Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\database.com\cms\db_fns.php on line 123

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\database.com\cms\db_fns.php on line 125

    i enlose the function
    PHP Code:

    //[COLOR="#0000FF"]fuctions in mysql that work[/COLOR]


    // first connect to database.


     
    function db_connect()
      {
            
    $connection mysql_connect('localhost''database''password');
            
            if(!
    $connection)
        {
            return 
    false;
        }
        if(!
    mysql_select_db('database'))
        {
            return 
    false;
        }
            
            return  
    $connection;
      }

    // second, extract information from database 


        
    function find_user($id)
        {
           
    db_connect();
            
            
    $query =  sprintf("SELECT 
                             DOB_D, 
                             DOB_D, 
                             DOB_D, 
                             health, 
                             weight, 
                             height ,
                number_smoked
                 FROM 
                userr_details
                WHERE 
                user_id = '%s'     
                   "
    mysql_real_escape_string($id)
                            );

            
    $result mysql_query($query);
            
          
    $row mysql_fetch_array($result);
            
            
    print_r$row);
        
        }
        
        echo 
    find_user('7');
        
            

    // [U][B]mysqli fuction[/B][/U]; 

    // same fuctions as above, but now attempting to use mysqli rather than mysql


    // Make the connection:
    $dbc = @mysqli_connect (DB_HOSTDB_USERDB_PASSWORDDB_NAME);

    if (!
    $dbc) {
        
    trigger_error ('Could not connect to MySQL: ' mysqli_connect_error() );
    }


    // (not sure if this is correct procedure but now convert connection to a fuction- so that it can then be used within a function. 





      
    function db_connect()
      {
            global 
    $dbc;
            
            
    $connection $dbc;
            
    if(!
    $connection)
        {
            return 
    false;
        }
        
        
        if(
    $connection)
        {
            
            return  
    $connection;
      }
        
      }
     

    // below is the 'target' function- i am now using[COLOR="#FF0000"] mysqli[/COLOR] rather than[COLOR="#FF0000"] mysql //[/COLOR],  to extract the data from the database.- it no longer works. 



        
    function find_user($id)
        {
          
    db_connect();
        
          
    $query =  sprintf("SELECT 
                             DOB_D, 
                             DOB_D, 
                             DOB_D, 
                             health, 
                             weight, 
                             height ,
                number_smoked
                 FROM 
                user_details
                WHERE 
                user_id = '%s'     
                   "
    ,($id)
                            );

            
    $result mysqli_query($query);
            
          
    $row mysqli_fetch_array($result);
            
            
    print_r$row);
        
        }
        
        echo 
    find_user('7');

    // [COLOR="#FF0000"]  THE ERROR MESSAGES [/COLOR]

    Warningmysqli_query() expects at least 2 parameters1 given in C:\wamp\www\database.com\cms\db_fns.php on line 123

    Warning
    mysqli_fetch_array() expects parameter 1 to be mysqli_resultnull given in C:\wamp\www\database.com\cms\db_fns.php on line 125 

    QUESTION TWO


    I use the function below to sort out an array of data that is extracted from the database. my problem however is that the function is designed for mysql. however, i cant seem to locate the equivalent mysqli 'mysql_fetch_array' function.

    does anyone have any ideas how i can convert this fucntion to mysqli


    PHP Code:

     

        
    function db_result_to_array($result)
        {
            
    db_connect();
            
          
    $res_array = array();
            
            for (
    $count=0;  $row mysql_fetch_array($result) ; $count++)
            {
             
                 
    $res_array[$count] = $row;
            }
            
            return 
    $res_array;
        } 
    thank you very much everyone for your help

    warm regards

    Andreea 115

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by andreea115 View Post

    Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\database.com\cms\db_fns.php on line 123
    Like the error says, mysqli_query needs two parameters. See the manual: http://www.php.net/mysqli_query
    The first parameter should be the database connection.

    Quote Originally Posted by andreea115 View Post
    I use the function below to sort out an array of data that is extracted from the database. my problem however is that the function is designed for mysql. however, i cant seem to locate the equivalent mysqli 'mysql_fetch_array' function.
    http://www.php.net/manual/en/mysqli-...etch-array.php

    You are using the procedural style.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brilliant. thank you to guido2004 and the team at sitepoint for their help in this matter. i have been trying to work out this simple problem for months now.

    Many of us newbies to PHP are self-taught, so without the help of you oldhands it would simply be impossible for us to learn.

    warmest and sweetest regards

    Andreea

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    I'm self-taught in PHP myself and learned a lot here at the SP forums, so I'm glad I could be of help.


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
  •