Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    55
    Member #
    31209

    Php mysqli grab data

    Ok, So i am trying to view all the members in table row members. I go the following script from w3schools, and it gives this error. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in --/--/--/--/members.php on line 70

    Code is:
    Code:
    <?php 
    $sql = "SELECT * FROM users";
    echo "<table border='1'>
    <tr>
    <th>Username</th>
    <th>Lastname</th>
    </tr>";
    
    while($row = mysqli_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['username'] . "</td>";
      echo "<td>" . $row['website'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    
    ?>
    Can anyone help me?

    Thank you!

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,716
    Member #
    5580
    Liked
    718 times
    You are asking to fetch the results of the query, but you didn't actually do the query.

    Try this:
    PHP Code:
    <?php 
    $sql 
    "SELECT * FROM users";
    $result=mysqli_query($sql);

    echo 
    "<table border='1'> 
    <tr> 
    <th>Username</th> 
    <th>Lastname</th> 
    </tr>"
    ;  
    while(
    $row mysqli_fetch_array($result))   {   
    echo 
    "<tr>";   
    echo 
    "<td>" $row['username'] . "</td>";   
    echo 
    "<td>" $row['website'] . "</td>";   
    echo 
    "</tr>";  
     } 
    echo 
    "</table>";  
    ?>
    Last edited by mlseim; Jul 03rd, 2013 at 06:11 AM.


  4. #3
    Member
    Join Date
    Mar 2012
    Posts
    55
    Member #
    31209
    OK, So if I change the code to what you gave me, it gives these errors (still no data)

    Warning: mysqli_query() expects at least 2 parameters, 1 given in /xx/xx/xx/xx/members.php on line 64

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /xx/xx/xx/xx/members.php on line 71

    If I change the code you gave me to this, it takes away the first error.

    Code:
    <?php 
    $sql = "SELECT * FROM users";
    
    echo "<table border='1'> 
    <tr> 
    <th>Username</th> 
    <th>Lastname</th> 
    </tr>";  
    while($row = mysqli_fetch_array($result))
    $result=mysqli_query($sql);
       {   
    echo "<tr>";   
    echo "<td>" . $row['username'] . "</td>";   
    echo "<td>" . $row['website'] . "</td>";   
    echo "</tr>";  
     } 
    echo "</table>";  
    ?>


    How do I get rid of the second error?


  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,716
    Member #
    5580
    Liked
    718 times
    I guess im just used to mysql not mysqli ...

    Mysqli requires different fields.

    Youll have to consult google for mysqli tutorials. Im not on a real pc now, so i cant do any scripting.

    Or use mysql instead of mysqli.


  6. #5
    Member
    Join Date
    Mar 2012
    Posts
    55
    Member #
    31209
    Sorry, i miss typed. what you gave me got rid of the first error. It helped. thank you. but I am not sure how to fix the second error. ( the error ocors on the line that says "while $row =etc..."

    If you can figure it out from that,then thanks. if not,than still thanks. :-D

  7. #6
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,716
    Member #
    5580
    Liked
    718 times
    Look at the example below from the "php.net manual". This is typical of dealing with MySQLi (as opposed to MySQL).
    MySQLi is object oriented, so another step is added to create the array of multiple rows.

    Code:
    $query = "SELECT * FROM City";
    $result = $mysqli->query($query);
    
    while($row = $result->fetch_array())
    {
    $rows[] = $row;
    }
    
    foreach($rows as $row)
    {
    echo $row['CountryCode'];
    }

    See more about MySQLi:
    http://php.net/manual/en/mysqli-result.fetch-array.php

    Then look at MySQL ... you'll see the differences between MySQL and MySQLi


  8. #7
    Member
    Join Date
    Aug 2013
    Location
    Venice, Florida
    Posts
    38
    Member #
    37112
    mysgli is the preferred php handler by far..


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:30 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com