0

这是我的php代码

<?php
try
{   
    $mysqlConnection = mysqli_connect('localhost', 'root', 'root');
    if($mysqlConnection)
    {
        echo "Connected"."\n";
        mysqli_select_db('test',$mysqlConnection);
        $result = mysqli_query("SELECT * from tbl_details");
        $row = mysqli_fetch_array($result);
        echo strlen($row);
    }
    else
    {
        echo "Not connected";
    }
}
catch(Exception $exp)
{
    echo $exp;
}   
?>

我已经验证了凭据,并且表中也有数据,但我仍然没有得到任何记录。任何任何指出为什么?

更新::

工作代码

<?php
try
{   
    $mysqlConnection = mysqli_connect('localhost', 'root', 'root','test');
    if($mysqlConnection)
    {
        echo "Connected"."\n";
        $result = $mysqlConnection->query("SELECT NAME,State FROM tbl_details");
        echo $result->num_rows."\n";
    }
    else
    {
        echo "Not connected";
    }
}
catch(Exception $exp)
{
    echo $exp;
}   
?>
4

4 回答 4

4
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$resultaat = $mysqli->query("SELECT * FROM tabel");
while ($record = $resultaat->fetch_assoc())
{
  print_r($record);
}
于 2013-01-28T14:47:56.323 回答
0

您缺少这段代码来显示结果:

while($row = mysql_fetch_array($result))
  {
  echo $row['name of column'] . " " . $row['name of column'];
  echo "<br />";
  }

看这个链接:http ://www.w3schools.com/php/php_mysql_select.asp

于 2013-01-28T14:49:19.190 回答
0

试试下面的代码:

$link = mysql_connect('localhost','root','');
$db_selected = mysql_select_db('test',$link);

$result = mysql_query("SELECT * from tbl_details");
while($row = mysql_fetch_array($result))
{
    echo $row['fieldname'];
};
于 2013-01-28T14:49:36.913 回答
0

这是初学者的最佳网站 - 初学者的“MySQLi”

并记住不要使用 mysql_ 函数,因为它们已被弃用。

用于备份我的 anwser 的 PHP 站点

一些示例代码

    $db = new mysqli('localhost', 'user', 'pass', 'demo');


    if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');

}

于 2013-01-28T14:51:15.500 回答