PHP Database Code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dongowarrior
Forum Newbie
Posts: 3
Joined: Fri Jul 08, 2011 5:03 am

PHP Database Code

Post by Dongowarrior »

Hello All,

I need a bit opf guidance with a peice of my code. I have been designing a website for the past couple of months and i am now at the clean up stage. I am have some frustration with a snippet of code i am trying to simply. Here is the code;

$sqlCommand = "SELECT pagetitle FROM games WHERE category='action'";
$query = mysqli_query($myConnection, $sqlCommand & ' LIMIT 39') or die(mysqli_error($myConnection));

$query2 = mysqli_query($myConnection, $sqlCommand &' LIMIT 80,119') or die(mysqli_error($myConnection));

$query3 = mysqli_query($myConnection, $sqlCommand &' LIMIT 80,119') or die(mysqli_error($myConnection));

$query4 = mysqli_query($myConnection, $sqlCommand &' LIMIT 120, 159') or die(mysqli_error($myConnection));
//List A-Z 1---------------------------------------------
$menusystem = '';
while ($row = mysqli_fetch_array($query)) {
$pagetitle = $row["pagetitle"];
$menusystem .= '<a href="Play.php?Game=' . $pagetitle . '">' . $pagetitle . '</a></br>';
}
mysqli_free_result($query);
//List A-Z 2---------------------------------------------
$menusystemB = '';
while ($row = mysqli_fetch_array($query2)) {
$pagetitle = $row["pagetitle"];
$menusystemB .= '<a href="Play.php?Game=' . $pagetitle . '">' . $pagetitle . '</a></br>';
}
mysqli_free_result($query2);
//List A-Z 3---------------------------------------------
$menusystemC = '';
while ($row = mysqli_fetch_array($query3)) {
$pagetitle = $row["pagetitle"];
$menusystemC .= '<a href="Play.php?Game=' . $pagetitle . '">' . $pagetitle . '</a></br>';
}
mysqli_free_result($query3);
//List A-Z 4---------------------------------------------
$menusystemD = '';
while ($row = mysqli_fetch_array($query4)) {
$pagetitle = $row["pagetitle"];
$menusystemD .= '<a href="Play.php?Game=' . $pagetitle . '">' . $pagetitle . '</a></br>';
}
mysqli_free_result($query4);

As you can see its very simple, i have 4 menus each displaying 40 values (lists basically). What i am trying to acheive is using the $sqlcommand values i want to add on the limit in the $query. For example $sqlcommand line and the add the limit on the end, "SELECT pagetitle FROM games WHERE category='action' LIMIT ??". Its obviously the & "LIMIT ??" on each $query thats the problem.

Ay suggestions would be fantastic
regards
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Database Code

Post by Celauran »

mysql_query()

Two things:
1. You've got your arguments in the wrong order.
2. Concatenate with .

Code: Select all

$res = mysql_query($query . " LIMIT 30", $connection);
Dongowarrior
Forum Newbie
Posts: 3
Joined: Fri Jul 08, 2011 5:03 am

Re: PHP Database Code

Post by Dongowarrior »

Hi,

Thank you for you input. Going by your reply i have changed my code to this;
mysqli_query($sqlCommand . " LIMIT 39", $myConnection)

It is now throwing me a mysql error. am i still doing something wrong?

regards and thanks for the help
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP Database Code

Post by McInfo »

Notice the "i" and the order of the arguments:

Code: Select all

mysql_query($query, $link);
mysqli_query($link, $query);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Database Code

Post by Celauran »

McInfo wrote:Notice the "i" and the order of the arguments:
Ah, my bad. Completely missed that.
Post Reply