选择*不会在php mysql中打印任何内容

时间:2011-04-19 21:06:19

标签: php mysql database

我制作了一个php脚本,如下所示,并且输出是Record Added to Table

<?PHP

$user_name = "root";
$password = "";
$database = "test_db";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "INSERT INTO name (FirstName) VALUES ('bill')";
$result = mysql_query($SQL);

mysql_close($db_handle);

print "Records added to the database";
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

?>

现在看看我做的表的内容

<?PHP

$user_name = "root";
$password = "";
$database = "test_db";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "Select * from name";
$result = mysql_query($SQL);

print "$result";
mysql_close($db_handle);

print "Records added to the database";
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

?>

但输出是资源ID#3Records添加到数据库,而我想看到表的内容

5 个答案:

答案 0 :(得分:3)

无法像尝试一样将mysql_query的返回值发送到输出。

mysql_query返回一个结果集,例如与http://www.php.net/mysql_fetch_row一起使用。

示例:

while($row=mysql_fetch_array($result)) {
    print_r($row);
}

答案 1 :(得分:1)

$result包含mysql_query的返回值resource http://php.net/manual/en/function.mysql-query.php

使用mysql_fetch_assoc($result)mysql_fetch_array($result)或...来获取实际数据。

答案 2 :(得分:1)

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("Result: %s", $row[0]);  
}

答案 3 :(得分:1)

mysql_query()将返回一个结果集对象,您必须在循环中读取该对象以获取其行:

while($row = mysql_fetch_array()) {
   echo htmlspecialchars($row['column1']);
}

答案 4 :(得分:1)

如果您想从数据库中进行选择,请尝试以下方法:

<?PHP

$user_name = "root";
$password = "";
$database = "test_db";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "Select * from name";
$result = mysql_query($SQL);

 while ($row = mysql_fetch_array($result))
 {
  print $row[0]." Record is selected";
 }
mysql_close($db_handle);


}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

?>