PHP MySQL PDO connects to the database, cannot manipulate the database, and cannot read data. Kneel and beg for answers.

Source: Internet
Author: User
Tags php mysql
PDO MySQL


try{
$votePDO = new PDO ("Mysql:host=127.0.0.1;dbname=legou", ' Root ', ' 2sinilei. ', array (pdo::attr_persistent = true));
$votePDO = new PDO ();
$pdoconn->setattribute (pdo::attr_persistent,true);
echo "Success
";
}
catch (Pdoexception $e) {
echo $e->getmessage ();
}

$result = $votePDO->query ("SELECT * from Uz_types");
Var_dump ($result);
$votePDO->query ("INSERT into uz_types (TypeName) VALUES (' Ah ah ')");
echo "count=". $count. "
";
$result->setfetchmode (PDO::FETCH_ASSOC);
Print_r ($result);
if (empty ($result))
{
echo "NULL";

}else
{
echo "has". Count ($result). " Datas ";
}
echo count ($result);
foreach ($result as $k = $v)
{
echo $v [' TypeName '];

};
?>



Here is the result of the page output

Success
BOOL (FALSE) count=
Null1

//
PHP.ini has been configured well.
There is no link on the error note, but why is this output?
Solve the solution Ah ~


Reply to discussion (solution)

Var_dump ($result);
$arr = $votePDO->errorinfo ();
Print_r ($arr); Post this result and see

Array ([0] = S1000 [1] = 0 [2] = Unknown Initial character Set index ' "received from server. Initial client Character set can is forced via the ' characterencoding ' property. ) count=

Wrong coding??

Print out success
Represents $votePDO = new PDO ("Mysql:host=127.0.0.1;dbname=lego ... Perform the correct

Print out bool (false)
Represents $result = $votePDO->query ("SELECT * from Uz_types"); Execution failed

Print out count=
Normal
echo "count=". $count. "
"; The $count is not assigned a value

Since $result is false,
Then if (empty ($result)) is established
It's normal to print out null.

Print out success
Represents $votePDO = new PDO ("Mysql:host=127.0.0.1;dbname=lego ... Perform the correct

Print out bool (false)
Represents $result = $votePDO->query ("SELECT * from Uz_types"); Execution failed

Print out count=
Normal
echo "count=". $count. "
"; The $count is not assigned a value

Since $result is false,
Then if (empty ($result)) is established
It's normal to print out null.

Why does the query fail to solve the problem?

Your library, how do I know why I failed
Print_r ($votePDO->errorinfo ());
Look at the error message

Your library, how do I know why I failed
Print_r ($votePDO->errorinfo ());
Look at the error message

I posted it on the 2 floor, trouble to see.

Var_dump ($result);
$arr = $votePDO->errorinfo ();
Print_r ($arr); Post this result and see

Array ([0] = S1000 [1] = 0 [2] = Unknown Initial character Set index ' "received from server. Initial client Character set can is forced via the ' characterencoding ' property. ) count=

$votePDO = new PDO ("Mysql:host=127.0.0.1;dbname=legou", ' Root ', ' 2sinilei. ', array (pdo::attr_persistent = True,pdo :: Mysql_attr_init_command = ' SET NAMES \ ' utf8\ '));

Let's try this.

$votePDO = new PDO ("Mysql:host=127.0.0.1;dbname=legou", ' Root ', ' 2sinilei. ', array (pdo::attr_persistent = True,pdo :: Mysql_attr_init_command = ' SET NAMES \ ' utf8\ '));

Let's try this.

Can be read out, is the coding problem code is not unified reading data. Thank you for the bamboo.

  • 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.