dongmi4035 2015-09-27 01:56
浏览 50
已采纳

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

I'm having to convert my inspection app to MySQLi but have been having many issues doing so since Amazon EC2 updated their MySQL

With not knowing much about php/mysql to begin with, I'm at a loss. Most of my searches have been way beyond what I understand.

This is what the file used to look like.

<?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();

?>

This is what I have so far. One error I'm getting line 17 has unexpected ',' (comma), even that every line has the same setup.

Thanks in advance for any help with this.

<?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);

?>

UPDATE: To add 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条回答 默认 最新

  • doutong2132 2015-09-27 02:28
    关注

    I've taken the liberty of rebuilding a bit on how you fetch your values, this should be a bit more easier to read and (in my opinion) a better structure. Also, you can specify the database in your connection, like this (just makes for easier reading, up to you really).

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

    Below is how your query could look. This will loop through all the results, and put them into the variables, only if we actually have a result.

    <?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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化