请帮我转换MySQL到MySQLi

时间:2015-09-16 13:52:42

标签: php mysql html5 mysqli

请有人向我解释并协助将旧代码转换为新的MySQLi格式吗?我将不胜感激。

这是我目前的代码:

<html>
    <head>
        <title>Last 10 Results</title>
    </head>
    <body>
        <table>
        <thead>
            <tr>
                <td>Id</td>
                <td>Name</td>
            </tr>
        </thead>
        <tbody>
        <?php
            $connect = mysql_connect("localhost","root", "root");
            if (!$connect) {
                die(mysql_error());
            }
            mysql_select_db("apploymentdevs");
            $results = mysql_query("SELECT * FROM demo LIMIT 10 ORDER BY Id");
            while($row = mysql_fetch_array($results)) {
            ?>
                <tr>
                    <td><?php echo $row['Id']?></td>
                    <td><?php echo $row['Name']?></td>
                </tr>

            <?php
            }
            ?>
            </tbody>
            </table>
    </body>
</html>

我再次感谢你的帮助。

这就是我所做的(我很抱歉,我是这样的菜鸟)

                    <?php
                        // Database details
                        $dbhost = 'localhost';
                        $dbuser = '#';
                        $dbpass = '#';
                        $dbname = '#';
                        $db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
                        if($db->connect_errno > 0){
                            die('Unable to connect to database [' . $db->connect_error . ']');
                        }
                        $results = mysqli_query("SELECT * FROM formdata LIMIT 10 ORDER BY id");
                        while($row = mysqli_fetch_array($results)) {
                        ?>
                            <tr>
                                <td><?php echo $row['ID']?></td>
                                <td><?php echo $row['FullName']?></td>
                                <td><?php echo $row['Mobile']?></td>
                                <td><?php echo $row['Email']?></td>
                                <td><?php echo $row['Province']?></td>
                            </tr>

                        <?php
                        }
                        ?>  

2 个答案:

答案 0 :(得分:2)

这里的问题是你没有将连接传递给你的查询

$results = mysqli_query($db, "SELECT 
                        ^^^^

阅读文档:

mysql_不同,它必须作为参数传递。

然后确保您的查询没有失败。

修改

脚注:

正如NaijaProgrammer在对OP的评论中所发现的那样,LIMIT追踪ORDER BY并且完全是我的疏忽。

“建议:1。检查您的表名.2。运行PhpMyAdmin中的代码.3。将ORDER BY语句放在LIMIT语句之前。 - NaijaProgrammer”

SELECT的参考:

手册中的示例:

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [INTO OUTFILE 'file_name' export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]]

我想指出的是,你在这里使用小写字母代表“id”ORDER BY id,这不应该是ORDER BY的问题。

但是, $row['ID']$row['id']之间存在差异。如果您的数据库配置为区分大小写,则需要以小写形式编写ID。

答案 1 :(得分:2)

更新此行:

public RES Post<RES>(string url, string content) where RES : new()
{
    using (var client = new WebClient())
    {
        client.Headers[HttpRequestHeader.ContentType] = "application/json";
        var result = client.UploadString(url, content);
        return JsonConvert.DeserializeObject<RES>(result, jsonSerializerSettings);
    }
}

$results = mysqli_query("SELECT * FROM formdata LIMIT 10 ORDER BY id");