php - I can't connect to db or pull data -


i using same code `

php $postid = 41;  <!-- hidden items , variables. elements not revealed !-->   <span  id="gamelength"><?php   // mysql connect configuration  $dbname="my_db";  $host="localhost";  $user="guessthe";  $dbh=mysql_connect ($host,$user,"correctpassword?") or die ('i cannot connect database because: ' . mysql_error(). '');  mysql_select_db ("$dbname") or die('i cannot select database because: ' . mysql_error());  $sql="select * games postid = $postid";  $result=mysql_query($sql);  $rows=mysql_fetch_array($result);  $gameid = $rows['id'];   $game100s = $rows['game100s'];   $gamesplayedalltime = $rows['gamesplayed'];   $gamespointsalltime = $rows['gamescore'];   $gamelength = $rows['gamelength']; // number of questions  $gamescore = $rows['gamescore'];  $gametype = $rows['gametype'];  $gametitle = $rows['gamesubtitle'];  echo $gamelength; 

there value in gamelength row! can't code pull of rows! idea i'm doing wrong?

you're using mysql, depcirated - , phased out. should use mysqli or pdo instead. also, $postid defined outside php-tag? might copy/paste mistake? anyway, can try code below, in mysqli:

<?php      $postid = 41;  ?>  <!-- hidden items , variables. elements not revealed !-->   <span  id="gamelength"><?php   // mysql connect configuration $dbname = "my_db"; $host   = "localhost"; $user   = "guessthe";  // connecting database $mysqli = new mysqli($host, $user, "correctpassword?", $dbname); if ($mysqli->connect_errno) {     // if here, connection failed     echo "failed connect mysql: (".$mysqli->connect_errno.") ".$mysqli->connect_error; }  $sql ="select * games postid = $postid";  if ($result = $mysqli->query($sql)) {     // if query sucsessfull, can rows     while ($row = $result->fetch_assoc()) {         $gameid             = $row['id'];          $game100s           = $row['game100s'];          $gamesplayedalltime = $row['gamesplayed'];          $gamespointsalltime = $row['gamescore'];          $gamelength         = $row['gamelength']; // number of questions         $gamescore          = $row['gamescore'];         $gametype           = $row['gametype'];         $gametitle          = $row['gamesubtitle'];     } } else {     // if query failed, here }  echo $gamelength; ?> 

i see people commenting need put $postid variable inside quotes in query, when using double-quotes (") variables posted, it's not needed. note things case-sensitive, if results doesn't show, check spelling-mistakes.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -