Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2005
    Posts
    89
    Thanks
    2
    Thanked 0 Times in 0 Posts

    connection error (mysqli)

    Hello All,

    I have a script that connects to a database and displays a few images and text. It worked fine when running on my machine at home, but I brought the code and database to another machine, and got a few errors. I'm pretty sure its to do with how my php.ini file is set up but I can seem to figure it out.

    Heres the error message I get

    Warning: mysqli::mysqli() [function.mysqli]: (/1045): in C:\wamp\www\work\greg\db_fns.php on line 5

    Warning: mysqli::query() [function.query]: Couldn't fetch mysqli in C:\wamp\www\work\greg\index.php on line 9

    Fatal error: Call to a member function fetch_assoc() on a non-object in C:\wamp\www\work\greg\index.php on line 13
    and heres the code for db_fns.php

    PHP Code:
    <?php

    function db_connect()
    {
       
    $handle = new mysqli('localhost''content''password''content'); 
       if (!
    $handle)
       {
         return 
    false;
       }
       return 
    $handle;
    }

    function 
    get_writer_record($username)
    {
      
    $handle db_connect();
      
    $query "select * from writers where username = '$username'";
      
    $result $handle->query($query);
      return(
    $result->fetch_assoc());
    }

    function 
    get_story_record($story)
    {
      
    $handle db_connect();
      
    $query "select * from stories where id = '$story'";
      
    $result $handle->query($query);
      return(
    $result->fetch_assoc());
    }

    function 
    query_select($name$query$default='')
    {
      
    $handle db_connect();

      
    $result $handle->query($query);

      if (!
    $result)
      {
        return(
    '');
      }

      
    $select  "<select name='$name'>";
      
    $select .= '<option value=""';
      if(
    $default == ''$select .= ' selected ';
      
    $select .= '>-- Choose --</option>';

      for (
    $i=0$i $result->num_rows$i++) 
      {
        
    $option $result->fetch_array();
        
    $select .= "<option value='{$option[0]}'";
        if (
    $option[0] == $default
        {
          
    $select .= ' selected';
        }
        
    $select .=  ">[{$option[0]}] {$option[1]}</option>";
      }
      
    $select .= "</select>\n";

      return(
    $select);
    }

    ?>
    and here is the code for index.php

    PHP Code:
    <?php
      
    include_once('db_fns.php');
      
    //include_once('header.php');

      
    $handle db_connect();

      
    $pages_sql 'select * from pages order by code';
      
    $pages_result $handle->query($pages_sql);

      echo 
    '<table border="0" width="400">';
          
      while (
    $pages $pages_result->fetch_assoc()) 
      {
        
    $story_sql "select * from stories
                      where page = '{$pages['code']}'
                      and published is not null
                      order by published desc"
    ;
       
        
    $story_result $handle->query($story_sql);
        
        if (
    $story_result->num_rows
        {
          
    $story $story_result->fetch_assoc();
          echo 
    "<tr>
                <td>
                  <h2>{$pages['description']}</h2>
                  <p>{$story['headline']}</p>
                  <p align='right' class='morelink'>
                    <a href='page.php?page={$pages['code']}'>
                    Read more {$pages['code']} ...
                    </a>
                  </p>
                </td>
                <td width='100'>"
    ;
          if (
    $story['picture'])
          {
            echo 
    '<img src="resize_image.php?image=';
            echo 
    urlencode($story[picture]);
            echo 
    '&max_width=80&max_height=60"  />';
          }
          echo 
    '</td></tr>';
        }
      }
      echo 
    '</table>';

      
    //include_once('footer.php');
    ?>
    I've searched around but can't seem to figure it out. Any thoughts?

    Thanks all

  • #2
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    mysqli appears to require MySQL v4.1 or above. does your new machine have that?
    "There is more than one way to do it."


  •  

    Posting Permissions

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