0

I know I am having a syntax error here but I can't figure this out.

I'm trying to get the result of a single specific record in my database to display in a PHP variable. Seems simple enough but apparently not for me. Here is the code I'm trying to run:

    // Create connection
    $con=mysqli_connect($mysqlserver,$mysqluser,$mysqlpass,$mysqldb);

    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $SQL = "SELECT * FROM players_brixx_gastonia WHERE email='catheygreaney@example.com'"; 
    $result = mysqli_query($SQL,$con);
    $result1 = mysqli_fetch_assoc($result);
    $prevpoints1 = $result1['points'];
    echo $prevpoints1;

    mysqli_close($con);

My page is outputting: "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in"

Which normally means my query is failing however when running the query by itself in phpmyadmin, it completes just fine displaying the row I need.

Whats up with this?

UPDATE:

KaNch solved my issue. Thanks!

Michael White
  • 87
  • 2
  • 9

3 Answers3

2
 // Create connection
$con=mysqli_connect($mysqlserver,$mysqluser,$mysqlpass,$mysqldb);

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$SQL = "SELECT * FROM players_brixx_gastonia WHERE email='catheygreaney@example.com'"; 
$result = mysqli_query($con,$SQL);
$result1 = mysqli_fetch_assoc($result);
$prevpoints1 = $result1['points'];
echo $prevpoints1;

mysqli_close($con);
PrakashSharma
  • 421
  • 5
  • 15
0

Use this, you haven't created a connection to the database. First create a connection to the database as follows

$con = mysqli_connect("localhost","usrname","password","db_name");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Execute your query as below

$result = mysqli_query($con, "SELECT * FROM players_brixx_gastonia WHERE email='catheygreaney@example.com'");
while($row = mysqli_fetch_array($result))
{
    echo $row['points'];
}
Royden Rego
  • 132
  • 2
  • 13
0

mysql_query returna a resource if the query is executed successfully, or FALSE if there is an error. Since mysql_fetch_assoc complains about the argument being a boolean, maybe you have an error in your query.

Machavity
  • 29,816
  • 26
  • 86
  • 96
riss
  • 1
  • 1