MySQLi - 麻烦运行查询

时间:2015-12-19 19:31:37

标签: php mysqli pdo

这是我的代码:

<?php

require "../include/functions.php";

error_reporting(E_ALL);
ini_set('display_errors', '1'); 

ConnectWithMySQLiDatabase();

$Cat = addslashes($_POST["Category"]);

$v = $conn->query("SELECT * FROM `categories` WHERE `id`=$Cat");
$vrowi = mysql_fetch_array($v);

$url = $conn->real_escape_string($vrowi['Link']);   


if($Cat!=""){
$conn->query("DELETE FROM `products` WHERE `Category`='$Cat'");

以下是ConnectWithMySQLiDatabase();中的functions.php函数:

$CURRENTPATH = dirname(__FILE__);

require_once("$CURRENTPATH/config.php");


function ConnectWithMySQLiDatabase() {

     global $dbhost, $dbuser, $dbpass, $database, $HTTP_SERVER_VARS;

    $conn=mysqli_connect($dbhost, $dbuser, $dbpass, $database);
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }


    $conn->set_charset("utf8");

}

在配置文件中,我已正确设置$dbhost, $dbuser, $dbpass, $database,个变量。

当我尝试运行这些查询时,我收到此错误:

    <br />
<b>Notice</b>:  Undefined variable: conn in <b>/var/www/html/sportsector.bg/admin/fetch_category_products
.php</b> on line <b>19</b><br />
<br />
<b>Fatal error</b>:  Call to a member function query() on a non-object in <b>/var/www/html/sportsector
.bg/admin/fetch_category_products.php</b> on line <b>19</b><br />

我的错误在哪里,我该如何解决?

第二个问题: 到目前为止,我使用mysql_query作为PHP的扩展来连接MySQL数据库。你怎么看。我可以更轻松地使用mysqlipdo作为连接MySQL数据库的新扩展程序。哪一个更靠近旧的mysql_query

提前谢谢!

3 个答案:

答案 0 :(得分:0)

您的$ conn对象永远不会被声明,至少不会在您显示的代码中声明。您假设$ conn是使用mysqli_connect()实例化的全局对象。嗯,事实并非如此。如果要将其用作对象,请使用new关键字调用构造函数。

$ conn = new mysqli_connect($ dbhost,$ dbuser,$ dbpass,$ database);

答案 1 :(得分:0)

首先,不要将自己与编写查询的面向对象程序样式混淆。有些使用面向对象的样式,有些使用程序样式

我已经以两种方式更新了你的代码。选一个。而且,走在前面。然后,出现错误。确定。

面向对象。

<?php

require "../include/functions.php";

error_reporting(E_ALL);
ini_set('display_errors', '1'); 
ConnectWithMySQLiDatabase();

$Cat = addslashes($_POST["Category"]);
$v = $conn->query("SELECT * FROM `categories` WHERE `id`=$Cat");
$vrowi = $v->fetch_array(MYSQLI_ASSOC);
$url = $conn->real_escape_string($vrowi['Link']);   

if($Cat!=""){
$conn->query("DELETE FROM `products` WHERE `Category`='$Cat'");

ConnectWithMySQLiDatabase(); function which is in functions.php:

$CURRENTPATH = dirname(__FILE__);
require_once("$CURRENTPATH/config.php");

function ConnectWithMySQLiDatabase() {
    global $dbhost, $dbuser, $dbpass, $database, $HTTP_SERVER_VARS;
    $conn=new mysqli($dbhost, $dbuser, $dbpass, $database);
    // Check connection
    if ($conn->connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $conn->set_charset("utf8");
}
?>

程序风格

<?php

require "../include/functions.php";

error_reporting(E_ALL);
ini_set('display_errors', '1'); 
ConnectWithMySQLiDatabase();

$Cat = addslashes($_POST["Category"]);
$v =  mysqli_query($conn, "SELECT * FROM `categories` WHERE `id`=$Cat");
$vrowi = mysqli_fetch_array($v, MYSQLI_ASSOC);
$url = $conn->real_escape_string($vrowi['Link']);

if($Cat!=""){
 mysqli_query($conn, "DELETE FROM `products` WHERE `Category`='$Cat'");
ConnectWithMySQLiDatabase(); function which is in functions.php:
$CURRENTPATH = dirname(__FILE__);
require_once("$CURRENTPATH/config.php");

function ConnectWithMySQLiDatabase() {

    global $dbhost, $dbuser, $dbpass, $database, $HTTP_SERVER_VARS;

    $conn=mysqli_connect($dbhost, $dbuser, $dbpass, $database);
    // Check connection
    if ($conn->connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $conn->set_charset("utf8");
}
?>

答案 2 :(得分:-1)

如果您要使用此行:

$url = $conn->real_escape_string($vrowi['Link']);   

然后

$vrowi = mysql_fetch_array($v);

应该是

$vrowi = mysqli_fetch_assoc($v);