网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 服务器与数据库 > MySQL
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

Reply
 
Thread Tools Display Modes
  #1   IP: 49.87.116.189
Old 2014-11-22, 08:44 PM
Adams Adams is offline
初级会员
 
Join Date: 2007-04-05
Posts: 1
Adams 现在声名狼藉
Default 73-sql语言的设计与编写

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();
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 03:39 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.