Mysqli data query is always null

Source: Internet
Author: User
The code for querying data from mysqli is always null is as follows. the code is tested many times and the cause is unknown. the SQL statement contains the query result in Navicat. let's take a 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 site projects ";
$ Query = "select * from picture where type = 'homepage on-site 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)

It's strange that you don't write the query results.

What is displayed on the page after running?

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

He did not perform the query,

$ Rs = $ DB-> query ($ query );
What is this?

He did not perform the query,

Try mysqli_connect

It's strange that you don't write the query results.


$ The rs value is always null. I suspect it is an environment problem.

What is displayed on the page after running?


Num rows: 0 query failed

What is displayed on the page after running?


An old-fashioned program, written for 10 years, is not maintained by the server for several years.

You are careless (I am not careful)
You can use mysqli to connect to a database and perform queries, but you cannot use the mysqli function to check (mysqli_num_rows!

He did not perform the query,


$ DB-> query ($ query );
This statement has been queried. the mysqli method is used.

You are careless (I am not careful)
You can use mysqli to connect to a database and perform queries, but you cannot use the mysqli function to check (mysqli_num_rows!



If (mysqli_num_rows ($ rs) = 0 ){
Echo "num rows: 0 ";
}
This sentence is added in my test. it does not matter if I delete this sentence. $ rs is still null, that is, $ rs = $ DB-> query ($ query) the result is always null. I don't know why.

$ Rs = $ DB-> query ($ query );
If (! $ Rs ){
Printf ("Errormessage: % s \ n", $ mysqli-> error );
}

See what's wrong.

$ Query = "select * from picture where type = 'homepage on-site works' order by chage_date desc ";
If it is the same query statement, it is likely to be the cause of the character set.
Is your php file encoded with 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 = 'homepage on-site works' order by chage_date desc ";
$ Rs = $ DB-> query ($ query );

I am a beginner, but I have seen a lot of problems (I am wrong, don't scold me ):

$ Db = new mysqli ("127.0.0.1", "root", "admin159753", "51dm", "utf8 ");

More parameters are provided by mysqli! Database connection problems!

If (! $ Db ){
Echo "Cloud not connect to database ";
} Else {
Return $ db;
}

If you use $ db as the judgment condition, there is also a problem. no matter the mysqlil connection fails or succeeds, an object will be returned to you! $ Db is always true! If you don't believe it, add echo "OK" to return $ db ";

And should the mysqli class use $ db-> errno to determine whether the database connection is successful?

Therefore, the first step you connect to the database is problematic. of course, you cannot find anything!

$ Query = "select * from picture where type = 'homepage on-site works' order by chage_date desc ";
If it is the same query statement, it is likely to be the cause of the character set.
Is your php file encoded with UTF-8?


I also changed it to UTF-8, but it still didn't work. After I restarted the server, Kingsoft guard found numerous viruses and was not taken care of for years.

If ($ rs ){
Echo "rs ";
} Else {
Echo "query failed". $ db-> error;
}



After reading the php version, it was actually 5.2.13 ...... So old



It seems that you have to be careful. the source file parameters are not paired.

Thank you!

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.