Log in

View Full Version : 73-sql语言的设计与编写


Adams
2014-11-22, 08:44 PM
mysql
mysqli mysql improvment
pdo

mysql


conn.inc.php
<?php
数据库连接
step 1 link database
$link=mysql_connect("localhost","root","abc");
if (!$link){
echo "连接失败";
exit;
}

mysql>use cheapglasses123;选择默认的数据库 行命令


step 2 choose database
mysql_select_db("cheapglasses123"); 选择默认的数据库


<?php

require "conn.inc.php";

//执行任何SQL(前提必须连接的用户有操作SQL的权限)
mysql>create table shops

$sql="CREATE TABLE shops(id int not null auto_increment,
name varchare(50) not null default '',
price double not null default '0.00', num int not null
default '0',desn text, primary key('id'),
key name(name,price))"; //在语句后面不用加封号

$result=mysql_query($sql); 执行上边的$sql语句

//下边if是查出错的代码
if(!$result) {
echo "ERROR ".mysql_errno()." :".mysql_error();
exit;
}

var_dump($result); 输出$result的返回结果

mysql_close();

================
<?php
require "conn.inc.php";
$sql="CREATE TABLE shops(id int not null auto_increment, name varchar(50) not null default '', price double not null default '0.00', num int not null default '0',desn text, primary key(id), key name(name,price))";

$result=mysql_query($sql);
if(!$result) {
echo "ERROR ".mysql_errno()." :".mysql_error();
exit;
}
var_dump($result);
mysql_close();
==================

<?php
require "conn.inc.php";
$sql="CREATE TABLE if not exists shops(id int not null auto_increment, name varchar(50) not null default '', price double not null default '0.00', num int not null default '0',desn text, primary key(id), key name(name,price))";

$result=mysql_query($sql);

//解决错误,使用这个if广开语句
if(!$result) {
echo "ERROR ".mysql_errno()." :".mysql_error();
exit;
}
var_dump($result);
mysql_close();

第一步,链接数据库
第二步,选择数据库
第三步,执行SQL语句
第四步,解决错误
第五步,关闭链接

========================================

<?php
require "conn.inc.php";
$sql="insert into shops(name,price,num,desn) values('ibm','12000','10','very good')";
//如果是select语句成功返回结果集(资源),失败返回false,所有执行非色select语句,成功返回true,失败返回false

$result=mysql_query($sql);
if(!$result) {
echo "ERROR ".mysql_errno()." :".mysql_error();
exit;
}
var_dump($result);
mysql_close();

====================
$sql="insert into shops(name,price,num,desn) values('','','','','');
如提交的是变量:将变量放在{}中
$sql="insert into shops(name,price,num,desn) values('{}','{}','{}','{}','{}');

http://localhost/yahoo/demo.php?name=book&price=79&num=10&descn=very good

同理用表单组合语句
-------

<?php
require "conn.inc.php";
$sql="insert into shops(name,price,num,desn) values('{$_GET["name"]}','{$_GET["price"]}','{$_GET["num"]}','{$_GET["desn"]}')";

echo $sql."<br>"; //看sql语句有没有成功
$result=mysql_query($sql);
if(!$result) {
echo "ERROR ".mysql_errno()." :".mysql_error();
exit;
}

echo "最后自动增长的ID: ".mysql_insert_id()."<br>";
echo "影响的行数:".mysql_affected_rows()."<br>";
var_dump($result);
mysql_close();


=================


<?php
require "conn.inc.php";
$sql="insert into shops(name,price,num,desn) values('{$_GET["name"]}','{$_GET["price"]}','{$_GET["num"]}','{$_GET["desn"]}')";
echo $sql."<br>";
$result=mysql_query($sql);
if(!$result) {
echo "ERROR ".mysql_errno()." :".mysql_error();
exit;
}
echo "最后自动增长的ID: ".mysql_insert_id()."<br>";
echo "影响的行数:".mysql_affected_rows()."<br>";
var_dump($result);
mysql_close();

=====================

<?php
require "conn.inc.php";
$sql="update shops set name='{$_GET["name"]}' where id>5";
echo $sql."<br>";
$result=mysql_query($sql);

//判断sql语句有没有错误
if(!$result) {
echo "ERROR ".mysql_errno()." :".mysql_error();
exit;
}

//判断有没有影响行数
if(mysql_affected_rows()>0){
echo "执行成功!<br>";
}else{
echo "记录没有改变!<br>";
}

echo "最后自动增长的ID: ".mysql_insert_id()."<br>"; //由于update不增加行, 这就为0
echo "影响的行数:".mysql_affected_rows()."<br>";

mysql_close();

==============================

<?php
require "conn.inc.php";
$sql="desc shops";
echo $sql."<br>";
$result=mysql_query($sql);

//判断sql语句有没有错误
if(!$result) {
echo "ERROR ".mysql_errno()." :".mysql_error();
exit;
}

//判断有没有影响行数
if(mysql_affected_rows()>0){
echo "执行成功!<br>";
}else{
echo "记录没有改变!<br>";
}

echo "最后自动增长的ID: ".mysql_insert_id()."<br>"; //由于update不增加行, 这就为0
echo "影响的行数:".mysql_affected_rows()."<br>";

mysql_close();