douzhao9608 2014-04-08 05:14
浏览 65
已采纳

查询mysql时出错[关闭]

Here is my query to create a table using mysql and php.

<?php
$con=mysql_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysql_connect())
  {
  echo "Failed to connect to MySQL: " . mysql_error();
  }

// Create table
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT,)";

// Execute query
if (mysql_query($con,$sql))
  {
  echo "Table persons created successfully";
  }
else
  {
  echo "Error creating table: " . mysql_error($con);
  }
?>

I am getting the Error as Check your Syntax

How can I resolve this error ?

  • 写回答

7条回答 默认 最新

  • dongsuiying7773 2014-04-08 05:16
    关注

    You are using , after INT

      $sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT,)";
    

    use as

      $sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT)";
    

    And if you remove your error is solved.

    I recommend you to use mysqli and not to use mysql. Because this extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used.

    Source : http://www.php.net/manual/en/intro.mysql.php

    Here is the mysqli code for you.

    <?php
    $con=mysqli_connect("example.com","peter","abc123","my_db");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    
    // Create table
    $sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT)";
    
    // Execute query
    if (mysqli_query($con,$sql))
      {
      echo "Table persons created successfully";
      }
    else
      {
      echo "Error creating table: " . mysqli_error($con);
      }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c