-5

I can't figure out why I keep getting this error. This is the code I have. I have tried switching stuff around and no luck. I got the code from my book, so I would hope it would work, but that's not always the case.

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\CIS224\Company_Cars.php on line 16

<!DOCTYPE hmtl PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>
<title>Create Database</title>
</head>

<body>
<?php
$SQLstring = "SELECT * FROM company_cars";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
echo "<table width ='100%' border='1'\n";
echo "<tr><th>License</th><th>Make</th><th>Model</th><th>Mileage</th><th>Year</th></tr>\n";
while ($Row = mysqli_fetch_array($result) !== FALSE) {
    echo "<tr><td>{$Row[0]}</td>";
    echo "<td>{$Row[1]}</td>";
    echo "<td>{$Row[2]}</td>";
    echo "<td align='right'>{$Row[3]}</td>";
    echo "<td>{$Row[4]}</td></tr>\n";
    }
echo "</table>\n";

?>
</body>
</html>
user2474677
  • 1
  • 1
  • 1
  • Stop using `@` for starters. Then start using `mysql_error()` to figure out what's wrong. Unless you are a fan of stabbing into the dark, but then don't ask us. – deceze Jun 11 '13 at 13:19
  • Once again : [Don't use mysql_* extension](http://www.php.net/en/mysql_query) as they’re deprecated. Use [PDO](http://php.net/manual/en/book.pdo.php) or [MSQLi](http://php.net/manual/en/book.mysqli.php) instead. – xlecoustillier Jun 11 '13 at 13:21
  • 1
    You're mixing **`mysql`** and **`mysqli`** commands. The two are not interchangeable... – War10ck Jun 11 '13 at 13:28

4 Answers4

2

It seems you forgot to connect to the database. Try

$DBConnect = mysql_connect('host', 'user', 'pass');
$db = mysql_select_db('dbname');

Also you are calling mysqli_fetch_array, it should be mysql_fetch_array:

while ($Row = mysqli_fetch_array($QueryResult) !== FALSE) {
//                 ^ remove      ^ also this!!!

Unless you are actually using mysqli_*, in which case the query call is wrong. You were also passing the wrong variable, your result is $QueryResult not $result.

You really should ditch mysql_* and learn PDO or MySQLi.

MrCode
  • 63,975
  • 10
  • 90
  • 112
0

Remove @ in mysql_query() and try..

Shankar Akunuri
  • 177
  • 3
  • 14
0

Why are you using @ in mysql_query it returns true or false.

$QueryResult = mysql_query($SQLstring, $DBConnect);

remove the @ as I have done in code. and it will work.

And there is also another problem in your mysql_fetch_array function:

while ($Row = mysql_fetch_array($QueryResult))

Use the above code instead of your while code.

And always use the PDO Mysql.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

change in while loop

  while ($Row = mysqli_fetch_array($QueryResult) !== FALSE) {

replace

  ($result) to ($QueryResult)
Rajendra Yadav
  • 645
  • 3
  • 12