Mysqli query data is always NULL

Source: Internet
Author: User
The code is as follows, test many times, always do not know the reason, SQL statement in Navicat inside have query result, everybody help me to look:
function Db_connect () {
$db =new mysqli ("127.0.0.1", "root", "admin159753", "51DM", "UTF8");
$db->set_charset (' UTF8 ');
if (! $db) {
echo "Cloud not connect to database";
} else {
return $db;
}
}

$DB =db_connect ();
$type = "Home station works";
$query = "SELECT * from picture where Type= ' home station Works ' ORDER by chage_date DESC";
$rs = $DB->query ($query);

if (Mysqli_num_rows ($rs) ==0) {
echo "Num rows:0";
}

if ($rs) {
echo "RS";
} else {
echo "Query failed";
}
?>


Reply to discussion (solution)

You didn't write the results.

What does the post-run page show?

while ($row =mysqli_fetch_assoc ($rs)) {
Var_dump ($row);
}

He didn't execute the query,

$rs = $DB->query ($query);
What is it?

He didn't execute the query,

Try using Mysqli_connect

You didn't write the results.


The value of $rs is always null, and I suspect it's an environmental problem.

What does the post-run page show?


Num rows:0 Query failed

What does the post-run page show?


An antique program, written in 10, the server was not maintained for several years

You're careless (I'm careless, I didn't notice)
You use the Mysqli class to connect the database and query, but in the Mysqli function to check (mysqli_num_rows) Of course it is not!

He didn't execute the query,


$DB->query ($query);
This sentence has been queried, using the mysqli way

You're careless (I'm careless, I didn't notice)
You use the Mysqli class to connect the database and query, but in the Mysqli function to check (mysqli_num_rows) Of course it is not!



if (Mysqli_num_rows ($rs) ==0) {
echo "Num rows:0";
}
This sentence is I test casually add, remove this sentence is OK, $rs is still null, that is $rs = $DB->query ($query) The result is always null, do not know why

$rs = $DB->query ($query);
if (! $rs) {
printf ("ErrorMessage:%s\n", $mysqli->error);
}

See what's wrong.

$query = "SELECT * from picture where Type= ' home station Works ' ORDER by chage_date DESC";
If it is the same query statement, it is most likely the reason for the character set.
Is your PHP file encoded in Utf-8?

if ($rs) {
echo "RS";
} else {
echo "Query failed". $db->error;
}

$db =new mysqli ("127.0.0.1", "root", "admin159753", "51DM", "UTF8");

$query = "SELECT * from picture where Type= ' home station Works ' ORDER by chage_date DESC";
$rs = $DB->query ($query);

I am a beginner, but I see a lot of problems (I said wrong not to scold me AH):

$db =new mysqli ("127.0.0.1", "root", "admin159753", "51DM", "UTF8");

Mysqli give more Parameters! Database connection will be a problem!

if (! $db) {
echo "Cloud not connect to database";
} else {
return $db;
}

Use $DB to judge the condition, also have the question, Mysqlil connection regardless of defeat or succeeds, will return an object to you! $DB is always true! If you do not believe, you return $db, add an echo "OK";

And should the Mysqli class use the $db->errno to determine if the database connection was successful?

So, your first step to connect to the database there is a problem, of course, do not find things!

$query = "SELECT * from picture where Type= ' home station Works ' ORDER by chage_date DESC";
If it is the same query statement, it is most likely the reason for the character set.
Is your PHP file encoded in Utf-8?


I also deliberately changed to utf-8, or not, after restarting the server, Jinshan Guardian found the virus countless, for several years no one to take care of

if ($rs) {
echo "RS";
} else {
echo "Query failed". $db->error;
}



Read the version of PHP, incredibly is 5.2.13 ... So old



It seems to be careful, the source file parameters are not paired, positive solution

Thank you

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.