Page 1 of 1

PHP Database Code

Posted: Fri Jul 08, 2011 5:25 am
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

Re: PHP Database Code

Posted: Fri Jul 08, 2011 8:30 am
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);

Re: PHP Database Code

Posted: Fri Jul 08, 2011 9:00 am
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

Re: PHP Database Code

Posted: Fri Jul 08, 2011 1:06 pm
by McInfo
Notice the "i" and the order of the arguments:

Code: Select all

mysql_query($query, $link);
mysqli_query($link, $query);

Re: PHP Database Code

Posted: Fri Jul 08, 2011 1:24 pm
by Celauran
McInfo wrote:Notice the "i" and the order of the arguments:
Ah, my bad. Completely missed that.