Php cannot connect to mysql. how can this problem be solved?

Source: Internet
Author: User
Php cannot connect to mysql. I am a newbie. I connected to the test table create & nbsp; table & nbsp; test (id & nbsp; int, & nbsp; name & nbsp; in the test2 database; varchar (255) and then write code in zendsudio (the code is written in the book) & nbsp ;:& lt ;? Php $ connmysqli_init (); if (! $ Co php failed to connect to mysql
I am a newbie, connecting to a test table in the test2 database
Create table test
(Id int,
Name varchar (255)
)


Then write the code in zendsudio (the code is written in the book ):

$ Conn = mysqli_init ();
If (! $ Conn)
{
Echo "mysqli_init error ";
Exit (0 );
}
$ Ret = mysqli_real_connect ($ conn, "localhost", "root", "840803 ",
"Test2", 3306, "test", MYSQLI_CLIENT_FOUND_ROWS );
If ($ ret)
{
Echo "mysqli_real_connect error! ";
Exit (0 );
}
$ SQL = 'Insert into test values (1, "tom ")';
$ Ret = mysqli_query ($ conn, $ SQL );
If (! $ Ret)
{
Echo "mysqli_query error! ";
Exit (0 );
}
$ SQL = 'select * from test ';
$ Ret = mysqli_query ($ conn, $ SQL );
While ($ row = mysqli_fetch_array ($ ret, MYSQLI_ASSOC ))! = NULL)
{
Echo "id:". $ row ['id']."
";
Echo "name". $ row ['name']."
";
}
?>

After running:
Mysqli_real_connect error!

What's wrong with this? could you please give me some advice? urgent, online!

------ Solution --------------------
If ($ ret)
{
Echo "mysqli_real_connect error! ";
Exit (0 );
}

After the connection is successful, it does not enter here. Where does it enter? Yes if (! $ Ret?
------ Solution --------------------
if($ret)
{
echo "mysqli_real_connect error!";
exit(0);
}


An error is reported if $ ret does not exist.

If (! $ Ret)
{
Echo "mysqli_real_connect error! ";
Exit (0 );
}

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.