How do you see inside a mysqli_query object to trouble shoot? - php

I've come into this problem a few times and it's a pain. Right now the line if(mysqli_num_rows($result) != 1) gives the error mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given and $result is the result of mysqli_query(). I can't echo $result and I tried the following code to find out what's inside $result with no success
if (!mysqli_query($link, "SET #a:='this will not work'"))
printf("Error: %s\n", mysqli_error($link));
while ($row = mysqli_fetch_row($result))
printf("%s\n", $row[0]);
I'm trying to trouble shoot but it's like I come to a dead end because I can't see inside the result of mysqli_query().
How can I find the source of the problem? I'm guessing it's bad SQL syntax but I need more details.
Here is the PHP code where $result is defined
$result = mysqli_query($link, 'SELECT `password`
FROM `ajax_login`
WHERE userid = \''.$userName.'\' LIMIT 1');

Are you checking the error code/message? Something like:
if(mysqli_errno($link)){
die(mysqli_error($link));
}
(of course, you wouldn't use die in production)

Related

Using mysqli_query with mysql_query arguments

I'm migrating from PHP 5 to PHP 7, and I made a grep to get all mysql_query to change them to mysqli_query. The problem is, apparently the parametres changed using the procedural style.
mysql_query($query, $link_identifier)
mysqli_query($link_identifier, $query)
Even tho using the object oriented style they're still the same parametres.
Question is, will it work if I leave the parameters of mysqli_query as $query, $link thinking that the function is smart enough to detect which is which or I need to change them all to match the right parameters ?
Order of arguments isn't interchangeable.
Php.net mysqli_query.
Php.net mysql_query.
I also tested it.mysql_query
$link = mysql_connect("localhost","login","pass");
$db_selected = mysql_select_db('db', $link);
$sql = "SELECT * FROM table LIMIT 5";
$result = mysql_query($sql, $link);
while($row = mysql_fetch_array($result)) {
echo $row['col_name'];
}
mysql_close($link);
mysqli_query with with wrong order of arguments will produce: Warning: mysqli_query() expects parameter 1 to be mysqli, string given code here:
$link = mysqli_connect("localhost", "login", "pass", "db");
//arguments in wrong order
if ($result = mysqli_query("SELECT * FROM table LIMIT 5", $link)) {
while($row = mysqli_fetch_array($result)) {
echo $row['col_name'];
}
}
//produce
//Warning: mysqli_query() expects parameter 1 to be mysqli, string given in
mysqli_close($link);

PHP Select Login Error

I'm just a newbie in PHP and I need to create a php Login form but It have a syntax error saying Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\login.php on line 18
maybe you can help me on this tnx!
<?php
$con=mysqli_connect("localhost","root","","reservation");
if (mysqli_connect_errno())
{
echo "Failed to connect to Database: " . mysqli_connect_error();
}
else
{
}
if ($_POST["SUBMIT"]="SUBMIT")
{
$username = $_POST['username'];
$password =$_POST['password'];
$sql ="SELECT * FROM usersaccount where='$username' and password ='$password'";
$result=mysqli_query($con,$sql);
$count=mysqli_num_rows($result);
if($count==1)
{
echo "success";
}
//mysqli_close($con);
}
?>
Your SELECT statement is wrong ... should be
SELECT * FROM `usersaccount` WHERE `username` ='$username' and `password` ='$password'";
---------- // Here.. Added the column name
You are not providing the column name for username field. That was your issue. Since you are matching results with condition you need a WHERE clause too !
Disclaimer : Stop using mysql_* functions as they are deprecated. Switch to MySQLi or PDO instead.
It means your query (i.e., $sql) is broken. You are comparing a table to a string, when you should be using a WHERE clause. Also, password is a reserved word; escape it, like this:
$sql ="SELECT * FROM usersaccount WHERE `username` ='$username' and `password` ='$password'";
Also, you are wide open to SQL injection; you need prepared statements.
According to documentation you have to do this:
$con=mysqli_connect("localhost","root","","reservation");
//...
$query = "SELECT * FROM usersaccount ='$username' and password ='$password'";
if ($stmt = mysqli_prepare($con, $query)) {
mysqli_stmt_execute($stmt);/* execute query */
mysqli_stmt_store_result($stmt);/* store result */
printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));
mysqli_stmt_close($stmt);/* close statement */
}
Since the error message is reporting that its first parameter is a boolean, you should take a closer look at that first parameter.
$result=mysqli_query($con,$sql);
Read the documentation : If it works, mysqli_query() returns a mysqli_result() or FALSE (a boolean) if it doesn't work. So, your query didn't work and it returned a boolean.
You should probably check the outcome of executing your query and, if it fails, you should show the error message generated:
if ( $result = mysqli_query($con, $sql) === FALSE ) {
printf("Invalid query: %s\nWhole query: %s\n", mysqli_error, $sql);
exit();
}

