resource id #4 Why am I getting this?

后端 未结 5 815
感动是毒
感动是毒 2020-12-11 20:51

I have a pretty basic forum template I am working on for testing purposes

When I create a topic, and press submit, the proccess updates the database but doesn\'t out

相关标签:
5条回答
  • 2020-12-11 20:56
    <?php
    
    $host="server";
    $username="usernamehere"; 
    $password=""; 
    $db_name="forum";
    $tbl_name="question"; 
    mysql_connect("$host", "$username", "")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    $sql="SELECT * FROM $tbl_name ORDER BY id DESC";
    // OREDER BY id DESC is order result by descending
    
    $result=mysql_query($sql);
    echo $result; //remove it
    ?>
    <html>
    <body>
    <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
    <td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
    <td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
    <td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
    <td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
    </tr>
    
    <?php
    
    // Start looping table row
    while($rows=mysql_fetch_array($result)){
    
    echo"<tr>
    <td bgcolor=#FFFFFF>$rows['id']</td>
    <td bgcolor=#FFFFFF><a href='view_topic.php?id=$rows['id']'>$rows['topic']</a><BR></td>
    <td align=center bgcolor=#FFFFFF>$rows['view']</td>
    <td align=center bgcolor=#FFFFFF>$rows['reply']</td>
    <td align=center bgcolor=#FFFFFF>$rows['datetime'];</td>
    </tr>";
    
    
    // Exit looping and close connection 
    }
    mysql_close();
    ?>
    
    <tr>
    <td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php"><strong>Create New Topic</strong> </a></td>
    </tr>
    </table>
    </body>
    </html>
    

    just check above code i think your problem should be done

    0 讨论(0)
  • 2020-12-11 21:02

    If you check the link - http://php.net/manual/en/function.mysql-query.php

    For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error

    Hence you are seeing the Resource#4

    . What is it you want to achieve?

    0 讨论(0)
  • 2020-12-11 21:07

    Problem is in your code:

    $result=mysql_query($sql);
    echo $result;
    

    $result is resource type, since mysql_query($sql) returns resource Stop echoing $result.

    0 讨论(0)
  • 2020-12-11 21:15

    You are getting resource id #4 because $result is an resource,you must extract the values contained in it by this way,

    $result=mysql_query($sql);
    $values = mysql_fetch_array($result);
    var_dump($values);
    

    More about resource variable

    Update 2(From OP comments)

    You are printing values using field name,In that case you will have to change to

    while($rows=mysql_fetch_array($result,MYSQL_ASSOC))
    

    Or you can directly use mysql_fetch_assoc(),which in your case will be

    while($rows=mysql_fetch_assoc($result)){
          echo $rows['id'];
    }
    
    0 讨论(0)
  • 2020-12-11 21:15

    You don't have to use mysql_fetch_array(). If you want, try something like this:

    $sql="SELECT * FROM $tbl_name ORDER BY id DESC"; //that's your query
    $result=mysql_query($sql);
    $rows=mysql_num_rows($result);
    $iteration=0;
    echo "<table>";
    
    while($iteration < $rows){
        $cell_in_your_html_table = mysql_result($result , $iteration , 'column_name_from_database');
        echo "<tr><td>".$cell_in_your_html_table."</td></tr>";
          $iteration++;
    }
    echo "</table>"
    
    0 讨论(0)
提交回复
热议问题