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 1 year ago.
I'm running such a simple SQL connection and select but I am getting very odd errors whilst running queries.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Webmaster\run\forum_mc.php on line 23
Warning: mysqli_query(): Empty query in C:\xampp\htdocs\Webmaster\run\forum_mc.php on line 24
()
<?php
$sqli_host = 'localhost';
$sqli_dbname = 'forum_handling';
$sqli_username = 'root';
$sqli_password = ''; //null as defualt
$fhconnect = mysqli_connect($sqli_host, $sqli_username, $sqli_password, $sqli_dbname);
if (!$fhconnect) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
$query = "SELECT Forum Title FROM forum_categories ORDER";
$result = mysqli_query($fhconnect, $query);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
$data = mysqli_query($fhconnect,$row);
printf ("%s (%s)\n", $row[0], $row[1]);
mysqli_close($fhconnect);
your query should be like as follows:
$query = "SELECT Forum,Title FROM forum_categories ORDER BY Title";
chenge order by as you want.
check your field name Forum Title You can't put a blank space between Forum and Title , and also write order by fieldname
Related
This question already has answers here:
mysqli_query() expects parameter 1 to be mysqli, object given
(3 answers)
Closed 2 years ago.
I am trying to find a sum of a column in mysql table but I get an error as below.
<?php require_once("../../../../512/1.001/data/class.php");
$sql=mysqli_query($db,"select SUM(tbl_ccp_loans.payableamount) AS total"); $row = mysqli_fetch_assoc($sql);
$sum = $row['total'];
$sum;?>
I expect the Total sum is 'Number' but the output is
Warning: mysqli_query() expects parameter 1 to be mysqli, object given in /home/testing/public_html/orrf/bej/ccp-main2/_dashboard/data.php on line 223
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /home/testing/public_html/orrf/bej/ccp-main2/modules/_dashboard/data.php on line 224
Function mysqli_query() expects as first parameter object of mysqli class (returned by mysqli_connect()), but based on code in your comments, your $db variable is an object of different class. If this is a custom class, try to use appropriate methods from this class to execute a query and retrieve results.
Next is a basic example using mysqli_connect().
<?php
require_once("../../../../512/1.001/data/class.php");
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$db) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
$sql = mysqli_query($db, "select SUM(tbl_ccp_loans.payableamount) AS total");
if ($sql === false) {
echo 'Error. ' . mysqli_connect_errno() . PHP_EOL;
exit;
}
$row = mysqli_fetch_assoc($sql);
$sum = $row['total'];
echo $sum;
?>
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 3 years ago.
I have written a mysql_query and it is not working.
I want to know the problem via mysqli_error() however it gives the following error mysqli_error() expects parameter 1 to be mysqli
my mysql code is as follows:
$connect = mysqli_connect("localhost","root", "", "tomuman");
$query = mysqli_query($connect, "SELECT id, to FROM messages WHERE read='0'");
and mysqli_error as follows:
echo mysqli_error($query);
What could cause this problem?
You should not use $query as a parameter for mysqli_error()
use
echo mysqli_error($connect);
Just after trying to connect you can also check for specific connection errors with:
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
Add following code in your query to get the mysql error message.
or die (mysqli_error($connect))
as like below
$query = mysqli_query($connect, "SELECT id, to FROM messages WHERE read='0'") or die (mysqli_error($connect));
if ($query) {
echo "success";
}
else {
echo("Error description: " . mysqli_error($connect));
}
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 9 years ago.
struggling with my web design assignment. I've been following a tutorial to add in a search feature for my website, but I've been getting the following error:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /search.php on line 31
line 31 is (or was)
<pre>if(mysqli_num_rows($results) >= 1)</pre>
That was the original error. as per instructions in the comments, I've since revised the code:
<pre>
<?php
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyword']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter the name/brand of what you're looking for.";
exit();
}
//database connection info
$host = "localhost";
$db_name = "sookehhh_shopsy_db";
$username = "sookehhh_shopsy";
$password = "xxxx";
//connecting to server and creating link to database
$link = mysqli_connect($host, $username, $password, $db_name) or die('Could not connect: ' . mysqli_connect_error());
//MYSQL search statement
$query = "SELECT * FROM sookehhh_shopsy_db WHERE name LIKE '%" . mysqli_real_escape_string($link, $searchTerm) . "%'";
// original query$query = "SELECT * FROM sookehhh_shopsy_db WHERE name LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
//added suggestion below - not sure if correct place?
if (!$result) {
die(mysqli_error($link));
}
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "Product Name: " . $row['name'] . "<br />";
$output .= "Price: " . $row['price'] . "<br />";
}
echo $output;
}
else
echo "There was no matching record for that item " . $searchTerm;
?>
</pre>
made necessary changes and updated yet again -
now the only error message I'm getting here is "Table 'sookehhh_shopsy_db.sookehhh_shopsy_db' doesn't exist"
I'm assuming that I need to change the username, perhaps because it's too similar?
Anywho, thanks for your help so far, and I apologise for my complete ignorance.
I've been trying to teach myself, but unfortunately time is a luxury I just don't have at the moment.
The problem is your query returned false meaning there was an error in your query. After your query you could do the following:
if (!$result) {
die(mysqli_error($link));
}
Or you could combine it with your query:
$results = mysqli_query($link, $query) or die(mysqli_error($link));
That will print out your error.
Also... you need to sanitize your input. You can't just take user input and put that into a query. Try this:
$query = "SELECT * FROM shopsy_db WHERE name LIKE '%" . mysqli_real_escape_string($link, $searchTerm) . "%'";
In reply to: Table 'sookehhh_shopsy_db.sookehhh_shopsy_db' doesn't exist
Are you sure the table name is sookehhh_shopsy_db? maybe it's really like users or something.
This question already has answers here:
Warning: mysql_query(): 3 is not a valid MySQL-Link resource
(4 answers)
Closed 9 years ago.
ERROR:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/247QC/system/core.php on line 21
Invalid query: Whole query: SELECT * FROM users WHERE username='matt' AND password='5657572fc913e2d2a9548ba4f4'
From my knowledge I have not done anything wrong in my code but I thought I would ask, since the last time I used MySQL was 2 years ago, things might of changed.
I am wondering what is the error and how do i fix it, I am at the point after googling for the last hour and results saying it was a connection issue and after testing it was connecting to the server correctly (using the hostname not IP address)
The MySQL engine it is using is InnoDB and Collation latin1_swedish_ci
code
$r_hostname = "monitor";
$r_username = "QCSYSTEM";
$r_password = "123456";
$link = mysql_connect($r_hostname,$r_username,$r_password);
$db = mysql_select_db('QCSYSTEM', $link);
$Password = sha1($_POST['password']);
$username = $_POST['username'];
$query = sprintf("SELECT * FROM users WHERE username='%s' AND password='%s'",
mysql_real_escape_string($username),
mysql_real_escape_string($Password));
$result = mysql_query($query,$db);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
} else {
print "success";
}
The second argument to mysql_query should be a link identifier. You are using $db as the second argument, which is nothing but a boolean value. Try this..
$result = mysql_query($query,$link);
or just don't pass any second argument.
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.