如何使用php从mysql数据库输出数据

时间:2017-03-28 17:32:04

标签: php mysql

我正在使用mysql创建一个使用php输入和输出数据(博客帖子)的博客。我创建了一个表,为数据插入行,例如:&id;标题,内容和日期" addentry.php"。 我在我的" viewblog.php"上使用php输出数据时遇到问题。它表示" //从表"(第115行)输出,那就是我要输出数据的地方。我尽力找到解决方案。

提前致谢。



<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- image.html
     A trivial document
     -->
<html xmlns = "http://www.w3.org/1999/xhtml">


    <head><title> My Blog </title></head>

    <style type ="text/css">

        body{ 

        position: fixed; 
        overflow:overlay;
        width: 100%;
        top: -20px;
        left: -20px;
        right: -40px;
        bottom: -40px;       


        height: auto;
        background-image:url(image.jpg);
        background-size: cover;



        }
        .container{

            background-color: #ecdad6;          
            padding: 30px;
            width:920px;        
            margin-left: 25%;
            padding-bottom:1000px;
            padding-left:0px;
            border: 2px solid black;
            }

        .links{
            position: absolute;

            padding-right: 135px;
            padding-bottom: 800px;              
            margin-left: 680px;
            margin-right: 100px;
            font-size: 20px;
            word-wrap: break-word;
            top:-3px;


        }


        .blog{
            position: absolute;
            width:678px;
            padding-bottom: 920px;              
            margin-left: 10px;
            font-size: 20px;
            text-align: left;            
            word-wrap: break-word;

            }
        ul li { margin-top: -10px; }

        }
        
        iframe{
        border:2px solid black;
        width:4000px;
        display:block;
        *}

    } 
    </style>
    <body>

        <!--Logo & hyperlinked -->
        <p align = "center"><a href="viewblog.php"><img src = "Logo.jpg"      alt="My logo" width="10%" height="10%"/></a></p>
        <br/>
        <hr width="50%">

        <div class="container">                         
            <div class="blog"> 
                <?php
                        
				$host   =   "dbprojects.eecs.qmul.ac.uk"  ;
				$user   =   "hm315"  ;
				$pass   =   "cXtXuyf2pnF4H"  ;
				$db   =   "hm315"  ;
				 
				$link  =  mysql_connect ( $host ,  $user ,  $pass );
				if (! $link ) {
				    die( 'Could not connect: '  .  mysql_error ());
				}
				echo  'Connected successfully' ;

				$db_selected  =  mysql_select_db ( $db ,  $link );
				if (! $db_selected ) {
				    die ( 'Can\'t use $db : '  .  mysql_error ());
				}

				echo  'Connected successfully' ;

				//select from table
			        $sql = "SELECT * FROM post02";
				$row=mysql_fetch_array($sql);
				
				echo 'selecting table works';
				
				//output from table
				
				

                            	
				//connection + database, record created and close connection.

		              if ($link->query($sql) === TRUE) {
				    echo "New record created successfully";
			      } else {
				    echo "Error: " . $sql . "<br>" . $link->error;
			      }
				
                       		mysql_close ( $link );
                ?>
                
                
                <form action='index.php' method='get'></form>
                <div class="links">
                    <a href="viewblog.php"> <ul><li>home</li></ul></a> 
                    <a href="login.html"> <ul><li>logIn</li></ul></a> 
                    <a href="entry.html"> <ul><li>add_entry</li></ul></a>                 
                </div>                  
            </div> 
              </div>

    </body>
</html>
&#13;
<?php

  $host   =   "xxx.ac.uk"  ;
	$user   =   "xxx"  ;
	$pass   =   "xxx"  ;
	$db   =   "xxx"  ;
	 
	$link  =  mysql_connect ( $host ,  $user ,  $pass, $db );
	if (! $link ) {
	    die( 'Could not connect: '  .  mysql_error ());
	}
	echo  'Connected successfully' ;
	

	$db_selected  =  mysql_select_db ( $db ,  $link );
	if (! $db_selected ) {
	    die ( 'Can\'t use $db : '  .  mysql_error ());
	}
/*
	// 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)";



	$connection = mysql_query($sql);

	if (! $connection ) {

	    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 post2('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");

	//record created is successful
	if ($link->query($sql) === TRUE) {
	    echo "New record created successfully";
	} else {
	    echo "Error: " . $sql . "<br>" . $link->error;
	}
	 
 
mysql_close ( $link );
?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

问题不是那么清楚,我根据问题的标题写了一个示例代码。哦,不要忘记改变你的信誉。

<table>
<tr>
  <th>Column 1</th>
  <th>Column 2</th>
  <th>Column 3</th>
</tr>

<?php
$r = $link->query("SELECT * FROM table");
while($rf = $r->fetch_assoc())
{
  echo "<tr> 
          <td>{$rf['column_1']}</td>
          <td>{$rf['column_2']}</td>
          <td>{$rf['column_2']}</td>
        </tr>";
}
?>

</table>