Discussion:
connection mySQL
iccsi
2014-05-09 21:37:41 UTC
Permalink
I have following code to connect MySQL server.
I got Connected to database message, but I got Could not run query : No
database selected error message when run mysql_query.
I check that the database name is correct, I use root account which should
have the rights to do select.
I can access my db using phpMyAdmin, so my PHP and MySQL server both
running.

It seems that it connected to MySQL, but for some reason it does not connect
to the database.

Are there any configuration need to check for the connection?
Your help and information is great appreciated,


$hostname = "localhost";
$username = "MyUser";
$password = "mypassword";

try {
$dbh = new PDO("mysql:host=localhost;dbname=mydb;charset=utf8",
$username, $password);
echo "Connected to database";
}
catch(PDOException $e)
{
echo $e->getMessage();
}


$result = mysql_query("SELECT invid, invdate, note, amount FROM invheader");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-05-09 21:48:46 UTC
Permalink
Post by iccsi
I have following code to connect MySQL server.
I got Connected to database message, but I got Could not run query : No
database selected error message when run mysql_query.
I check that the database name is correct, I use root account which should
have the rights to do select.
I can access my db using phpMyAdmin, so my PHP and MySQL server both
running.
It seems that it connected to MySQL, but for some reason it does not
connect to the database.
Are there any configuration need to check for the connection?
Your help and information is great appreciated,
$hostname = "localhost";
$username = "MyUser";
$password = "mypassword";
try {
$dbh = new PDO("mysql:host=localhost;dbname=mydb;charset=utf8",
$username, $password);
echo "Connected to database";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$result = mysql_query("SELECT invid, invdate, note, amount FROM invheader");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
You are using PDO to connect and mysql_* functions to execute the queries.
That is not possible. You either stick with PDO prepared statements or use
mysql_* functions. Here is a tutorial on how you can use PDO functions:

http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
Jim Giner
2014-05-09 21:57:24 UTC
Permalink
Post by iccsi
I have following code to connect MySQL server.
I got Connected to database message, but I got Could not run query : No
database selected error message when run mysql_query.
I check that the database name is correct, I use root account which
should have the rights to do select.
I can access my db using phpMyAdmin, so my PHP and MySQL server both
running.
It seems that it connected to MySQL, but for some reason it does not
connect to the database.
Are there any configuration need to check for the connection?
Your help and information is great appreciated,
$hostname = "localhost";
$username = "MyUser";
$password = "mypassword";
try {
$dbh = new PDO("mysql:host=localhost;dbname=mydb;charset=utf8",
$username, $password);
echo "Connected to database";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$result = mysql_query("SELECT invid, invdate, note, amount FROM invheader");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
You make a PDO connection but then you are trying to use mysql_*
functions to access it - for which you DON'T have a connection. IMHO -
stick with pdo as in
$q = "select invid, invdate, note, amount from invheader";
$result = $dbh->query($q);
if (!$result)
{
echo "Could not run query - error msg is: ";
$pdo_errinfo = $dbh->ErrorInfo();
echo $pdo_errinfo[2];
exit();
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
iccsi
2014-05-09 22:14:25 UTC
Permalink
Thanks a million for the information and help,

Regards,


Iccsi,
Post by iccsi
I have following code to connect MySQL server.
I got Connected to database message, but I got Could not run query : No
database selected error message when run mysql_query.
I check that the database name is correct, I use root account which
should have the rights to do select.
I can access my db using phpMyAdmin, so my PHP and MySQL server both
running.
It seems that it connected to MySQL, but for some reason it does not
connect to the database.
Are there any configuration need to check for the connection?
Your help and information is great appreciated,
$hostname = "localhost";
$username = "MyUser";
$password = "mypassword";
try {
$dbh = new PDO("mysql:host=localhost;dbname=mydb;charset=utf8",
$username, $password);
echo "Connected to database";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$result = mysql_query("SELECT invid, invdate, note, amount FROM invheader");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
You make a PDO connection but then you are trying to use mysql_*
functions to access it - for which you DON'T have a connection. IMHO -
stick with pdo as in
$q = "select invid, invdate, note, amount from invheader";
$result = $dbh->query($q);
if (!$result)
{
echo "Could not run query - error msg is: ";
$pdo_errinfo = $dbh->ErrorInfo();
echo $pdo_errinfo[2];
exit();
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...