PHP loop reading data from database problem
世界只因有你
世界只因有你 2017-06-29 10:08:05
0
4
975

As shown in the figure below, why can’t the program read database data?

世界只因有你
世界只因有你

reply all(4)
ringa_lee

There are several problems with your code:

  1. After connecting to the database, the data table selection operation mysql_select_db() must be performed before the query operation mysql_query(), otherwise the query will not know which data table to search in, and the statement will be run directly on the entire database. However, the way you write the SQL statement does not conform to the format of the query on the database, so the execution will fail, that is, there is no data.

  2. mysql_query()For the SELECT statement, what is returned is a resource reference or FALSE value. To traverse all the rows in it, you need to use mysql_fetch_assoc() to traverse the resource and return the row data.

  3. It is recommended not to write the question mark of <?php and php separately, because this is the logo that the engine recognizes when PHP starts. Separating it may cause engine recognition errors

So,
First point, swap the positions of the mysql_select_db() and mysql_query() lines.
Second point, change foreach($result as $row) to while($row = mysql_fetch_assoc($result))

洪涛

It should be mysql_select_db('news', $conn);

代言

mysql_query() returns a resource identifier only for SELECT, SHOW, EXPLAIN or DESCRIBE statements, or FALSE if the query was executed incorrectly.
For other types of SQL statements, mysql_query() returns TRUE when executed successfully and FALSE when an error occurs.
A non-FALSE return value means the query is valid and can be executed by the server. This does not say anything about the number of rows affected or returned. It's possible that a query executed successfully but did not affect or return any rows.

mysql_fetch_assoc() function fetches a row from the result set as an associative array.
Returns an associative array generated based on the rows obtained from the result set, or false if there are no more rows.

while($row = mysql_fetch_assoc($result)) {
    echo $row["id"];
}
过去多啦不再A梦

Remove the judgment!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!