如何使用php从mysql中的单个表中获取所有数据?

如何使用php从mysql中的单个表中获取所有数据?

问题描述:

I'm trying to simply get all the data from a mysql table using the following code:

$dbc = mysqli_connect('host', 'user', 'password', 'table');
$q = 'SELECT * FROM users';
$r = mysqli_query($dbc, $q);

$user_array = array();
    while ($row = mysql_fetch_array($r)) 
        {
        $user_array[]=$row;
        }
    echo "<br />";
    echo "print r of user_array: <br />";
    print_r($user_array);

i'm getting nothing. I looked at this tutorial (http://www.w3schools.com/php/php_mysql_select.asp), among others, but all they do is confuse me.

What am I doing wrong?

我正在尝试使用以下代码从mysql表中获取所有数据: p> \ n

  $ dbc = mysqli_connect('host','user','password','table'); 
 $ q ='SELECT * FROM users'; 
 $ r = mysqli_query(  $ dbc,$ q); 
 
 $ user_array = array(); 
 while($ row = mysql_fetch_array($ r))
 {
 $ user_array [] = $ row; 
} 
 \ echo  “&lt; br /&gt;”; 
 echo“print r of user_array:&lt; br /&gt;”; 
 print_r($ user_array); 
  code>  pre> 
 
 

我什么都没得到。 我查看了本教程( http://www.w3schools.com/php/php_mysql_select.asp a>)等等,但他们所做的只是让我困惑。 p>

我做错了什么? p> div>

try changing this

mysql_fetch_array($r)

to

mysqli_fetch_array($r)

You have connected via the procedural MySQLi API (mysqli_connect()), but you are attempting to use the old mysql_fetch_array() from the old mysql_*() API. They aren't compatible.

You need to fetch via mysqli_fetch_array() or mysqli_fetch_assoc() instead. mysqli_fetch_assoc() is recommended unless you really need numeric keys in addition to column names in the output $row:

// Check for errors.
if ($r) {
  while ($row = mysqli_fetch_assoc($r)) {
    $user_array[] = $row;
  }
}
else echo mysqli_error($dbc);

What you're working with now is a simple query with no parameters, just a SELECT * without a WHERE clause. For the future, when you start adding WHERE conditions, you'll want to start reading about prepared statements and mysqli::prepare().