0

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.");
?>
bpross
  • 363
  • 3
  • 11
  • If you check PHP.net for each of the functions, it'll provide documentation for it. Things are, for the most part, called the same. So just check PHP.net for each function, such as `mysqli_fetch-array`. If you're unsure, you can search for it, or find it via Google. – Qirel Sep 27 '15 at 02:05
  • That's where I found that part and they had the underscore like I have but it's that piece after $result is where my error is popping up. – bpross Sep 27 '15 at 02:10
  • Alright, I'll post you an answer how. Can you please update your post with what `connect.php` contains? – Qirel Sep 27 '15 at 02:12

2 Answers2

2

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'];
    }
}
?>
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Wow!, thank you so much. Something new try and get used to. I only do this for my own stuff for work. I'd never be able to do this full time. Too much to keep up on. I'll run it shortly, then I'll start working on the next error. The last couple weeks have been a bear. Hoping to get it ironed out in a couple more weeks. I hate using paper for my rental inspections. – bpross Sep 27 '15 at 02:47
1

JFYI.

There is absolutely no point in converting your inspection app to MySQLi the way it offered in the other answer.

The only point in such a conversion is to make your queries safe while with such a direct conversion it remained congenially vulnerable. So, you might saved yourself a lot of trouble by leaving this code alone, with exactly the same outcome.

Proper way is described in this answer, but you will have to find another volunteer to write a code for you.

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Thanks JFYI. Since I'm the only one that uses this I decided to eliminate MySQL all together for this. It's to difficult for me to work this change in all the files. Nothing is sensitive in those fields, and reading/writing to a few text files is a lot easier than trying to get MySQL working again, for me anyway. – bpross Sep 27 '15 at 12:30
  • When each rental inspection is finished, I have it all put to pdf and those fields are reset or NULL every time ready for the next. So no real sense to keep using the database for something that will last an hour or two. When I wrote this a few years ago I used the database setup just in case others at work might have wanted to use it. I was tired of home hours(no pay) finishing and entering data. But they still wanted to use paper and were used to spending a few hours at home home each day. I wanted to get it over and done with and not have to bring work home if possible. – bpross Sep 27 '15 at 12:30