Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

i try change mysql code to pdo

and make this codes:

<?php
$jds="SELECT tc,tn,tb,tfz,tmz FROM teams WHERE leag='$lig'  ORDER BY tmz DESC, tfz DESC, tzade DESC LIMIT 18";
$sth = $conn->prepare($jds);
$sth->execute();
if ($shom = $sth->rowCount() > 0){
$j=0;
    while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$j++;
if($row['tc'] == $team_id){
?>
some html codes 
<?php
}
else{
?> other html codes

but i think have sql injection problem and some problems in $lig

then change to this code: $lig in select --> :lig and execute() --> execute(array(':lig' => $lig))

<?php
$jds="SELECT tc,tn,tb,tfz,tmz FROM teams WHERE leag=:lig  ORDER BY tmz DESC, tfz DESC, tzade DESC LIMIT 18";
$sth = $conn->prepare($jds);
$sth->execute(array(':lig' => $lig));
if ($shom = $sth->rowCount() > 0){
$j=0;
    while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$j++;
if($row['tc'] == $team_id){
?>

but not work

can you help me to make my codes best permorfamnce?

sorry for bad english i am beginner

share|improve this question

closed as off-topic by Gareth Rees, syb0rg, Bobby, rolfl, Uri Agassi Feb 19 at 13:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After getting your code to work, you may edit this question seeking a review of your working code." – Gareth Rees, syb0rg, Bobby, rolfl, Uri Agassi
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Hi, and welcome to CodeReview. Here we review working coe. If you code is not working you shoud consider asking on Stack Overflow, but, you will need to add a lot more detail before you do that, like what part is not working, and what errors you are getting. –  rolfl Feb 19 at 13:34
    
@rolfl Thanks for help –  MinA Feb 19 at 15:21
add comment

1 Answer

up vote 2 down vote accepted

Please, READ THE MANUAL. The PDOStatement::rowCount() method does not apply to SELECT statements:

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

You're SELECT-ing, which returns a resultset, and doesn't affect rows. It merely fetches them.
You need this:

if ($sth->execute(array(':lig' => $lig)))
{//if this returns true, the query was successful
    $j = 0;
    while ($row = $sth->fetch(PDO::FETCH_ASSOC))
    {
        ++$j;
        /* code here */
    }
}

Or, if you need to know how many rows you selected:

$result = $sth->fetchAll(PDO::FETCH_ASSOC);
$rowCount = count($result);
foreach($result as $row)
{
    if ($row['tc'] == $team_id)
    {
    }
    else
    {
    }
}

One thing I do wonder about, though:

if($row['tc'] == $team_id)

If you are looking for the row where the tc field has a distinct value, why not add it to the WHERE clause of your query?

$sth = $conn->prepare(
    'SELECT tc,tn,tb,tfz,tmz 
     FROM teams
     WHERE leag=:lig
       AND tc = :tc
     ORDER BY tmz DESC, tfz DESC, tzade DESC
     LIMIT 18'
);
$bind = array(':lig' => $lig, ':tc' => $team_id);
if ($sth->execute($bind))
{//pass array to execute here
    $j = 0;
    while ($row = $sth->fetch(PDO::FETCH_ASSOC))
    {
        ++$j;
        /* code here */
    }
}
share|improve this answer
    
but i have else for $row['tc'] == $team_id and i add to my question so what can i do? –  MinA Feb 19 at 13:27
    
@MinA: The else wasn't in your question, so I didn't know that. If there's an else, then you needn't add it to your WHERE clause. Ignore that bit. The rest of my answer, though, still applies. Especially the strong recommendation to read the documentation –  Elias Van Ootegem Feb 19 at 14:09
    
Thank you for very good helping –  MinA Feb 19 at 14:17
    
can you help me in this question? stackoverflow.com/questions/22176016/… –  MinA Mar 4 at 15:46
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.