Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysqli questions

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I've only just started using object oriented mysqli and I'm sure that I am overly-complicating the code. Isn't there a simpler way of doing something like this for multiple values without all the semi-duplicates?

Code:
// Values from foreach loop of uploaded files submitted from form
$FileName1 = $FileList[0];
$FileName2 = $FIleList[1];
$FileName3 = $FileList[2];

// Check if file has already been inserted
$FileQuery1 = "SELECT ID FROM fileuploads WHERE FileName = '$FileName1'";
$FileQuery2 = "SELECT ID FROM fileuploads WHERE FileName = '$FileName2'";
$FileQuery3 = "SELECT ID FROM fileuploads WHERE FileName = '$FileName3'";

$Result1 = $mysqli->query($FileQuery1);
$Result2 = $mysqli->query($FileQuery2);
$Result3 = $mysqli->query($FileQuery3);

$FileExists1 = $Result1->fetch_row(); 
$FileExists2 = $Result2->fetch_row();
$FileExists3 = $Result3->fetch_row();
 
Hi

[ul]
[li]This has nothing to do with MySQL. Is a pure PHP question.[/li]
[li]Actually is neither related to mysqli, could be as well related to mysql too.[/li]
[/ul]

Personally I would think to something like this first :
PHP:
[navy]$FileExists[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]array_map[/color][teal]([/teal][green][i]'filename2id'[/i][/green][teal],[/teal] [navy]$FileList[/navy][teal]);[/teal]

[b]function[/b] [COLOR=darkgoldenrod]filename2id[/color][teal]([/teal][navy]$filename[/navy][teal])[/teal]
[teal]{[/teal]
  [b]global[/b] [navy]$mysqli[/navy][teal];[/teal]

  [b]return[/b] [navy]$mysqli[/navy][teal]->[/teal][COLOR=darkgoldenrod]query[/color][teal]([/teal]
    [green][i]"SELECT ID FROM fileuploads WHERE FileName = '"[/i][/green] [teal].[/teal] [navy]$mysqli[/navy][teal]->[/teal][COLOR=darkgoldenrod]real_escape_string[/color][teal]([/teal][navy]$filename[/navy][teal])[/teal] [teal].[/teal] [green][i]"'"[/i][/green]
  [teal])->[/teal][COLOR=darkgoldenrod]fetch_row[/color][teal]();[/teal]
[teal]}[/teal]
Of course, this was kept simple.

The mysqli way should look like this instead :
PHP:
[navy]$statement[/navy] [teal]=[/teal] [navy]$mysqli[/navy][teal]->[/teal][COLOR=darkgoldenrod]prepare[/color][teal]([/teal][green][i]'SELECT ID FROM fileuploads WHERE FileName = ?'[/i][/green][teal]);[/teal]

[navy]$FileExists[/navy][teal]=[/teal][COLOR=darkgoldenrod]array_map[/color][teal]([/teal][green][i]'filename2id'[/i][/green][teal],[/teal][navy]$FileList[/navy][teal]);[/teal]

[b]function[/b] [COLOR=darkgoldenrod]filename2id[/color][teal]([/teal][navy]$filename[/navy][teal])[/teal]
[teal]{[/teal]
  [b]global[/b] [navy]$statement[/navy][teal];[/teal]

  [navy]$statement[/navy][teal]->[/teal][COLOR=darkgoldenrod]bind_param[/color][teal]([/teal][green][i]'s'[/i][/green][teal],[/teal] [navy]$filename[/navy][teal]);[/teal]

  [b]if[/b] [teal]([/teal][navy]$statement[/navy][teal]->[/teal][COLOR=darkgoldenrod]execute[/color][teal]())[/teal]
    [b]return[/b] [navy]$statement[/navy][teal]->[/teal][COLOR=darkgoldenrod]fetch[/color][teal]();[/teal]

  [b]return[/b] [b]null[/b][teal];[/teal]
[teal]}[/teal]


Feherke.
[link feherke.github.com/][/url]
 
My apologies for posting in the wrong forum. I hadn't realized I had clicked the wrong one but thank you for your most helpful reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top