<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
if (!mysql_select_db('database'))
die("Can't select database");
// choose id 31 from table users
echo $id; //31
echo $name; //id31's name
echo $surname //id31's surname
echo $blablabla //id31's blablabla
mysql_close($link);
?>
Run Code Online (Sandbox Code Playgroud)
试一试:
$id = mysql_real_escape_string(31);
$result = mysql_query('SELECT * FROM `users` where `id` = ' . $id . ' LIMIT 1');
$row = mysql_fetch_assoc($result);
echo $row['id']; // 31
echo $row['name']; // name
// etc..
// If you wanted to output ALL the fields you could loop through
foreach($row as $field_name => $value) {
echo $field_name . ': ' . $value . '<br />';
}
// The output would look something like
// id: 31
// name: John Smith
// ...
Run Code Online (Sandbox Code Playgroud)
使用的功能:
mysql_real_escape_string() - 转义字符串中的特殊字符以用于SQL语句mysql_query() - 发送MySQL查询mysql_fetch_assoc() - 将结果行作为关联数组获取| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |