从MySQL转换为MySQLi面向对象

时间:2016-02-16 02:22:44

标签: php mysql twitter-bootstrap

好的,所以我被告知最好转换到新的mysqli 所以我一直在一个新的网站上工作,到目前为止一直很好,但我遇到了一个问题,我无法弄清楚如何将其转换为我的搜索查询

我在我的网站上添加了搜索功能,但现在我无法使用它。

这是我的旧代码:

1. Open Services (services.msc) and restart MySQL57 service.
2. Execute the following commands in MySQL.
   use <<database name>>;
   set global optimizer_switch='derived_merge=OFF';
3. Update the .edmx.

我已将代码升级为用户oo

这就是我所拥有的:

$query = "SELECT * FROM snippet_tools WHERE `db_title` LIKE ".sql_val('%'.$_GET['search'].'%')." OR `db_body`=".sql_val('%'.$_GET['search'].'%');
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());

$anymatches = mysql_num_rows($result);

if ($anymatches == 0 ) {

但每次我运行它来执行搜索时,我都会收到此错误消息:

注意:尝试在第611行的H:\ root \ site5 \ questions.php中获取非对象的属性

2 个答案:

答案 0 :(得分:0)

您可以参考W3学校网站,了解MySQL和MySQLi的变体及其功能。继续你的项目真的很有帮助。

Database connection example in both ways

所以你可以根据这个概念解决你的问题。尝试提取概念。

答案 1 :(得分:0)

尝试这种方法!

<?php

    error_reporting(E_ALL);

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

    $get = array('search' => "Lon");

    $sql_search = "SELECT * FROM cities WHERE (city_name LIKE '%$get[search]%')";

    $result = $conn->query($sql_search);

    $anymatches = $result->num_rows;

    if ($anymatches == 0 ){
        echo "No matches!";
    }
    else
    {
        echo $anymatches . " match found!";
    }

?>



Screenshot of my database

Screenshot of the result