一直试图使用程序化的PHP连接到数据库,但我得到错误数组到字符串转换

时间:2014-06-28 00:34:15

标签: php

<?php
function connect()
{
$conn = new mysqli('localhost','root','','test' ) or
    die ('There is a problem connecting to     the    db.');
return $conn;
}
function select_Db($conn){
   $stmt = $conn->prepare('SELECT id,UserName,Password FROM users') or
       die('Problem with query.');
   $stmt->execute();
   $stmt->bind_result($id,$UserName,$Password);
   $rows = array();
   while ( $row = $stmt->fetch() ) 
       {
     $item = array(
       "id"=> $id, 
       "UserName"=>$UserName, 
       "Password" =>$Password  
     );  
     $rows[] = $item;
   }
   return $rows;
}

一直尝试使用程序化的php连接到数据库,但我得到错误数组到字符串转换。以下是代码和相应的错误。请帮助跟踪代码中的错误。

Notice: Array to string conversion in C:\wamp\www\MYSQL_Project\index.php.php on line 15
Notice: Array to string conversion in C:\wamp\www\MYSQL_Project\index.php.php on line 16
Notice: Array to string conversion in C:\wamp\www\MYSQL_Project\index.php.php on line 17

1 个答案:

答案 0 :(得分:0)

由于您正在SELECT尝试定期query()。使用预准备语句

无需过度复杂化
$res = $conn->query('SELECT id,UserName,Password FROM users');
$rows = array();
while( $row = $res->fetch_assoc() ) $rows[] = $row;