MySQL Query Not Working

bauss

New member
I'm trying to code a addon for Xenforo and the following code isn't working.

Code:
<?php

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("thepoker_db");

$query = mysql_query("SELECT user.username
                , user.bcp_lbp
                , user.bcp_freeroll_money_won
                , profile.field_value AS poker_user
             FROM xf_user
           LEFT OUTER
            JOIN xf_user_field_value AS profile
              ON profile.user_id = user.user_id
             AND profile.field_id = '626c61636b5f636869705f706f6b65725f757365726e616d65'
           WHERE user.secondary_group_ids IN (2,4,5)");
$n = 0;

	 while($results = mysql_fetch_array($query))
            {
			    $n++;
                print "<table><tr><td>#." . $n++ . "</td>";
                print "<td>" .$results['username'] . "</td>";
				print "<td>" . $results['poker_user'] . "</td>";
                print "<td align=\"center\">". $results['bcp_lbp'] ."</td>";
				print "<td align=\"center\">$". $results['bcp_freeroll_money_won'] ."</td></tr></table>";
            }

?>

I get the error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\pokertalk\leaderstest.php on line 19

I'm using Wamp Server. Any idea how to fix it?
 
I'm trying to code a addon for Xenforo and the following code isn't working.

Code:
<?php

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("thepoker_db");

$query = mysql_query("SELECT user.username
                , user.bcp_lbp
                , user.bcp_freeroll_money_won
                , profile.field_value AS poker_user
             FROM xf_user
           LEFT OUTER
            JOIN xf_user_field_value AS profile
              ON profile.user_id = user.user_id
             AND profile.field_id = '626c61636b5f636869705f706f6b65725f757365726e616d65'
           WHERE user.secondary_group_ids IN (2,4,5)");
$n = 0;

	 while($results = mysql_fetch_array($query))
            {
			    $n++;
                print "<table><tr><td>#." . $n++ . "</td>";
                print "<td>" .$results['username'] . "</td>";
				print "<td>" . $results['poker_user'] . "</td>";
                print "<td align=\"center\">". $results['bcp_lbp'] ."</td>";
				print "<td align=\"center\">$". $results['bcp_freeroll_money_won'] ."</td></tr></table>";
            }

?>

I get the error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\pokertalk\leaderstest.php on line 19

I'm using Wamp Server. Any idea how to fix it?

You have missed querying the database. Your Query has not yet been executed by the time the
Code:
while($results = mysql_fetch_array($query))
gets executed. The mysql_fetch_array - needs a recordset as a parameter and not the query you are passing to it.
here should be the fixed code -
Code:
<?php

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("thepoker_db");

$query = mysql_query("SELECT user.username
                , user.bcp_lbp
                , user.bcp_freeroll_money_won
                , profile.field_value AS poker_user
             FROM xf_user
           LEFT OUTER
            JOIN xf_user_field_value AS profile
              ON profile.user_id = user.user_id
             AND profile.field_id = '626c61636b5f636869705f706f6b65725f757365726e616d65'
           WHERE user.secondary_group_ids IN (2,4,5)");
$n = 0;
$result=mysql_query($query)                                  // Execute the Query to generate the Recordset
	 while($results = mysql_fetch_array($result))    // Loop through the Recordest
            {
			    $n++;
                print "<table><tr><td>#." . $n++ . "</td>";
                print "<td>" .$results['username'] . "</td>";
				print "<td>" . $results['poker_user'] . "</td>";
                print "<td align=\"center\">". $results['bcp_lbp'] ."</td>";
				print "<td align=\"center\">$". $results['bcp_freeroll_money_won'] ."</td></tr></table>";
            }

?>
 
Try this:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql)

// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)\n",$row[0],$row[1]);

// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]);

// Free result set
mysqli_free_result($result);

mysqli_close($con);
?>
 
Back
Top