• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2168
  • Last Modified:

mysqli_query problem

Can you see anything wrong with the following?

Line 26  $sqlquery = "SELECT * FROM $table WHERE (state = va)";
Line 27  $result = mysqli_query($sqlquery);
Line 28  $number = mysqli_num_rows($result);

I get the following errors:

Warning: mysqli_query() expects at least 2 parameters, 1 given in c:\Inetpub\wwwroot\zip\db_connect.php on line 27

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in c:\Inetpub\wwwroot\zip\db_connect.php on line 28

0
comtekso
Asked:
comtekso
  • 7
  • 5
  • 3
  • +1
3 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
mysqli_query: you have to pass the connection handle as first parameter.
the second error will go away as the first one is solved
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
 
soapergemCommented:
Somewhere in your code, you probably do this:

mysqli_connect('localhost', 'my_user', 'my_password');

Make sure to save that as a variable, like this:

$link = mysqli_connect('localhost', 'my_user', 'my_password');

Then use this:

$result = mysqli_query($link, $sqlquery);

Please note that this is exactly what angelIII said in his first reply, just expanded.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
comteksoAuthor Commented:
OK, fixed the first problem, now I am getting this:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in c:\Inetpub\wwwroot\zip\db_connect.php on line 28

$sqlquery = "SELECT * FROM $table WHERE (state = va)";
$result = mysqli_query($link, $sqlquery);
$number = mysqli_num_rows($result);
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please try this:
$sqlquery = "SELECT * FROM $table WHERE (state = va)";
$result = mysqli_query($link, $sqlquery,MYSQLI_USE_RESULT);
$number = mysqli_num_rows($result);
0
 
comteksoAuthor Commented:
I tried displaying $result using print_r, but there doesn't appear to be anything in $result
0
 
comteksoAuthor Commented:
Same error msg with the following:

$sqlquery = "SELECT * FROM $table WHERE (state = va)";
$result = mysqli_query($link, $sqlquery,MYSQLI_USE_RESULT);
$number = mysqli_num_rows($result);
0
 
comteksoAuthor Commented:
Would the amount of data returned be a problem. When I run the SELECT command thru the command-line interface I get 1200 records.
0
 
star_trekCommented:
try the following
$sqlquery = "SELECT * FROM $table WHERE state = 'va' ";
if($result = mysqli_query($link, $sqlquery)) {
$number = mysqli_num_rows($result);
printf("The number of rows are %d\n",$number);
}

To fetch the rows use mysql_fetch_assoc or mysqli_fetch_array
0
 
soapergemCommented:
One thing I wanted to point out:

Don't you need quotes around 'va' in your query?

$sqlquery = "SELECT * FROM $table WHERE (state = 'va')";
0
 
soapergemCommented:
Oh, I see star_trek noticed this too, since he changed his code to reflect this. (sorry I didn't see your comment right away)
0
 
comteksoAuthor Commented:
OK here is what I get when I use star_trek's suggestion:

Notice: Undefined variable: number in c:\Inetpub\wwwroot\zip\db_connect.php on line 36

Line 36 = if ($number < 1) {

How can I tell which variable is empty? $result or $number? Shouldn't I be able to echo $result? When I try it I don't get anything.


<?php
 /* declare some relevant variables */
$DBhost = "localhost";
$DBuser = "triton";
$DBpass = "pass5620";
$DBName = "triton";
$table = "zipcodes";

$link = mysqli_connect($DBhost,$DBuser,$DBpass);
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

// $sqlquery = "SELECT * FROM $table WHERE (zipcode = 24352)";
// $result = mysqli_query($link,$sqlquery,MYSQLI_USE_RESULT);
// $number = mysqli_num_rows($result);

$sqlquery = "SELECT * FROM $table WHERE state = 'va' ";
if($result = mysqli_query($link, $sqlquery)) {
$number = mysqli_num_rows($result);
printf("The number of rows are %d\n",$number);
}

$i = 0;

if ($number < 1) {
print "<CENTER><P>There Were No Results for Your Search</CENTER>";
}
else {
while ($number > $i) {
$thename = mysqli_result($result,$i,"name");
$theemail = mysqli_result($result,$i,"email");
print "<p><b>Name:</b> $thename<br><b>E-Mail:</b>
$theemail</p>";
$i++;
}
}
?>
0
 
soapergemCommented:
Aaaah! Don't post your passwords! Bad bad bad!
0
 
star_trekCommented:
$number is defined in the if scope,
try the following code
<?php
 /* declare some relevant variables */
$DBhost = "localhost";
$DBuser = "triton";
$DBpass = "pass5620";
$DBName = "triton";
$table = "zipcodes";

$link = mysqli_connect($DBhost,$DBuser,$DBpass);
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

if($result = mysqli_query($link, $sqlquery)) {
      $number = mysqli_num_rows($result);
      if($number < 1) {
           print "<CENTER><P>There Were No Results for Your Search</CENTER>";
      } else {
           while($row = $result->fetch_assoc()) {
                   $thename = $row["name"];
                  $theemail = $row["email"];
                  print "<p><b>Name:</b>$thename<br><b>E-Mail:</b>$theemail</p>";
            }
      }
} else {
     print "<CENTER><P>There Were No Results for Your Search</CENTER>";
}    
?>
0
 
star_trekCommented:
Just to be consistent with the style
change the following
while($row = $result->fetch_assoc()) {
                   $thename = $row["name"];
                  $theemail = $row["email"];
                  print "<p><b>Name:</b>$thename<br><b>E-Mail:</b>$theemail</p>";
            }

to

while($row = $result->fetch_assoc($result)) {
                   $thename = $row["name"];
                  $theemail = $row["email"];
                  print "<p><b>Name:</b>$thename<br><b>E-Mail:</b>$theemail</p>";
            }
0
 
soapergemCommented:
$result must be empty. $number is also empty, but that's cause-and-effect. Your code says that "if $result is OK, then define $number like this..." If $result is empty, then $number never gets defined and you end up with that problem. Perhaps this is a question for the other experts, but what is the advantage of using the mysqli_ functions over the typical mysql_ functions? I've only used those ones in the past, and never had any problems.
0
 
comteksoAuthor Commented:
Thanks for the help so far. I may go ahead and drop the mysqli, but it will have to wait until I am back in the office on Monday.
0
 
comteksoAuthor Commented:
OK I switched to mysql instead of mysqli. and everything works so far. I had to edit my php.ini to load mysql.dll. Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now