Javascript和PHP MySQL

时间:2011-01-23 09:28:59

标签: javascript mysql printing

<?php
$host = 'localhost';
$dbusername ='root';
$dbpassword ='';
$database ='mp19';

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

if (!$link)
  {exit("Connection Failed: " . $link);}

$sql="(SELECT * FROM 3-33)";
$result=mysql_query($sql, $link);


if (!$result)
  {exit("Error in SQL");}

echo "<table><tr>";
echo "<th>Date</th>";
while ($row=mysql_fetch_assoc($result))
  {
  var_dump($row);
  echo "<td>$row['date']</td></tr>";
  }
mysql_close($link);
?> 

连接成功但数据不会从phpmysql中获取,也不会在表中打印。为什么会这样?

2 个答案:

答案 0 :(得分:1)

我认为你犯了一些错误。您似乎缺乏对这些功能如何工作的基本了解。几乎所有这些变量都返回一些变量(如链接资源或行数组),您可以在其余代码中使用它们。如果调用失败,这些函数将返回false。因此,您应该检查值是否显式为false(通过使用===运算符),并且只有在一切正常时才会继续。然后,您必须使用函数的结果值,而不是函数本身。 mysql_query只是调用,而不是结果。

<?php
$host = 'localhost';
$dbusername ='root';
$dbpassword ='';
$database ='mp19';

$link = mysql_connect('localhost', 'root','12345678');

if ($link === false)
{
    exit("Connection Failed: " . mysql_error());
}

if (mysql_select_db('mp19', $link) === false)
{
    exit("Could not select database mp19: " . mysql_error());
}

// Is '3-33' really your table name?! You should pick another name. 
$sql = "(SELECT * FROM 3-33)";

$resultset = mysql_query($sql, $link);

// (!$sql)? $sql SQL is always set here. It is just the string 
if ($resultset === false) 
{
    exit("Error in SQL: " . mysql_error());
}

echo "<table><tr>";
echo "<th>Date</th>";

while ($row = mysql_fetch_assoc($resultset))
{
    $date = $row['fieldname'];
    echo "<td>$date</td></tr>";
}

mysql_close($link);

?> 

答案 1 :(得分:0)

查询无效 - 尝试在命令行加载mysql客户端并运行它:

mysql -uroot mp19 -p
(SELECT * FROM 3-33)
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3-33' at line 1