2015-10-15 101 views
0

因此,我試圖在我最近在我的WAMP服務器2.5版上設置的網站上從我的數據庫中的一個數據庫中回覆每行用戶。我可能是錯的,但我已經谷歌搜索,也搜索這個網站的帖子......我不認爲有這樣的問題,像這個問題還沒有解決。無法將PHP變量分配給mysqli查詢

在我通過以下教程/練習使用PHP的練習中,當然我發現MySQL最近更喜歡使用PDO或MySQLi,所以我決定嘗試使用MySQLi。

奇怪的是,我已經包含配置MySQL連接的PHP文件,並且我知道連接變量被正確調用,因爲我甚至已經打印出變量正在查詢的結果分配到:

mysqli_result Object ([current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 1 [type] => 0) 

以上是輸出,當我使用:

print_r($db_con->query("SELECT * FROM users")); 

然而,當我嘗試的結果分配給一個變量,比如$ user_rs,並打印變量的值,我得到「注意:未定義的變量:user_rs在li上的C:\ wamp \ www \ index.php中ne 58.「

這是我的文件。

dbconfig.php:

<?php 
    $db_con= new mysqli("localhost","root","abc123", "OTonGadgetHub"); 
    if($db_con->connect_errno){ 
     echo $db_con->connect_error; 

    } 

?> 

的index.php:

<?php session_start(); 
include ("dbconfig.php"); ?> 
<html> 
<head> 
    <title>My Webpage</title> 
    <link rel = "stylesheet" type = "text/css" href = "site.css" /> 
    <link rel="shortcut icon" href="index.html?img=favicon" type="image/ico" /> 
    <script> 
      function validatingForm(){ 
       var x; 
       var y; 
       x = document.getElementById('nameCheck').value; 
       y = document.getElementById('password').value; 
       if(x !="" && y !=""){ 
        return true; 
        } 
        else if(x =="" && y == ""){ 
         document.getElementById('errorMsg').innerHTML='<font color="red">(required) Name:</font>'; 
         document.getElementById('errorPass').innerHTML='<font color="red">(required) Password:</font>'; 
         return false; 
        } 
        else if(x =="" && y!=""){ 
         document.getElementById('errorMsg').innerHTML='<font color="red">(required) Name:</font>'; 
         document.getElementById('errorPass').innerHTML='<font color="blue">Password:</font>'; 
         return false; 
        } 
        else if(y =="" && x!=""){ 
         document.getElementById('errorPass').innerHTML='<font color="red">(required) Password:</font>'; 
         document.getElementById('errorMsg').innerHTML='<font color="blue">Name:</font>'; 
         return false; 
         } 

      } 
     </script> 
</head> 

<body> 
    <?php include("header.php"); ?> 
       <?php 

       if (isset($_SESSION['user'])) 
         echo "<p class ='welcome' id='greeting'> Hi, ". $_SESSION['user'] . "! Welcome to the site!</p>"; 
       else 
         echo "<p class ='welcome' id='greeting'> Please Login:</p> 
             <form action='welcome.php' method='post'> 
              <center><b id = 'errorMsg'>Name:</b> 
              <input type='text' id='nameCheck' name = 'username' /></center> 
              <br /> 
              <center><b id='errorPass'>Password:</b> <input type='text' id ='password' name = 'password'/></center> 
              <br /><br /> 
              <center><input type='submit' value='Log In' onClick='return validatingForm()'/></center> 
             </form>"; 
       ?> 
      <?php 
      if (isset($_SESSION['user'])) 
      $user_rs = $db_con->query("SELECT * FROM users"); 
       print_r($db_con->query("SELECT * FROM users")); 
       print_r($user_rs); 
       echo "<center><h1> User List:</h1> 
        <table border='1'> 
        <tr> 
        <td><b>User</b></td> 
        <td><b>Login password</b></td> 
        </tr> 
        <tr>"; 
        while($record = $user_rs->fetch_object()){ 
         echo "<td>" . $record['id'] . "</td>"; 
         echo "<td>" . $record['username'] . "</td>"; 
         echo "</tr>"; 
        } 
        echo"</table></center>"; 
      ?> 
      <?php if (isset($_SESSION['user'])) 
       echo "<center><a href='logout.php'>Logout</a></center>";?> 

      <p class = "content"> This is a page that is a scrap work in progress. </p> 


      <?php include("footer.php"); ?> 
    </body> 
</html> 

我有其他的文件,但相信這些是問題的範圍之外。不過,如果需要,我會很樂意提供其他文件。

提醒:我擔心的事實是,我基本上不能使用應該分配給$ user_rs的結果。主要的是,即使我似乎已經正確地將它分配給查詢,它似乎沒有任何作用。先謝謝您的幫助。

+0

你正在提取$ reco rd作爲一個對象,但試圖顯示它,就好像它是一個數組:'$ record-> id'和'$ record-> username' –

+0

正如@Mark所說 - 試試這個語法:'$ record-> id'和'$ record-> username'〜但爲什麼要運行查詢兩次? – RamRaider

+0

@RamRaider爲什麼運行查詢兩次?我不確定我明白你的意思。 –

回答

0

這顯然是一個錯字錯誤。

在本質上,它要求如何分配到的表達結果的變量,並且答案是一樣明顯,因爲它可以:

$user_rs = $db_con->query("SELECT * FROM users"); 

和所討論的特定問題可以bolied向下

「爲什麼我收到Undefined variable: user_rs錯誤從下面的代碼

 $user_rs = $db_con->query("SELECT * FROM users"); 
     print_r($user_rs); 

答案將是「這是不可能的」。

檢查您正在運行的文件和類似的東西。

+0

關於這種錯誤,你有一個很好的觀點,它不應該發生在我的知識上。我試圖在很多挫敗之後在dbconfig裏設置一個變量,並且出於某種原因調用該變量可以工作得很好。這讓我覺得這是一個奇怪的範圍問題。也許一個字符在我的PHP文件中誤讀? –

0

您的數據庫查詢是將記錄集作爲對象提取,但您試圖使用數組語法引用這些字段。

<html> 
<head> 
    <title>My Webpage</title> 
    <link rel = "stylesheet" type = "text/css" href = "site.css" /> 
    <link rel="shortcut icon" href="index.html?img=favicon" type="image/ico" /> 
    <script> 
     function validatingForm(){ 
      var x; 
      var y; 
      var e; 
      var p; 

      x = document.getElementById('nameCheck').value; 
      y = document.getElementById('password').value; 
      e = document.getElementById('errorMsg'); 
      p = document.getElementById('errorPass'); 

      if(x !="" && y !=""){ 
       return true; 
      } else if(x =="" && y == ""){ 
        e.innerHTML='<font color="red">(required) Name:</font>'; 
        p.innerHTML='<font color="red">(required) Password:</font>'; 
        return false; 
      } else if(x =="" && y!=""){ 
        e.innerHTML='<font color="red">(required) Name:</font>'; 
        p.innerHTML='<font color="blue">Password:</font>'; 
        return false; 
      } else if(y =="" && x!=""){ 
        e.innerHTML='<font color="red">(required) Password:</font>'; 
        p.innerHTML='<font color="blue">Name:</font>'; 
        return false; 
      } 
     } 
    </script> 
</head> 
<body> 
<?php 
     include("header.php"); 

     if (isset($_SESSION['user'])){ 
      /* Use is logged in */ 
      echo " 
       <p class ='welcome' id='greeting'> 
        Hi, ". $_SESSION['user'] . "! Welcome to the site! 
       </p>"; 
     } else { 

      /* User is NOT logged in */ 
      echo " 
       <p class ='welcome' id='greeting'> Please Login:</p> 
       <form action='welcome.php' method='post'> 
        <center> 
         <b id = 'errorMsg'>Name:</b> 
         <input type='text' id='nameCheck' name = 'username' /> 
        </center> 
        <br /> 
        <center> 
        <b id='errorPass'>Password:</b> 
          <input type='text' id ='password' name = 'password'/> 
        </center> 
         <br /><br /> 
        <center> 
         <input type='submit' value='Log In' onClick='return validatingForm()'/> 
        </center> 
       </form>"; 
     } 


     if (isset($_SESSION['user'])){ 

      /* Query the db, once! */ 
      $user_rs = $db_con->query("SELECT * FROM `users`;"); 

      /* Assume there is a recordset and create the table */ 
      echo " 
       <center> 
        <h1> User List:</h1> 
        <table border='1'> 
         <tr> 
          <td><b>User</b></td> 
          <td><b>Login password</b></td> 
         </tr>"; 

       /* loop through recordset - new row for each user */ 
       while($record = $user_rs->fetch_object()){ 
        echo " 
         <tr> 
          <td>" . $record->id . "</td> 
          <td>" . $record->username . "</td> 
         </tr>"; 
       } 
       /* Close the table */ 
       echo " 
        </table> 
       </center>"; 
     } 

     if (isset($_SESSION['user'])) echo "<center><a href='logout.php'>Logout</a></center>"; 
?> 

      <p class = "content"> This is a page that is a scrap work in progress. I WILL use CSS to do my presentation in future '-)</p> 


     <?php 
      include("footer.php"); 
     ?> 
    </body> 
</html> 
+0

優秀的代碼清理幫助,謝謝! –

0

非常感謝你給我關於這個問題的指針和方向。

我真的很喜歡給出的答案,但不是因爲有人可能會認爲我接受RamRaider的答案的原因。

所以我想我會把它作爲一個問題的實際答案。我的問題似乎是,我沒有明確地定義PHP的花括號。我遺漏了我的代碼,結果,當我忽略用於檢查用戶會話的if語句的括號時,它似乎變得很混亂。可憐的PHP服務器:(我真的濫用它了。

如果您的if-else語句的範圍很長(跨越不止一兩個代碼行),總是找到一種方法來將範圍語法,否則你是在一個艱難的過程

這就是說,關於查詢數據庫只有一次,並調用屬性好像我查詢對象,這對我仍然有幫助在編寫mysqli語句時要注意這個註釋

非常感謝大家