Jump to content

Recommended Posts

Hi,

 

thanks for the reply and for some reason I'm getting this error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in: I don't know what the problem is.

function products () {

   $con = mysqli_connect("localhost","root","");


   if (!$con) {
     die('Could not connect: ' . mysql_error());
    }


    mysql_select_db("bicycle");




 $result = mysqli_query($con,"SELECT BikeCode, Model  FROM bike");




 while($row = mysqli_fetch_array($result))
  {
   echo $row['BikeCode'] . " " . $row['Model'];
   echo "
";
 }

Your query failed and it's returning false. You can't pass a boolean through the mysqli_fetch_array() function.

 

$sql = "SELECT BikeCode, Model FROM bike";

$result = mysqli_query($con, $sql) or die("Error: Failed to query database. ({$sql})");

Edited by Xaotique

You'll find that the error in your query is because you haven't selected a database.

 

You cannot mix mysql (no i) and mysqli (with an i) statements.

 

You either need to specify the database in the mysqli_connect statement or use a mysqli_select_db statement (or even specify the database name in your query statement.)

I added a new mysqli_select_db statement, but it still gives me that error and I thought it was because my MySQL service wasn't running, but it seems to working fine.

 

 function products () {

   $con = mysqli_connect("localhost","root","");


   if (!$con) {
     die('Could not connect: ' . mysqli_error());
    }
   else {
        echo 'connected';
   } 


   $db= mysqli_select_db("bicycle");




 $sql = "SELECT BikeCode, Model FROM bike";
$result = mysqli_query($db, $sql) or die("Error: Failed to query database. ({$sql})"); 




 while($row = mysqli_fetch_array($result))
  {
   echo $row['BikeCode'] . " " . $row['Model'];
   echo "
";
 }










   }


If it's the same as mysql functions, you have to specify the connection as the first parameter in the mysql_select_db (atleast I think it's a must, never tried without).

 

You could also double check your query in phpmyadmin to make sure it's valid.

Edited by Xaotique

The mysqli_select_db statement requires the mysqli link resource ($con in your case) as the first parameter. Do you have php's error_reporting set to E_ALL so that all the php detected errors will be reported?

 

Also, the mysqli_error() statement requires the mysqli link resource as a parameter.

Yes, I have it on E_ALL. http://tinypic.com/r/e5jybk/6

 

 

 

function products () {


$con = mysqli_connect("localhost","root","");


if (!$con) {
die('Could not connect: ' . mysqli_error());
}
else {
echo 'connected';
}


$db= mysqli_select_db($con,"bicycle");




$sql = "SELECT BikeCode, Model FROM bike ";
$result = mysqli_query($db, $sql) or die("Error: Failed to query database. ({$sql})");




while($row = mysqli_fetch_array($result))
{
echo $row['BikeCode'] . " " . $row['Model'];
echo "
";
}

}

Edited by Krux20

If you are still getting an error at your mysqli_fetch_assoc statement, with the code you have been posting, it's because some of your code inside of your while(){} loop, that you have been cutting out of the post, is reusing and overwriting the $result variable.

This is all my code




<?php
error_reporting(E_ALL);
session_start();



$page = 'index.php';



// $_SESSION['cart'. $_GET['add']] = 1;
// $_SESSION['cart'] += 1;






function products () {


$con = mysqli_connect("localhost","root","");


if (!$con) {
die('Could not connect: ' . mysqli_error());
}
else {
echo 'connected';
}


$db= mysqli_select_db($con,"bicycle");

$sql = "SELECT BikeCode,Model FROM bike ";
$result = mysqli_query($db, $sql) or die("Error: Failed to query database. ({$sql})");

while($row = mysqli_fetch_array($result))
{
echo $row['BikeCode'] . " " . $row['Model'];
echo "<br />";
}





}

?>

Edited by Krux20

The error message you are getting now cannot possibly be the same one at the start of this thread, because you are using the WRONG variable name as the first parameter to the mysqli_query() statement. $db ISN'T the connection link. You should be getting a php error message at the mysql_query() statement.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.