mysql vs mysqli配置文件和查询

问题描述 投票:0回答:2

我需要开始使用mysqli扩展名,但是我正在寻找各种有冲突的信息,这取决于我要使用的所有信息的方式。

例如,我的标头连接到当前看起来像这样的'config.php'文件:

<?php
$hostname_em = "localhost";
$database_em = "test";
$username_em = "user";
$password_em = "pass";
$em = mysql_pconnect($hostname_em, $username_em, $password_em) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

但是当我转到php.net时,我看到我应该使用它,但是在更新所有内容后,我没有数据库。

<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";

$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

echo $mysqli->host_info . "\n";
?>

我还仔细检查了一下,并在我的网站的以下代码中添加了一个“ i”,再次没有运气:

mysql_select_db($database_em, $em);
$query_getReview =
"SELECT 
reviews.title,
reviews.cover_art,
reviews.blog_entry,
reviews.rating,
reviews.published,
reviews.updated,
artists.artists_name,
contributors.contributors_name,
contributors.contributors_photo,
contributors.contributors_popup,
categories_name
FROM
reviews
JOIN artists ON artists.id = reviews.artistid
JOIN contributors ON contributors.id = reviews.contributorid
JOIN categories ON categories.id = reviews.categoryid
ORDER BY reviews.updated DESC LIMIT 3";
$getReview = mysql_query($query_getReview, $em) or die(mysql_error());
$row_getReview = mysql_fetch_assoc($getReview);
$totalRows_getReview = mysql_num_rows($getReview);

这是我的显示页面上迄今为止唯一提到mysql的地方:

<?php } while ($row_getReview = mysql_fetch_assoc($getReview)); ?>

[我确实在oracle上看到了一个东西,另一个stackoverflow答案指出有人自动地更新了这些东西,但是此时我的代码太少了,看起来好像已经过时了。

php mysql mysqli database-connection
2个回答
2
投票

向任何mysql函数添加i不会使其成为有效的mysqli函数。即使存在这样的功能,也可能参数不同。在这里看一下http://php.net/manual/en/book.mysqli.php并花一些时间检查mysqli函数。也许尝试一些例子来熟悉事物的工作方式。我也建议您选择任何一种面向对象的代码或过程。不要混合它们。


0
投票

我最近刚切换到mysqli,花了我几个小时来解决这个问题。它对我来说效果很好,希望对您有所帮助。

这里有连接到BD的功能:

function sql_conn(){
    $sql_host = "localhost";
    $sql_user = "test";
    $sql_pass = "pass";
    $sql_name = "test";
    $sql_conn = new mysqli($sql_host, $sql_user, $sql_pass, $sql_name);
    if ($sql_conn->connect_errno) error_log ("Failed to connect to MySQL: (" . $sql_conn->connect_errno . ") " . $sql_conn->connect_error);
    return $sql_conn;
}

这将返回一个Mysqli对象,以后您可以使用该对象进行请求。您可以将其放在config.php中并将其包含或添加到文件的顶部,无论哪种方式最适合您。

[一旦有了这个对象,就可以像这样使用它来对对象进行查询:(在这种情况下,如果出现错误,它将在error_log中输出。我喜欢在那里,可以回显它代替。

    //Use the above function to create the mysqli object.
    var $mysqli = sql_conn();

    //Create the query string (truncated for the example)
    var $query = "SELECT reviews.titl ... ... ted DESC LIMIT 3";

    //Launch the query on the mysqli object using the query() method
    if(!($results = $mysqli->query($query))){
        //It it fails, log the error

        error_log(mysqli_error($mysqli));
    }else{
        //Manipulate your data.
        //here it depends on what you retunr, a single value, row or a list of rows.
        //Example for a set of rows

        while ($record = $results->fetch_object()){ 
          array_push($array, $record); 
        }
    }
   //Just to show, this will output the array:
   print_r($array);

  //Close the connection:
  $mysqli->close();

因此,基本上,在mysqli中,您创建一个对象并使用该方法来解决问题。希望这可以帮助。一旦弄清楚了,您很可能会比mysql更喜欢mysqli。无论如何我都做了。

PS:请注意,这是从现有的有效代码中复制/粘贴的。可能有一些错字,并且可能忘记了在某个地方更改var,但这是为了让您了解mysqli的工作方式。希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.