我无法连接到db或提取数据

时间:2015-07-11 02:41:58

标签: php mysql

我正在使用相同的代码`

<form>
    <select ng-model="selectFilter">
        <option value="incident_at">Incident Date</option>
        <option value="ip_addr" selected="selected">IP Address</option>
        <option value="mac_addr">MAC Address</option>
        <option value="created_at">Created On</option>
        <option value="author">Author</option>
    <select>
    <input type="text" placeholder="Search by IP" ng-model="filterTable.ip_addr">
</form>

gameLength行中有一个值!我无法获取此代码来拉出任何行!知道我做错了吗?

3 个答案:

答案 0 :(得分:1)

您的代码中存在许多错误

试试这个......

<?php

$postId = 41;

?>
<!-- hidden items and variables. Elements that will not be revealed !--> 

<span  id="gameLength">
<?php 

// MySQL connect configuration

$host = "localhost";
$dbname = "my_db";
$user = "username";
$password = "password";

$dbh = mysql_connect ($host,$user,$password) or die ('I cannot connect to the database because: ' . mysql_error() . '');

mysql_select_db($dbname, $dbh) or die('I cannot select the database because: ' . mysql_error());

$sql = "SELECT * FROM games WHERE postId='$postId'";
$result = mysql_query($sql);

while($rows = mysql_fetch_array($result)){
   $gameId = $rows['id']; 
   $game100s = $rows['game100s']; 
   $gamesPlayedAllTime = $rows['gamesPlayed']; 
   $gamesPointsAllTime = $rows['gameScore']; 
   $gameLength = $rows['gameLength']; // get number of questions
   $gameScore = $rows['gameScore'];
   $gameType = $rows['gameType'];
   $gametitle = $rows['gameSubTitle'];

   echo $gameLength;
}

?>

答案 1 :(得分:1)

您正在使用已被删除的MySQL,并将逐步淘汰。您应该使用MySQLi或PDO。另外,您的$postId是在PHP标记之外定义的吗?可能只是一个复制/粘贴错误?无论如何,您可以尝试以下代码,即MySQLi:

<?php 
    $postId = 41; 
?>

<!-- hidden items and variables. Elements that will not be revealed !--> 

<span  id="gameLength"><?php 

// MySQL connect configuration
$dbname = "my_db";
$host   = "localhost";
$user   = "guessthe";

// Connecting to the database
$mysqli = new mysqli($host, $user, "correctPassword?", $dbname);
if ($mysqli->connect_errno) {
    // If we are here, the connection failed
    echo "Failed to connect to MySQL: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
}

$sql ="SELECT * FROM games WHERE postId = $postId";

if ($result = $mysqli->query($sql)) {
    // If the query was sucsessfull, we can get the rows
    while ($row = $result->fetch_assoc()) {
        $gameId             = $row['id']; 
        $game100s           = $row['game100s']; 
        $gamesPlayedAllTime = $row['gamesPlayed']; 
        $gamesPointsAllTime = $row['gameScore']; 
        $gameLength         = $row['gameLength']; // get number of questions
        $gameScore          = $row['gameScore'];
        $gameType           = $row['gameType'];
        $gametitle          = $row['gameSubTitle'];
    }
} else {
    // If the query failed, do something here
}

echo $gameLength;
?>

我看到有人评论你需要在查询中将$postId变量放在引号内,但是当使用双引号(&#34;)时,会发布变量,所以它不会真的需要。另请注意,事情区分大小写,因此如果您的结果不显示,请检查拼写错误。

答案 2 :(得分:0)

你需要修复这是你的代码,这应该可以解决错误。

$sql="SELECT * FROM games WHERE postId ='".$postId."' ";

如果您想要所有记录,可以使用while循环。这是一些伪代码。

while($row = mysql_fect_assoc($query)){
      echo $row["THE THING YOU WANT"];
      ...
}