我没有得到这个代码中的错误,“注意:未定义的变量:第27行的C:\ xampp \ htdocs \ w3s \ createtablepdo.php中的sql”

问题描述:

I don't understand what is wrong with the code below. Everything seems fine but I'm still getting a notice:

Notice: Undefined variable: sql in C:\xampp\htdocs\w3s\createtablepdo.php on line 27 " and error could not find driver.

 <?php
    $servername="localhost";
    $username="root";
    $password="";
    $dbname="mysql";
    try
    {
        //creating connection
        $conn = new PDO("mysqli:host=$servername;dbname=$dbname",$username,$password);

        //checking exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         // sql to create table
            $sql = "CREATE TABLE MyGuests (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
            firstname VARCHAR(30) NOT NULL,
            lastname VARCHAR(30) NOT NULL,
            email VARCHAR(50),
            reg_date TIMESTAMP
            )";
        //use exec cos no result are returned
        $conn->exec($sql);
        echo "table MyGuests created succesfully";
    }
    catch(PDOException $e)
    {
        echo $sql . "<br>" . $e->getMessage();
    }

    $conn = null;
?>

我不明白下面的代码有什么问题。 一切似乎都很好,但我还是得到了通知: p>

注意:未定义的变量:s \ in \ C:\ xampp \ htdocs \ w3s \ createtablepdo.php on 第27行“并且错误可能找不到驱动程序。 p> blockquote>

 &lt;?php 
 $ servername =”localhost“; 
 $ username =  “root”; 
 $ password =“”; 
 $ dbname =“mysql”; 
尝试
 {
 //创建连接
 $ conn = new PDO(“mysqli:host = $ servername; dbname  = $ dbname“,$ username,$ password); 
 
 //检查异常
 $ conn-&gt; setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION); 
 // sql创建表
  $ sql =“CREATE TABLE MyGuests(
 id INT(6)UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 firstname VARCHAR(30)NOT NULL,
 lastname VARCHAR(30)NOT NULL,
 email VARCHAR(50),
  reg_date TIMESTAMP 
)“; 
 //使用exec cos没有返回结果
 $ conn-&gt; exec($ sql); 
 echo”table MyGue  sts成功创建“; 
} 
 catch(PDOException $ e)
 {
 echo $ sql。  “&LT峰; br&gt;” 中 。  $ e-&gt; getMessage(); 
} 
 
 $ conn = null; 
?&gt; 
  code>  pre> 
  div>

I guess you are following this tutorial for creating new table using PDO. If you see the code on the link carefully, you will need to change the line

$conn = new PDO("mysqli:host=$servername;dbname=$dbname",$username,$password);

to following

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

See this link for PDO Connections and Connection management

Update / changed answer: I misread this error; apparently it's been some time since I've worked with PHP. Apparently this is not an issue of scope (I thought $sql was undefined outside of try {...}). Instead, the problem must be that $sql is never defined because you never reach the line that defines it.

Either way, the solution should be the same:

try
{
    // Assume something goes wrong here

    // You define $sql here, so this is from where it is valid:
    $sql = "CREATE TABLE ...";

} 
catch(PDOException $e)
{
    // This is where your error comes from, but since you never
    // reached the line where $sql was defined before jumping
    // into the Catch section, $sql is still undefined here:
    echo $sql . "<br>" . $e->getMessage(); 
}

Solution: Define $sql outside the whole try-catch clause:

$sql = ""; // empty is fine. 
try
{
    // Assume something goes wrong here
    $sql = "CREATE TABLE ...";
} 
catch(PDOException $e)
{
    // Now $sql will just be empty if errors occur before 
    // your query code is defined:
    echo $sql . "<br>" . $e->getMessage(); 
}

Once you fix this, you should be able to see what the real error is - the one that is hidden due to the undefined $sql messing up your error message.

The issue is, its mysql PDO not mysqli PDO. Try :

$conn = new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);