如何使用$ _session []名称为mysql数据库定义一个表?

时间:2014-04-12 22:01:29

标签: php mysql session

我需要帮助协调使用Session []

的方法
$_SESSION['loginname']=jordan@yahoo.com;
$table_name=$_SESSION['loginname']; 

是数据库中表的名称。

$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "$table_name";

我不断获得Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

这导致这组代码出现问题

$mysql = "SELECT DISTINCT quiz_name FROM $table";
$mydata = mysql_query($mysql,$con);
while($records = mysql_fetch_array($mydata)){

然后我添加mysql_error()并返回

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 ''jordan@yahoo.com'' at line 1

我做错了什么?谢谢

quiz_main.php

<?php
session_start();
?>
<!DOCTYPE html> 
<html>

<head>
  <title>Quiz</title>

  <link rel="stylesheet" type="text/css" href="css/style.css" /> 
</head>

<body>
  <div id="main">

    <header>
      <div id="welcome">
        <h2>Prairie View A&amp;M University</h2>
      </div><!--close welcome-->                
    </header>   

    <nav>
      <div id="menubar">
        <ul id="nav">
          <li><a href="index.php">Home</a></li>
          <li><a href="user-account.php">Account Info</a></li>
          <li class="current"><a href="quiz_main.php">Quiz</a></li>
          &nbsp &nbsp &nbsp &nbsp &nbsp
<?php
if($_SESSION['loginname'])
echo $_SESSION['loginname'].", "."<a href='user-account.php'>Account</a>"."     "."<a href='logout.php'>Logout</a>";

else
    die("You must login");
?>
        </ul>
      </div><!--close menubar-->    
    </nav>      

    <div id="site_content">

    <h2 style="font-size:50px" align="center" > Quiz Page</h2>

   <h2> All Quizzes:</h2></br>
    <?php
    $table_name=$_SESSION['loginname'];
    $username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = $table_name;

$con = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MYsql");
// echo "Connected to mysql<br>";

$db = mysql_select_db("$database")
 or die("Could not select Basketball_database");
 //echo "Connected to database";

//form for selecting quiz
echo "<form action=\"/xampp/Website_DataBase/Pvamu_website/quiz/index.php\" method=\"post\">";

$mysql = "SELECT DISTINCT quiz_name FROM '$table_name'";
$mydata = mysql_query($mysql,$con);

if($mydata === FALSE) {
die(mysql_error()); // TODO: better error handlin
}


while($records = mysql_fetch_array($mydata)){
    $quizname=$records['quiz_name'];

    echo "<input type=radio name=name_quiz value='".$records['quiz_name']."'>".$records['quiz_name']."<br>";
}


echo "<input type=submit value=Submit Continue>";
echo "</form>"; 


    ?>
    <a href="quiz_folder/coach_quizzes.php">Creat a Quiz</a>
      <div id="content">
        <div class="content_item">


      </div><!--close content_container-->          
    </div><!--close content_item-->
</div><!--close content-->   
    </div><!--close site_content-->     

    <footer>
     <a href="index.php">Home</a> | <a href="photos.php">Photos</a> | <a href="videos.php">Videos</a> | <a href="schedule.php">Schedule</a> | <a href="contact.php">Contact</a><br/><br/>

    </footer>   

  </div><!--close main-->

  <!-- javascript at the bottom for fast page loading -->
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <script type="text/javascript" src="js/image_slide.js"></script>  

</body>
</html>

2 个答案:

答案 0 :(得分:0)

1 删除引号

$hostname = "localhost";
$database = "basketball_database";
$table = $table_name;

2 将您的代码更改为

$mysql = "SELECT DISTINCT quiz_name FROM '$table'";
$mydata = mysql_query($mysql);
while($records = mysql_fetch_array($mydata)){

如果您的页面中没有开始会话..

在页面顶部添加session_start()

答案 1 :(得分:0)

如果表名中有特殊字符,则需要反引号:

$table = str_replace('`', '``', $table); // escape backticks in $table

$mysql = "SELECT DISTINCT quiz_name FROM `$table`";

@.字符需要反引号,许多其他可能的字符也需要反引号。请快速阅读SQL注入: