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 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误