0

错误消息:“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'portfolio' 附近使用正确的语法”

这是有问题的代码:

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$host="****"; // Host name 
$username="****"; // Mysql username 
$password="****"; // Mysql password 
$db_name="a9307665_br"; // Database name 
$tbl_name="portfolio"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die(mysql_error()); 
mysql_select_db("$db_name")or die(mysql_error());

$query_getPosts = "SELECT post_id, title, updated FROM $tbl_name ORDER BY updated DESC";
$getPosts = mysql_query($tbl_name) or die(mysql_error());
$row_getPosts = mysql_fetch_assoc($getPosts);
$totalRows_getPosts = mysql_num_rows($getPosts);
?>

我认为错误是基于 MySQL 的,这是导致错误的代码行:

$query_getPosts = "SELECT post_id, title, updated FROM $tbl_name ORDER BY updated DESC";

有人可以指出我正确的方向吗?

更新:结果:echo $query_getPosts;

SELECT post_id, title, updated FROM `portfolio` ORDER BY updated DESC
4

2 回答 2

2

这一行是错误的:

$getPosts = mysql_query($tbl_name) or die(mysql_error());

对此正确:

$getPosts = mysql_query($query_getPosts) or die(mysql_error());
于 2012-05-20T17:54:36.337 回答
0

您可以尝试用反引号包裹您的表名 -

$query_getPosts = "SELECT post_id, title, updated FROM `$tbl_name` ORDER BY updated DESC";

您还将表名指定为查询字符串:)

mysql_query($tbl_name)

尝试

mysql_query($query_getPosts)
于 2012-05-20T17:51:33.390 回答