试图将旧的mysql代码转换为mysqli

时间:2015-09-27 01:56:28

标签: php mysql mysqli amazon-ec2

我必须将我的检查应用程序转换为MySQLi,但自从Amazon EC2更新了MySQL以来一直有很多问题这样做

开始时我不太了解php / mysql,我不知所措。我的大多数搜索都超出了我的理解范围。

这就是文件的样子。

<?php
include("connect.php"); // Connect to RDS
$query="SELECT id, username, oldurl, homedata, clientemail, general_info, company_name, company_hours, company_phone, company_support_email, beyondscope FROM inspector WHERE username='{$_SESSION['username']}' ";
$result=mysql_query($query);
$num = mysql_num_rows ($result);
$username = mysql_result($result,$i,"username");
$oldurl = mysql_result($result,$i,"oldurl");
$homedata = mysql_result($result,$i,"homedata");
$clientemail = mysql_result($result,$i,"clientemail");
$general_info = mysql_result($result,$i,"general_info");
$company_name = mysql_result($result,$i,"company_name");
$company_hours = mysql_result($result,$i,"company_hours");
$company_phone = mysql_result($result,$i,"company_phone");
$company_support_email = mysql_result($result,$i,"company_support_email");
$beyondscope = mysql_result($result,$i,"beyondscope");
mysql_close();

?>

这是我到目前为止所拥有的。我遇到第17行的一个错误是出乎意料的&#39;,&#39; (逗号),即使每一行都有相同的设置。

提前感谢您提供任何帮助。

<?php
include("connect.php"); // Connect to RDS
$query="SELECT id, username, oldurl, homedata, clientemail, general_info, company_name, company_hours, company_phone, company_support_email, beyondscope FROM inspector WHERE username='{$_SESSION['username']}' ";
$result=mysqli_query($GLOBALS["___mysqli_ston"], $query);
$num = mysqli_num_rows($result);
$username = mysqli_fetch_array($result,$i,"username");
$oldurl = mysqli_fetch_array($result,$i,"oldurl");
$homedata = mysqli_fetch_array($result,$i,"homedata");
$clientemail = mysqli_fetch_array($result,$i,"clientemail");
$general_info = mysqli_fetch_array($result,$i,"general_info");
$company_name = mysqli_fetch_array($result,$i,"company_name");
$company_hours = mysqli_fetch_array($result,$i,"company_hours");
$company_phone = mysqli_fetch_array($result,$i,"company_phone");
$company_support_email = ($result,$i, "company_support_email");
$beyondscope = mysqli_fetch_array($result,$i,"beyondscope");
((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);

?>

更新:添加connect.php

<?php
$hostname='.rds.amazonaws.com'; 
$user='username';
$pass='password';
$dbase='dbasename';
$connection = ($GLOBALS["___mysqli_ston"] = mysqli_connect("$hostname" ,  "$user" ,  "$pass")) 
or die ("Can't connect to MySQL");
$db = ((bool)mysqli_query( $connection, "USE " . $dbase)) or die ("Can't select database.");
?>

2 个答案:

答案 0 :(得分:2)

我冒昧地重建了你如何获取你的价值观,这应该更容易阅读和(在我看来)更好的结构。此外,您可以在连接中指定数据库,就像这样(只是为了更容易阅读,真正取决于你)。

$connection = mysqli_connect($hostname, $user, $pass, $dbase);
if (!$connection) {
    echo "An error occurred connecting to the database.";
    exit;
}

以下是您的查询的外观。这将循环遍历所有结果,并将它们放入变量中,只有当我们确实有结果时才会这样。

<?php
include "connect.php"; // Connect to RDS
$query = "SELECT id, username, oldurl, homedata, clientemail, general_info, company_name, company_hours, company_phone, company_support_email, beyondscope FROM inspector WHERE username='{$_SESSION['username']}' ";

if (!$result = mysqli_query($connection, $query)) {
    // An error occured, do something
    // This means no results could be fetched
}
$num = mysqli_num_rows($result);

if (!$result) { // This means that we only fetch if we have a result
    while($row = mysqli_fetch_assoc($result)) {
        // Fetching all the rows
        $username               = $row['username'];
        $oldurl                 = $row['oldurl'];
        $homedata               = $row['homedata '];
        $clientemail            = $row['clientemail'];
        $general_info           = $row['general_info'];
        $company_name           = $row['company_name'];
        $company_hours          = $row['company_hours'];
        $company_phone          = $row['company_phone'];
        $company_support_email  = $row['company_support_email'];
        $beyondscope            = $row['beyondscope'];
    }
}
?>

答案 1 :(得分:1)

JFYI。

将检查应用程序转换为MySQLi绝对没有必要按照其他答案提供的方式。

此类转换的唯一目的是让您的查询安全,而这样的直接转换它仍然容易受到影响。所以,你可以通过单独保留这些代码来节省很多麻烦,结果完全相同。

this answer中描述了正确的方法,但您必须找另一位志愿者为您编写代码。