PHP代码无效

时间:2013-01-28 14:44:22

标签: php mysql lamp

这是我的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 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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

答案 2 :(得分: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'];
};

答案 3 :(得分:0)

这是初学者的最佳网站 - 'MySQLi' for Beginners

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

Php site to backup my anwser

一些示例代码

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


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

}