the problem was that in the config file was mysql so i change it to mysqli
but now i cant see the result in the select in the html area
this is the php script
<?php
require 'config.php';
$query = "SELECT cat_id, category FROM categories LIMIT 1";
$result = mysqli_query($con,$query);
if(!$result){
echo 'Query failed : '.mysqli_error();
exit(0);
}
$row = mysqli_fetch_assoc($result);
// mysql_fetch_assoc was the problem print_r($row);
mysql_close($con);
?>
this is the html script
<select name="cat_id"> </select>
this is thr error
Warning: mysqli_query() expects parameter 1 to be mysqli, resource
given in /home/content/60/10533160/html/cms/in.php on line 5
Warning: mysqli_error() expects exactly 1 parameter, 0 given in
/home/content/60/10533160/html/cms/in.php on line 7 Query failed :
it looks like you used mysql not mysqli for your connection.
maybe you should change that, as it says mysqli_query() expects parameter 1 to be mysqli
Your first error is caused because mysqli_fetch_assoc() needs two parameters, a database name and parameter two to be the array you wish to fetch.
Your code should be (presuming $con is the database connection):
mysqli_fetch_assoc($con,$result);
Your second error is simply caused because mysqli_error() expects 1 parameter: The database connection, but you connected with mysql.*. Don't mix the two libraries. mysqli has many improvements over the original mysql extension, so it is recommended that you use mysqli.
Make sure to do some research on Google before asking a question here.
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
This example is useful for you...
Related
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 4 years ago.
I've spent the past 2 hours trying to solve this one error. I am a complete rookie so I dont know what's going on. Here's the code, please help:
<?php
header('Access-Control-Allow-Origin: *');
$host="localhost"; // Host name
$username="id11111_ab"; // Mysql username
$password="*****"; // Mysql password
$db_name="id11111_cd"; // Database name
$tbl_name="ef"; // Table name
// Connect to server and select database.
$link = mysqli_connect($host, $username, $password, $db_name);
// Retrieve data from database
$sql = "SELECT * FROM scores ORDER BY score DESC LIMIT 10";
$result = mysqli_query($link,$sql);
// Start looping rows in mysql database.
while($rows=mysqli_fetch_array($result)){
echo $rows['name'] . "|" . $rows['score'] . "|";
// close while loop
}
?>
mysqli_query() returns false if it fails. Subsequently, the mysqli_fetch_array() function is being passed this false boolean value, on which it can't operate. You'd be wise to check the value that mysqli_query() returns isn't false prior to attempting to retrieve the resource. E.g.:
$result = mysqli_query($link,$sql);
if (!$result) {
die('Query failed');
}
It seems like the mysql connection is not established properly.
Check for errors using this:
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 8 years ago.
I looked at the other answers to problems similar to mine but cant seem to solve this.
Here is the code.
$connection = mysql_connect("localhost","root","starwars");
$conn = mysql_select_db("project", $connection);
// This code assumes $itemID is set to that of
// the item that was just rated.
// Get all of the user's rating pairs
$sql = "SELECT DISTINCT r.itemID, r2.ratingValue - r.ratingValue
as rating_difference
FROM rating r, rating r2
WHERE r.userID=$userID AND
r2.itemID=$itemID AND
r2.userID=$userID;";
$db_result = mysql_query($sql, $conn);
echo "The result is {$db_result}";
$num_rows = mysql_num_rows($db_result)or die('Cannot Execute:'. mysql_error());
The error being displayed is:
Warning: mysql_query() expects parameter 2 to be resource, boolean
given in C:\xampp\htdocs\recomender\ratingfiles\class.rating.php on
line 177
Line 177 is
$db_result = mysql_query($sql, $conn);
And if I echo $conn it gives the value of "1" which I thought was equal to true, thus boolean, any ideas?
Pass $connection as the second parameter, not $conn.
You assign the result of mysql_select_db to $conn, and mysql_select_db returns true or false, not a connection resource.
This probably means $conn is false, meaning it didnt get setup correctly. You may want to check how you have set that up and ensure the database connection details are correct and the server this is running on can access the database server.
Take a look at the return values on this page:
http://php.net/manual/en/function.mysql-connect.php
change first 2 lines to
$conn = mysql_connect("localhost","root","starwars");
mysql_select_db("project", $conn);
or, better, just refrain from using connection variable at all
mysql_connect("localhost","root","starwars");
mysql_select_db("project");
...
$db_result = mysql_query($sql);
$db_result = mysql_query($sql, $connection);
Second argument to mysql_connect must have
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.
http://php.net/manual/en/function.mysql-query.php
I've got a pretty standard call to a MySQL database and for some reason I can't get the code to work. Here's what I have:
$mysqli = mysqli_connect("localhost","username","password");
if (!$mysqli)
{
die('Could not connect: ' . mysqli_error($mysqli));
}
session_start();
$sql = "SELECT * FROM jobs ORDER BY id DESC";
$result = $mysqli->query($sql);
$num_rows = mysqli_num_rows($result);
Now, first, I know that it is connecting properly because I'm not getting the die method plus I added an else conditional in there previously and it checked out. Then the page displays but I get the errors:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in blablabla/index.php on line 11
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in blablabla/index.php on line 12
I've double-checked my database and there is a table called jobs with a row of "id" (it's the primary row). The thing that confuses me is this is code that I literally copied and pasted from another site I built and for some reason the code doesn't work on this one (I obviously copy and pasted it and then just changed the table name and rows accordingly).
I saw the error and tried:
$num_rows = $mysqli_result->num_rows;
$row_array = $mysqli_result->fetch_array;
and that fixed the errors but resulted in no data being passed (because obviously $mysqli_result has no value). I don't know why the error is calling for that (is it a difference in version of MySQL or PHP from the other site)?
Can someone help me track down the problem? Thanks so much. Sorry if it's something super simple that I'm overlooking, I've been at it for a while.
You didn't selected the database
$mysqli = mysqli_connect("localhost","username","password","database");
The problem is you haven't selected the database.
use this code for select database.
$mysqli = mysqli_connect("localhost","username","password");
mysqli_select_db("db_name",$mysqli);
You have to select database in order to fire mysql queries otherwise it will give you error.
I believe that schtever is correct, I do not think you are selecting the database. It isn't in the code snip and if you search online you see other people with similar errors and it was because the database wasn't selected. Please let us know if you selected a database before anything else is checked. Thanks.
Try this:
session_start();
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
$mysqli->close();
}
$query ="SELECT * FROM jobs ORDER BY id DESC";
$values = $mysqli->query($query);
if($values->num_rows != 0)
{
while($row = $values->fetch_assoc())
{
//your results echo here
}
}
else
{
//if no results say so here
}
See this manual for mysqli_connect you can select the database right in this function.
This question already has answers here:
mysqli::query(): Couldn't fetch mysqli
(4 answers)
Closed 1 year ago.
I was told to stop using MySQL and now learning MySQLi. However I ran into a problem using fetch array and don't know what I did wrong.
//Connect to db
include "mysqli_connect.php";
// Construct our join query
$sql = "SELECT userID, username, lastlogin FROM users";
//Crate results
$result = mysqli_query($link, $sql);
// Print out the contents of each row into a table
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
// Free result set
mysqli_free_result($result);
// Close connection
mysqli_close($link);
Messages I see:
[Connection Successful] Host info: db413417616.db.1and1.com via
TCP/IP
Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch
mysqli in /homepages/9/d413002686/htdocs/maintenance/testsession.php
on line 9
Warning: mysqli_fetch_array() expects parameter 1 to be
mysqli_result, null given in
/homepages/9/d413002686/htdocs/maintenance/testsession.php on line 12
Warning: mysqli_free_result() expects parameter 1 to be
mysqli_result, null given in
/homepages/9/d413002686/htdocs/maintenance/testsession.php on line 15
Warning: mysqli_close() [function.mysqli-close]: Couldn't fetch
mysqli in /homepages/9/d413002686/htdocs/maintenance/testsession.php
on line 18 ()
I used this to for mysqli_connect.php: http://www.php.net/manual/en/mysqli.construct.php
<?php
// $link (host, username, password, database)
$link = mysqli_connect('host', 'username', 'password', 'database');
//If connection is successful, otherwise show error message.
if (!$link) {
die('[Connect Fail] Error: (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
// Echo success message
echo '[Connection Successful] Host info: ' . mysqli_get_host_info($link) . "\n";
// Close the link
mysqli_close($link);
?>
This line in your include file should be removed:
// Close the link
mysqli_close($link);
You're closing $link, a.k.a. your connection to the database, and then trying to use it in your query. You only want to close it when you're all done with it.
This question already has answers here:
How to change mysql to mysqli?
(12 answers)
Closed 1 year ago.
I need some help in converting my script from using mysql to mysqli I have been trying for ever to do this and keep getting errors I have read articles on how to do this and still can get it to work.
Here is the original working script below.
<?php
require_once ('./mysqli_connect.php'); // Connect to the db.
if (isset($_POST['submitted'])) {
if ($dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) { // Make the connnection.
if (!mysql_select_db (DB_NAME)) { // If it can't select the database.
// Handle the error.
trigger_error("Could not select the database!\n<br />MySQL Error: " . mysql_error());
exit();
} // End of mysql_select_db IF.
} else { // If it couldn't connect to MySQL.
// Print a message to the user, include the footer, and kill the script.
trigger_error("Could not connect to MySQL!\n<br />MySQL Error: " . mysql_error());
exit();
} // End of $dbc IF.
// grab the tag
$tag = mysql_real_escape_string($_POST['tag']);
// see if the tag already exists and potentially what the current count is
$query = "SELECT id, count, page FROM tags WHERE tag='$tag'";
$result = mysql_query($query);
//--if there is a row, that means the tag exists
if(mysql_num_rows($result))
{
//--pull out the tag ID and the current count and increment by one.
$tag_info = mysql_fetch_array($result);
$tag_info_id = $tag_info["id"];
$tag_info_count = $tag_info["count"] + 1;
//--update the table with the new count
$sql_update_cnt = "UPDATE tags SET count='$tag_info_count'
WHERE id='$tag_info_id'";
mysql_query($sql_update_cnt);
echo "$tag now with $tag_info_count instances";
}
else
{
// tag is not there, so insert a new instance
$query = "INSERT INTO tags (tag, count) VALUES ('$tag', 1)";
if (!mysql_query($query, $dbc))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}
mysql_close($dbc);
}
?>
Here is the errors I keep getting.
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given on line 13
Warning: mysqli_error() expects exactly 1 parameter, 0 given on line 16
If you're going to go through the hassle, I would really recommend you consider PDO instead.
It's all a matter of changing mysql to mysqli.
For example, you could call mysqli_query just like you do mysql_query here.
When you call these functions, you need to pass in the database reference as the first parameter.
See the mysqli function reference here
It's been a while since I've done a mysql->mysqli conversion (like 2 or 3 years), but IIRC there are some functions for which you also have to reverse the parameter order. Isn't that lovely?