SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mysqli Fetch array and mysqli result

    HI all

    i am trying to improve my Script i am working on but when i attempted to do a see if the person's dsl is greater than an x amount i get this error being displayed on the page.

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\Program Files\xampp\htdocs\BC Clan Scripts\index.php on line 49

    I Tried to do some research on this errror one website told me i shud use mysqli_multi_query or somthing That did not do the trick.

    Here is my Code as of now

    Code:
    function calcDSLogin($username,$today,$LastedLogin)
    {
    	global $today;
    	global $LastedLogin;
                 global $username;
    	include("dbconnect.php");
    	$Query="SELECT TO_DAYS($today)-TO_DAYS($LastedLogin) FROM members WHERE username='$username'";
    	$result = mysqli_query($con,$Query);
    	$days = mysqli_fetch_array($result);
    	return $days[0];
    }
    Why do i get this error how can i fix this error thanks

    Thanks,
    Will

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try replacing:
    PHP Code:
    $result mysqli_query($con,$Query); 
    with
    PHP Code:
    $result mysqli_query($con,$Query) or die(mysqli_error()); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using die() is fine for debugging but the reason for the error is that your query is failing. Because it is failing, mysqli_query is returning false. mysqli_result expects a valid resource id from running the query.

    I recommend using some if constructs in your code to catch these errors. At any stage in using the database from connection and authentication, through selecting the database to use, to executing a query, things can go wrong. That includes things that don't have anything to do with your code. handle the errors gracefully, you can do that and still provide yourself with feedback duing development as to what might have happened.

    In your case.

    PHP Code:
    if($result mysqli_query($connection,$query))
    {
       
    $name mysqli_result($result,0,'name');
    }
    else
    {
       
    // Okay for development
       
    echo($query.' '.mysqli_error());
       
    // for production
       // echo('A friendly message');

    Making an application stable depends on your ability to predict what could go wrong. Making an application easy to use depnds in part, on how your applications reacts when something does go wrong.


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
  •