這種一般是sql語句的錯誤。你將php變量改為值之后將sql語句放到phpmyadmin或其他mysql工具里試一下是否能查到
創(chuàng)新互聯(lián)建站于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都做網(wǎng)站、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元薊州做網(wǎng)站,已為上家服務(wù),為薊州各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220
include 'mysql.php';
$server="localhost";
$user="用戶名";
$psw="密碼";
$database="數(shù)據(jù)庫";
$db = new Mysql();
$db-connect($server, $user, $psw, $database);
unset($server, $user, $psw, $database);
mysql.php是個數(shù)據(jù)庫連接類,內(nèi)容如下:
?php
class Mysql{
var $qryNum = 0;
var $qryInfo = '';
var $qryTime = 0.0;
var $debug = false;
var $connId;
var $tblPrefix = '';
var $tblFields = array();
var $lastQuery ;
var $openedQueries;//沒有釋放的查詢
var $transaction;
function Mysql() {
$this-debug = 0;
defined('UNBUFFERED') or define('UNBUFFERED',1);
}
function connect($host, $user, $pass, $name) {
$this-connId = @mysql_connect($host, $user, $pass) OR $this-halt("Cann't connect to server");
$this-selectDb( $name );
return;
}
function selectDb($name) {
return @mysql_select_db($name, $this-connId) OR $this-halt();
}
function insert($table, $data) {
$fields = $this-getFields($table);
$values = $columns = array();
foreach ($fields as $field) {
$column = $field['Field'];
if ( $field['Extra'] == 'auto_increment' ) {
$values[] = 'null';
} else if ( array_key_exists($column, $data) ) {
$values[] = "'" . $data[$column] . "'";
} else {
$values[] = "'" . $field['Default'] . "'";
}
$columns[] = $column;
}
$sql = "INSERT INTO $table(" . implode(',', $columns) .') VALUES('. implode(',', $values) .')';
return $this-query($sql, UNBUFFERED);
}
function update($table, $data, $conds) {
$updates = array();
$fields = $this-getFields($table);
foreach ($fields as $field) {
$column = $field['Field'];
if (isset($data[$column])) {
$updates[] = "$column='" . $data[$column] . "'";
}
}
$sql = "UPDATE $table SET ". implode(',', $updates) ." WHERE $conds";
return $this-query($sql, UNBUFFERED);
}
function delete($table, $conds) {
return $this-query("DELETE FROM $table WHERE $conds", UNBUFFERED);
}
function select($table, $columns = '*', $conds = 1) {
return $this-query("SELECT $columns FROM $table WHERE $conds");
}
function getFields($table) {
if (array_key_exists($table, $this-tblFields)) {
return $this-tblFields[$table];
}
$this-query("DESC $table");
return $this-tblFields[$table] = $this-fetchAll();
}
function fetchAll($qryId = 0) {
$rtn = array();
$qryId || $qryId = $this-lastQuery;
while ($row = $this-fetchAssoc($qryId)) {
$rtn[] = $row;
}
return $rtn;
}
function fetchOne($sql) {
$rst = $this-query($sql);
return mysql_fetch_array($rst, MYSQL_ASSOC);
}
function fetchRow($qryId = 0) {
$qryId || $qryId = $this-lastQuery;
return mysql_fetch_row($qryId);
}
function fetchAssoc($qryId = 0) {
$qryId || $qryId = $this-lastQuery;
return mysql_fetch_array($qryId, MYSQL_ASSOC);
}
function fetchArray($qryId = 0) {
$qryId || $qryId = $this-lastQuery;
return mysql_fetch_array($qryId, MYSQL_ASSOC);
}
function fetchObject($qryId = 0) {
$qryId || $qryId = $this-lastQuery;
return @mysql_fetch_object($qryId);
}
function result($rst, $row, $column = 0) {
return @mysql_result($rst, $row, $column);
}
function query($sql, $unbuffered = 0) {
if ( $this-debug ) {
$mtime = explode(' ', microtime());
$stime = $mtime[1] + $mtime[0];
}
$this-lastQuery = @mysql_query($sql, $this-connId);
//$unbuffered ? @mysql_unbuffered_query($sql, $this-connId) :
$this-lastQuery || $this-halt($sql);
if ($this-debug){
$mtime = explode(' ', microtime());
$etime = $mtime[1] + $mtime[0] - $stime;
$this-qryInfo .= sprintf("lib%1.5f/b %shr size=1 noshadow\r\n", $etime, $sql);
$this-qryTime += $etime;
}
$this-qryNum++;
if (strpos($sql, 'SELECT') !== false $this-lastQuery) {
$this-openedQueries[(int) $this-lastQuery] = $this-lastQuery;
}
return $this-lastQuery;
}
function dataSeek($rst, $row) {
// $qryId || $qryId = $this-lastQuery;
mysql_data_seek($rst, $row);
}
function debugOn() {$this-debug = true;}
function debugOff() {$this-debug = false;}
function getQueryNum() {return $this-qryNum;}
function affectedRows() {
return @mysql_affected_rows();
}
function numRows($qryId = 0) {
$qryId || $qryId = $this-lastQuery;
return @mysql_num_rows($qryId);
}
function insertId() {
return @mysql_insert_id();
}
function freeResult($qryId) {
$qryId || $qryId = $this-lastQuery;
if (isset($this-openedQueries[(int) $qryId])) {
unset($this-openedQueries[(int) $qryId]);
return @mysql_free_result($qryId);
}
return false;
}
function close() {
if (!$this-connId) {
return false;
}
if ($this-transaction) {
$this-transaction('commit');
}
if ($this-openedQueries){
foreach ($this-openedQueries as $key = $qryId){
$this-freeresult($qryId);
}
}
mysql_close($this-connId);
}
function halt() {
$str = sprintf("liMYSQL錯誤代碼:%s/li\r\n", mysql_errno());
$str.= sprintf("liMYSQL錯誤原因:%s/li\r\n", mysql_error());
if (func_num_args()) {
$sql = func_get_args();
$str.= sprintf("liSQL ? ?:%s/li\r\n", $sql[0]);
}
die($str);
}
function debug() {
$str = sprintf("li共執(zhí)行(%s)次查詢/li\r\n", $this-qryNum);
$str.= $this-qryInfo;
$str.= sprintf("li總用時:b[%1.5f]/b/li\r\n", $this-qryTime);
return $str;
}
}
自己可以研究一下,這個mysql類很實用的,我一直在用
插入的時候可以這樣寫:
$insert["name"] = "aaaaaa";//name就是你的數(shù)據(jù)庫中的字段名
$insert["age"] = "20";
$db-insert("user",$insert);
?php
$conn = mysql_connect('localhost','用戶名','密碼') or die('連接數(shù)據(jù)庫失敗,請檢查您的數(shù)據(jù)庫配置');//登錄地址localhost,這個不行,試試127.0.0.1,有的時候沒有加映射localhost是連接不上的
mysql_select_db('數(shù)據(jù)庫名');//選中數(shù)據(jù)庫
?
只能告訴你步驟,具體的還得靠你學(xué)習(xí)了。
執(zhí)行PHP代碼之前要安裝相應(yīng)的環(huán)境,就像劃船需要水一樣?,F(xiàn)在一般都用wamp5
安裝好wamp5后,到開發(fā)目錄,新建一個index.php 文件,在這個文件里使用mysql_connect()函數(shù)連接數(shù)據(jù)庫,然后使用mysql_select_db選擇數(shù)據(jù)庫。這樣就可以操作這個數(shù)據(jù)庫了。
然后就是使用php代碼來增刪改查數(shù)據(jù)庫了。
你估計連菜鳥都算不上。。。你真要想學(xué)PHP的話,建議先看一下視頻,一目了然,通過文字說了也不好懂的。網(wǎng)上一搜一大堆,具體的不打了,百度可能把我的回答刪了。
其實不用一開始就關(guān)心conn.php,建議先寫一個最簡單的php腳本來驗證MYSQL數(shù)據(jù)庫的連接、查詢、關(guān)閉功能是否正常,測試腳本示例如下:
?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli-connect_error) {
die('Connect Error (' . $mysqli-connect_errno . ') '
. $mysqli-connect_error);
}
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . $mysqli-host_info . "\n";
$mysqli-close();
?
如果測試通過了,我們再來看為什么需要conn.php文件——不可能把數(shù)據(jù)的IP地址、用戶名、密碼等參數(shù)寫入每一個PHP文件,辦法就是做一個簡單的conn.php來存放這些內(nèi)容,所有需要連接MYSQL的程序都引用conn.php進(jìn)行工作,所以conn.php中只要有如下一行即可:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$link是鏈接mysql返回的資源,報錯提示null given 說明你還沒有連接mysql,就查詢了