Jump to content

mysqli_query() expects parameter 1 to be mysqli


kurt.santo

Recommended Posts

Working on the following script:

echo '<p>Please choose a country</p>';require_once ('mysql_connect.php'); // connect to database// county displayif (isset($_GET['region'])){	$region = (int)$_GET['region'];// Scotland	if ($region == 1) {	$query = "SELECT county.county_name WHERE region_id = 1";	} else {// other counties	$query = "SELECT county.county_name WHERE region_id = 2";	}$result = mysqli_query ($dbc, $query);// display all counties in selected regionwhile ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)){echo "<span><a href=\"Testing.php?county={$row ['county_id']}\">{$row['county']}</a></div>";} //end of while loop

As soon as the parameter is passed with url the following error message "mysqli_query() expects parameter 1 to be mysqli, resource given" complains about the line reading "$result = mysqli_query ($dbc, $query);". What does the error message exactly mean? Tried so many combinations right now, but always same result...Kurt

Link to comment
Share on other sites

Are you sure 'mysql_connect.php' starts a databse connection with something like

$dbc = mysqli_connect($host, $username, $passwd, $dbname);

replacing each value with what is suppose to be there. In other words, are you sure you are really connected to the database?By the way, why don't you try to use MySQLi in it's OOP fashion? You know, like

$dbc = new mysqli($host, $username, $passwd, $dbname);$result = $dbc->query($query);while($row = $result->fetch_array()) {//Something to do with $row}

Link to comment
Share on other sites

Are you sure 'mysql_connect.php' starts a databse connection with something like
// database access informationDEFINE ('DB_USER', 'root');DEFINE ('DB_PASSWORD', 'honkytonk');DEFINE ('DB_HOST', 'localhost');DEFINE ('DB_NAME', 'testing');if ($dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) { // establish connnection.	if (!mysql_select_db (DB_NAME)) { // if cannot select database		// handle  error		trigger_error("Could not select the database!\n<br />MySQL Error: " . mysql_error());		// print message, include footer and exit.		exit();	} // End of mysql_select_db IF.} else { // if couldn't connect to MySQL	// print message, include footer and exit	trigger_error("Could not connect to MySQL!\n<br />MySQL Error: " . mysql_error());	exit();} 

I would like to keep my database connection info in the config file. I am going to move it out of the public dir for security reasons. Can you see what is going wrong in my script?Kurt

Link to comment
Share on other sites

As bright as the sun!It's using mysql functions, not mysqli ones. Rename the mysql_connect and mysql_select_db calls to respectively mysqli_connect and mysqli_select_db.

Link to comment
Share on other sites

As bright as the sun!It's using mysql functions, not mysqli ones. Rename the mysql_connect and mysql_select_db calls to respectively mysqli_connect and mysqli_select_db.
boen_robot,Changed and now it complains "mysqli_select_db() expects exactly 2 parameters, 1 given ". Which would be the second parameter? In the problemematic line (if (!mysqli_select_db (DB_NAME)) I am just checking if the problem is the connection to the database. I am confused what else would need to go there...Then, I cannot mix mysql and mysqli functions? Kurt
Link to comment
Share on other sites

Then, I cannot mix mysql and mysqli functions?
Nope. You can't.Look at the manual for goodness' sake! It's all here.If you'll be using the procedural style, you must explicitly give a link to the database connection like:
mysqli_select_db ($dbc, DB_NAME)

Link to comment
Share on other sites

Nope. You can't.Look at the manual for goodness' sake! It's all here.If you'll be using the procedural style, you must explicitly give a link to the database connection like:
mysqli_select_db ($dbc, DB_NAME)

boen_robot,did this at same time as you answered with:if (!mysqli_select_db ($dbc,DB_NAME)) I also changed $select to $query = "SELECT county_name FROM county WHERE region_id = 1" (other one accordingly). I did this because I saw this syntax more often and thought it might cause problems.Now line echo "<span><a href=\"Testing.php?county={$row ['county_id']}\">{$row['county_name']}</a></div>";throws the error "Undefined index: county_id "county_id is the primary key of the county table. Do you know why this could be?KurtKurt
Link to comment
Share on other sites

Try to do the same from outside of quotes. I've had a similar problem (though an error message was missing) and that's how I solved it:

echo '<span><a href="Testing.php?county=' . $row['county_id'] . '">' . $row['county_name'] . '</a></div>';

Also, do var_dump($row) so see what is defined.

Link to comment
Share on other sites

Try to do the same from outside of quotes. I've had a similar problem (though an error message was missing) and that's how I solved it:
echo '<span><a href="Testing.php?county=' . $row['county_id'] . '">' . $row['county_name'] . '</a></div>';

Also, do var_dump($row) so see what is defined.

Did as you said, but still same line and error message. Where would I put var_dump($row)? Have never used it before and still not so good with server side scripting...Kurt
Link to comment
Share on other sites

Did as you said, but still same line and error message. Where would I put var_dump($row)? Have never used it before and still not so good with server side scripting...Kurt
Right before the "echo" statement. What var_dump() does is to show you the contents of the variable you pass it, and its type. It's particularly useful with arrays, as in brackets you see the number of elements, each array key, and its value.
Link to comment
Share on other sites

Right before the "echo" statement. What var_dump() does is to show you the contents of the variable you pass it, and its type. It's particularly useful with arrays, as in brackets you see the number of elements, each array key, and its value.
Had to edit my post as I was not clear in what I meant.The dump showsarray(1) { ["county_name"]=> string(12) "Bedfordshire" }for first county_name and lists all other counties in same way.It always shows the dump line first, then the long error message and after that actually half of the the link. I missed them first in between all the error messages. Each link shows "testing.php?county=" , but the rest is missing. Guess the mistake somehow lies in that. What do you think?Kurt
Link to comment
Share on other sites

I solved the problem now, simply forgot to select county_id in my select clause. PHP had no access to county_id as it was neither passed through url nor returned from database query. But how could I adjust the code to have echo '<div>All in whereever</div>'; replaced with the name of the selected region plus the number of listed companies in brackets? The region is passed through the url, but the number of companies is a matter of various considerations. There is a table to associate a company with the relevant counties and then I need to check through all counties that are in the region passed in the url (is this the right approach?). The revised code so far:

require_once ('mysql_connect.php'); // connect to database// county displayif (isset($_GET['region'])){	$region = (int)$_GET['region'];// Scotland	if ($region == 1) {	$counties = "SELECT county_id,county_name FROM county WHERE region_id = 1";	} else {// other counties	$counties = "SELECT county_id,county_name FROM county WHERE region_id = 2";	}$result = mysqli_query ($dbc, $counties);// display all counties in selected regionecho '<div>All in whereever</div>';while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)){echo '<span><a href="Testing2.php?county=' . $row['county_id'] . '">' . $row['county_name'] . '</a></span>';} //end of while loop

I would appreciate if you could get me in the right direction...Kurt

Link to comment
Share on other sites

How about simply

"SELECT * FROM county WHERE region_id = $region";

?

Link to comment
Share on other sites

How about simply
"SELECT * FROM county WHERE region_id = $region";

?

Sorry, mate. Again, I was not clear enough (my English is really driving me round the bend):I meant the count of all companies, which are listed in one of the counties that sit in region. There is an association table for company - county and there is a foreign key called region_id in county table, which is primary key in region table.Also, realise now that the value of the parameter passed in url is the number of region, not the name. Would I best get this from database?Kurt
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...