在php和mysql中正确的编码技术

时间:2016-08-09 06:34:38

标签: php mysql

我是新手phpmysql.heres我的代码,我得到错误

  

1)未定义的变量:C

中的sql      

2)警告:mysqli_query():C:中的空查询

     

3)警告:mysqli_fetch_array()期望参数1为   mysqli_result,C中给出的布尔值

    <?php
    $servername="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password="root"; // Mysql password 
    $db_name="mydb"; // Database name 
    $tbl_name="forum_question"; // Table name 

    // Create connection
    $conn = new mysqli($servername, $username, $password,$db_name);

      // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    } 
    else
    {
    echo "Connected successfully";
     }
    // get value of id that sent from address bar 
    $id=$_GET['name'];
    $sqli="SELECT * FROM forum_question WHERE id='$id'";
    $result=mysqli_query($conn,$sql);
    $rows=mysqli_fetch_array($result);
    ?>
    <?php

    $tbl_name2="forum_answer"; // Switch to table "forum_answer"
    $sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";
    $result2=mysqli_query($conn,$sql2);
    while($rows=mysqli_fetch_array($result2)){
    ?>
    <?php
    }

    $sql3="SELECT view FROM $tbl_name WHERE id='$id'";
    $result3=mysqli_query($conn,$sql3);
    $rows=mysqli_fetch_array($result3);
    $view=$rows['view'];

    // if have no counter value set counter = 1
    if(empty($view)){
   $view=1;
   $sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";
   $result4=mysqli_query($conn,$sql4);
   }

   // count more value
   $addview=$view+1;
   $sql5="update $tbl_name set view='$addview' WHERE id='$id'";
   $result5=mysqli_query($conn,$sql5);
   mysql_close($conn);
   ?>

1 个答案:

答案 0 :(得分:2)

$id=$_GET['name'];
$sqli="SELECT * FROM forum_question WHERE id='$id'";
$result=mysqli_query($conn,$sql);
$rows=mysqli_fetch_array($result);
  1. 您正在定义$sqli,但随后尝试在$sql中使用mysqli_query()
  2. 因为您在mysqli_query()中使用了错误的变量,该函数会返回false而不是mysqli_result
  3. 您在mysqli_fetch_array()上使用的是false,而不是mysqli_result,因此会出现下一个错误。