dongtun1209 2017-03-28 23:48
浏览 84

我如何知道我的mysql数据库中是否存有记录? [重复]

This question already has an answer here:

I have 2 php files. One of them creates a table and inserts data into MySql using php. The other php selects the table in the database and outputs the data from the table. The table is outputting 0 results as in there aren't any record being stored into the table.

Need help, Thanks!

//select from table
                  
                $sql = "SELECT * FROM post02";
                $result = mysql_query($link, $sql);
                echo 'selecting table works';
                
                //output from table
            
                if (mysql_num_rows($result) > 0) {                   
                    while($row = mysql_fetch_assoc($result)) {
                    
                    
                    echo "id: " . $row["id"]. " - Name: " . $row["title"]. " " . $row["content"]. ""  . $row["date"]."<br>";
                        
                        
                    }
                } else {
                    echo "0 results";
                }   
            
// create a table


    $sql="create table post02(id INT(6) unsigned auto_increment primary key , title varchar(30) not null, content varchar(255) not null, date TIMESTAMP)";



    $result = mysql_query($sql);

    if (! $result ) {

        die ( 'Cant create table : '  .  mysql_error ());

    }

     echo  'Created a table successfuly' ;


//insert to table
    echo'insert table - initializing';

    if($_POST['submit']){
    $title = $_POST['title'];
    $content = $_POST['content'];
    $date = date('l jS \of F Y h:i:s A');
    }
    
    $sql = "INSERT INTO post02(title, content, date) VALUES ($title, $content,$date))";

    if($title =="" || $content=="" ){
        echo "please compelete your post!";
        return;
         
    }
    echo'insert table completed';

    
        mysql_query($db ,$sql);
    header("location: viewblog.php");

</div>
  • 写回答

2条回答 默认 最新

  • dougong7850 2017-03-28 23:58
    关注

    mysql_query accepts query as first parameter & connection string as second parameter. Try interchanging in all the locations in your code. Or you can remove the link parameter

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大