mysql 中每一行的页面 [英] page for each row in mysql
问题描述
我是 php 的新手,为了为所有文章制作一个独特的页面,我已经努力了几个小时.无论我做什么都行不通.
我知道我应该将 mysql 转换为 mysqli,但首先我只是想让它工作.
当我转到 url/video.php?id=3 时,它只显示一个空白页面,它不会从文章表中打印出名称"
有什么建议吗?
video.php
我从 index.php 链接到以下内容
<a href="/video.php?id=<? echo $row[id]; ?>
哪个有效.
我看到的另一件事,但我假设如果你看到了:
- 您是否已成功连接到 MySQL 服务器?
- 您是否选择了要使用的数据库?
- 您知道 PHP 中的 mysql_result 函数吗?和
- 您知道 MySQL 中的 LIMIT 吗?
试试:
MySQL:
/*** 连接并定义您的连接变量*/$connection = mysql_connect("localhost","root","");/*** 选择您的数据库*/mysql_select_db("mydatabase",$connection);/*** 检查 $_GET 中是否存在id"*/if( array_key_exists( "id" , $_GET ) ){$id = mysql_real_escape_string( $_GET [ "id" ],$connection );/*** 记住使用 LIMIT*/$source = mysql_query("SELECT `name` FROM `article` WHERE `id` = '$id' LIMIT 1",$connection);/*** 如果您只需要打印名称*/if(mysql_num_rows($source)){//只打印名字回声 mysql_result( $source );}/*** 但如果你需要检索数组*/$row = mysql_fetch_assoc( $source );回声 $row["name"];}
MySQLi:
/*** 连接并定义您的连接变量*/$connection = mysqli_connect("localhost","root","");/*** 检查是否已连接[ http://us2.php.net/manual/en/mysqli.connect-errno.php ]*/如果(mysqli_connect_errno() == 0){/*** 选择您的数据库*/mysqli_select_db( $connection , "mydatabase" );/*** 检查 $_GET 中是否存在id"*/if( array_key_exists( "id" , $_GET ) ){$id = mysqli_real_escape_string($connection,$_GET ["id"]);/*** 记住使用 LIMIT*/$source = mysqli_query($connection,"SELECT `name` FROM `article` WHERE `id` = '$id' LIMIT 1");/*** 如果您只需要打印名称*/if(mysqli_num_rows($source)){//只打印名字$row = mysqli_fetch_array( $source );回声 $row[0];}/*** 但如果你需要检索数组*/$row = mysqli_fetch_assoc( $source );回声 $row["name"];}}
并且,阅读 MySQL 上的 LIMIT>
祝你好运!
i'm new to php and i've been struggling for several hours with making a unique page for all the articles. whatever i do it wont work.
i know i should convert the mysql to mysqli, but to begin with i just wanna make this work.
when i go to url/video.php?id=3 it just show a blank page it do not print "name" from the article table out
any suggestions?
video.php
<?php
include "connect.php";
$id = mysql_real_escape_string($_GET['id']);
$query = "SELECT `name` FROM `article` WHERE `id` = '.$id.'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
// Echo page content
echo $row['name'];
?>
im linking from index.php with following
<a href="/video.php?id=<? echo $row[id]; ?>
which works.
Another thing that I see, but I assume that if you did:
- Have you managed to connect to the MySQL server?
- Did you select the database to use?
- you know the function mysql_result in PHP? And
- you know LIMIT in MySQL?
Try:
MySQL:
/**
* Connect and define your connection var
*/
$connection = mysql_connect("localhost","root","");
/**
* Select your database
*/
mysql_select_db("mydatabase",$connection);
/**
* Check if exists "id" in $_GET
*/
if( array_key_exists( "id" , $_GET ) )
{
$id = mysql_real_escape_string( $_GET [ "id" ],$connection );
/**
* Remember use LIMIT
*/
$source = mysql_query("SELECT `name` FROM `article` WHERE `id` = '$id' LIMIT 1",$connection);
/**
* if you need print only name
*/
if( mysql_num_rows( $source ) )
{
// print only name
echo mysql_result( $source );
}
/**
* but if you need retrive array
*/
$row = mysql_fetch_assoc( $source );
echo $row["name"];
}
MySQLi:
/**
* Connect and define your connection var
*/
$connection = mysqli_connect("localhost","root","");
/**
* Check if is connected[ http://us2.php.net/manual/en/mysqli.connect-errno.php ]
*/
if( mysqli_connect_errno() == 0 )
{
/**
* Select your database
*/
mysqli_select_db( $connection , "mydatabase" );
/**
* Check if exists "id" in $_GET
*/
if( array_key_exists( "id" , $_GET ) )
{
$id = mysqli_real_escape_string($connection,$_GET [ "id" ]);
/**
* Remember use LIMIT
*/
$source = mysqli_query($connection,"SELECT `name` FROM `article` WHERE `id` = '$id' LIMIT 1");
/**
* if you need print only name
*/
if( mysqli_num_rows( $source ) )
{
// print only name
$row = mysqli_fetch_array( $source );
echo $row[0];
}
/**
* but if you need retrive array
*/
$row = mysqli_fetch_assoc( $source );
echo $row["name"];
}
}
And, read bout the LIMIT on MySQL
Good luck!
这篇关于mysql 中每一行的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!