Mysql插入不工作,不给出错误

时间:2015-02-26 05:17:48

标签: php mysql

我不知道为什么以下代码无法将数据插入mysql。

if (!$link = mysql_connect('server', 'user', 'password')) {
    echo '700';
    exit;
}

if (!mysql_select_db('vendors', $link)) {
    echo '701';
    exit;
}
$sql2 = "INSERT INTO transactions (TransID, payment_status, last_name, first_name, payer_email, address_name, address_state, address_zip, address_country, verify_sign, payment_gross, ipn_track_id, business, reciver_email) VALUES ('kris', 'kris', 'kris', 'kris', 'kris','kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris')";

$result2 = mysql_query($sql2, $link);

代码有什么问题? php没有错误。

3 个答案:

答案 0 :(得分:2)

请尽量不要使用 mysql_connect 而是使用 mysqli_connect PDO_MySQL read this

还可以使用 die 查找代码中是否有错误

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

否则(推荐方式) -

程序风格

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO Persons (firstname, lastname, email)
VALUES ('Happy', 'John', 'john@example.com')";

if (mysqli_query($conn, $sql)) {
    echo "New Person created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

MySQLi面向对象的风格

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO Persons (firstname, lastname, email)
VALUES ('Happy', 'John', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
    echo "New Person created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

答案 1 :(得分:0)

尝试更改此

 $result2 = mysql_query($sql2, $link);

进入这个

$result2 = mysql_query($sql2, $link)or die(mysql_error());

答案 2 :(得分:0)

您必须编写如下代码以获取代码中的错误

$result = mysql_query($sql2,$link) or die(mysql_error());

or die(mysql_error())会在查询中给出错误