MySQLi Query will not execute

I am playing around with this trying to pull some articles from a database.
Here is the code i have so far: ($host etc are defined in config)
include ("admin/config.php");
$mysqli = mysqli_connect($host, $user , $pass , $database);
$query = "SELECT * FROM articles ORDER BY date LIMIT 5";
$result = mysqli_query($mysqli,$query);
while($row = mysqli_fetch_array($result))
{
Some code to display results
}
This is the error that gets generated but i just cant see what i am doing wrong. Any help will be much appreciated.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /website....
there is error in your query.
to get it always run all your queries this way
$result = mysqli_query($mysqli,$query) or trigger_error($mysqli->error."[$query]");

mysql_num_rows in the error log

I am encountering an error. My log is as follows:
PHP Warning: mysql_num_rows(): supplied argument is not
a valid MySQL result resource in
/home/domain/public_html/index.php on line 96
My code is as followsL
line 96:
mysql_num_rows(mysql_query("SELECT * FROM `table` WHERE
UNIX_TIMESTAMP(`date`)>$user_last_visit"));
What could cause this error?
You put two functions into each other directly:
mysql_num_rows(mysql_query(...));
Technically this is possible to do (function 1 returns something that becomes the parameter of function 2), however, in case function 1 returns something that function 2 can not deal with, you get errors.
That happens in your case. Instead store the return value into a variable and do error handling your own:
$result = mysql_query(...);
if (!$result) {
throw new Exception(sprintf('Database query failed: %s', mysql_error()));
}
$num_rows = mysql_num_rows($result);
Proper error handling is crucial for solid programming, so take care about return values. When in doubt double-check for the function in question with the PHP manual which is really a great resource. See mysql_query.
I would try rearranging the code
$sql = "SELECT * FROM `table` WHERE UNIX_TIMESTAMP(`date`) > $user_last_visit";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($result);
I can't tell from your code but you are creating a connection to the mysql server?
If you only want to count the rows, use the COUNT(*) keyword in the query; otherwise, the data base will prepare the whole resulting rows for output although you don't need them.
SELECT COUNT(*)
FROM `table`
WHERE UNIX_TIMESTAMP(`date`) > $user_last_visit
Then, simple execute the query:
$result = mysql_query($query);
list($count) = mysql_fetch_array($result);
Anyway, your query throws an error; there should be another warning showing the error. You can use mysql_error() to find out the error otherwise.
According to http://php.net/manual/en/function.mysql-query.php mysql_query returns "For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error."
So try doing
$query = "SELECT * FROM `table` WHERE UNIX_TIMESTAMP(`date`)>$user_last_visit"
$resource = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
mysql_num_rows($resource);
And then see what happens.

mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I' really stuck on this , I'm gettiing this error:
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in "filename"
Here is the code:
$sql = "SELECT * FROM $tbl_name WHERE....
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);
The wierd thing is that I've used the exact same code before and it worked fine
Any ideas??
That means the query failed. Usually it's a SQL syntax error. To find out, just insert this right before the _fetch_assoc line:
print mysql_error();
To prevent the error message, structure your code like this to check the $result beforehand:
$sql = "SELECT * FROM $tbl_name WHERE....";
if ($result = mysql_query($sql)) {
$row = mysql_fetch_assoc($result);
}
else print mysql_error();
Always run all your queries this way
$sql = "SELECT * FROM $tbl_name WHERE....";
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
$row = mysql_fetch_assoc($result);
And you will be notified of the cause of error.
But never print or let die() output any errors, as it's security flaw.
This error usually occurs because no data is returned from the query. Make sure the data is being returned by going into something like PHPMyAdmin and making sure the query returns some rows. You should also add the
or die(mysql_error());
At the end of your query.

Categories