I've created a database on az.pl I wanted to use old code which works perfectly on other website. I get this error message:
Could not connect to mysql
Here's my code:
$dbh = mysqli_connect("localhost","user","password", "db") or die ("could not connect to mysql");
I've also tried:
$c = mysql_connect("localhost", "user", "password");
mysql_select_db("db");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);
Both with the same result, the error message.
I've searched the web but I haven't found exact problem.
Consider following the docs when debugging your connection issue:
http://php.net/manual/en/function.mysqli-connect.php
<?php
$link = mysqli_connect("localhost", "user", "password", "db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
// this will tell you why the connection failed
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
I finally connected to the database. All I needed to do is to change isset to if in the following code:
if (!$_GET['pid']) {
$pageid = '1';
} else {
$pageid = ereg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}
// Query the body section for the proper page
$sqlCommand = "SELECT pagebody FROM pages WHERE id='$pageid' LIMIT 1";
$query = mysqli_query($link, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$body = $row["pagebody"];
}
I used MonkeyZeus code to connect to the database.Thank you MonkeyZeus.
Related
Host updated the environment to PHP 7. Had to update db.php and added the following:
<?php
include('administrator/configuration.php');
//echo DATABASE;
//mysql_select_db(DATABASE, mysql_connect(HOSTNAME, DBUSER, DBPASS));
$con = mysqli_connect(HOSTNAME, DBUSER, DBPASS, DATABASE) or die("Error message...");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!$con) {
"Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
printf ("Success: A proper connection to MySQL was made! The ???? database is great." . PHP_EOL);
printf("Host information: " . mysqli_get_host_info($con) . PHP_EOL);
echo "<p><hr />";
try {
//$query = sprintf("SELECT database() AS the_db");
$query = sprintf("SELECT COUNT(*) FROM albums");
/* return name of current default database */
if ($result = mysqli_query($con, $query)) {
$row = mysqli_fetch_row($result);
//printf("Default database is %s.\n", $row[0]);
printf($row[0]);
}
//throw new Exception();
} catch (Exception $e) {
print "Something went wrong /n" . $e;
} finally {
// do nothing
}
?>
As per How to change mysql to mysqli?
, I tried making the change mysql_query( -> mysqli_query($con, .
Navigating to ~/administrator/login.php results in a blank page. I edited ~/public_html/administrator/lib/connection.php - with the above change, nothing.
I'm guessing a WordPress upgrade to start then take it from there.
Any other suggestions?
I do not understand what is going wrong with code. The result is get is "connected successfully success Query failed". I tried few combinations and I get the same result. Please help me in solving this. Thanks in advance.
<?php
$link = mysql_connect('localhost', 'root1', '')
or die('Could not connect: ' . mysql_error());
if ($link) {
echo 'connected successfully';
}
$l = mysql_select_db('vtflix', $link) or die ('Could not select the database');
if ($l) {
echo ' success';
}
/*$varCNAME = 'John';
$varCONTENT = '4';
$varVID = '1';*/
$sql = "INSERT INTO mpaa(C_Name, ContentRating, V_ID) VALUES ('Jon', 4, 3)";
mysql_query($sql, $link) or die("Query failed");
$que = "SELECT * FROM mpaa";
$query = mysql_query($que, $link);
if (!$query) {
echo 'query failed';
}
while ($sqlrow = mysql_fetch_array($query, MYSQL_ASSOC)) {
$row = $sqlrow['C_Name'];
$nrow = $sqlrow['Content Rating'];
$mrow = $sqlrow['V_ID'];
echo "<br>" . $row . " " . $nrow . " " . $mrow . "<br>";
}
mysql_close($link);
?>
1.Don't use mysql_* library (deprecated from php5 onward + removed from php7) .Use mysqli_* OR PDO.
2.An example of mysqli_*(with your code)is given below:-
<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those errors
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>
Note:- To check php version (either on localhost or on live server) create a file with name phpInfo.php, and just write one line code in that file:-
<?php
phpinfo();
?>
Now run this file and you will get the current php version.
Like this:- https://eval.in/684551
Here it seems that you are using deprecated API of mysql_* .
1) Check your PHP version
<?php phpinfo();exit;//check version ?>
2) avoid the usage of mysql use mysqli or PDO
3) change your db connection string with this :
new Mysqlidb($hostname, $username, $pwd, $dbname);
example with you code
<?php
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>
I am starting to use mysqli and I got it working on my virtual server but can't get it working on my real server. The databases are the same. I have tried both store and get_result. Any idea what am I doing wrong?
Everything I try to echo, print or var_dump does not show up IF Place after execute but the command will be executed. It worked well on UPDATE and INSERT.
<?php
$mysqli = new mysqli("127.0.0.1", "user", "pass", "db", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$sam = 1;
$stmt = $mysqli->prepare("SELECT released FROM svers");
$stmt->execute();
echo "#";
var_dump($sam);
$res = $stmt->get_result();
var_dump($res);
$row = $res->fetch_assoc();
var_dump($row);
$mysqli->close();
The host_info is displayed as "127.0.0.1 via TCP/IP" followed By the # I echoed inside the if() but can't get anything from my database. Even the other # between the $row[ ] info are not showing. Here is my code:
<?php
$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$stmt = $mysqli->prepare("SELECT * FROM svers ORDER BY released DESC LIMIT 1");
if ($result = $stmt->execute()){
echo "#";
$result = $stmt->get_result();
$row = $result->fetch_array(MYSQLI_BOTH);
echo $row['version'] . "#" . $row['released'] . "#" . $row['note'];
$stmt->free_result();
}else {
echo "error";
}
$mysqli->close();
?>
Ok problem solved. My real server didn't support mysqlnd so can't use get_result(). Thanks for the help guys!
You are saying got it working on my virtual server but can't get it working on my real server.
Then check svers table on real server contains any data.
I creating a php file that pulls a view from an SQL database. Can someone let me know why this isn't working? It seems to be timing out. I am not getting a connection error, either. Thank you in advance.
<?php
require ('mysqli_connect.php');
$sql = "SELECT * FROM testview ;";
$result = mysqli_query($dbc,$sql);
// Check connection if ($dbc->connect_error) {
die("Connection failed: " . $dbc->connect_error); }
$result=mysqli_query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>userID</th><th>first_name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["userID"]."</td><td>".$row["first_name"]."</td></tr>";
}
echo "</table>"; } else {
echo "0 results"; }
}
$dbc->close();
?>
Here is the connection file
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'Test');
// Make the connection:
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' );
?>
give this a go. you were using mysqli and mysql in the same document. this somtime causes issues.
require_once ('mysqli_connect.php');
$q = "SELECT * FROM testview";
$r = mysqli_query($dbc, $q);
//there was no real need to check the connection, you should be doing this in your connection script.
//you where using 'mysqli' above and 'mysql' below.
$row = mysqli_fetch_array($r);
if ($r) {
echo "<table><tr><th>userID</th><th>first_name</th></tr>";
while ($row = mysqli_fetch_array($r)){
echo "<tr><td>" . $row["userID"] . "</td><td>" . $row["first_name"] . " " . $row["last_name"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
close($conn);
You do not have error messages turned on. Therefore when you are getting syntax errors and etc. they are not showing up and you are assuming a time-out. OR perhaps since you have a non-existent function in your die() call maybe it gets confused trying to die but not able to die.
Before you euthanize your code, turn error messages on. Your code will thank you.
Oh, and change
die("Connection failed: " . $dbc->connect_error);
to
die("Connection failed: " . mysql_error($dbc));
I've trying to display values from mysql but it return any empty page. The connection is fine but it does not fetch the data from mysql. I tried all the answers from the similar questions asked. But nothing helped. Can somebody please help me? This is the code
$con= mysql_connect($host, $username, $pwd);
if(!$con)
die("not connected". mysql_errno());
echo(Connected);
mysql_select_db("info",$con);
$query="select * from people";
$result= mysql_query($query,$con) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id']. " - ". $row['people_name'];
echo "<br />";
}
Try to check if your db user,password are correct! I test the code above :
<?php $con=mysqli_connect("localhost","root","","test"); // Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM people");
while($row = mysqli_fetch_array($result)) {
echo $row['id'] . " -- " . $row['people_name']; echo "<br>";
}
?>
and give me the result without error: 10 -- JOHN 11 -- PRADEEP
I just change mysql_connect to mysqli_connect add in $con= mysql_connect($host, $username, $pwd); a dbname. and $con become $con= mysqli_connect($host, $username, $pwd,$dbname); I use mysqli_query instead of mysql_query. Here is a stackQuestion for the mysql vs mysqli in php which can explain you the difference.
Try this
<?php
$con= mysql_connect('hostname', 'username', 'password');
if(!$con)
die("not connected". mysql_errno());
echo("Connected");
mysql_select_db("test",$con);
$query="select * from tabale_name";
$result= mysql_query($query,$con) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id']. " - ". $row['name'];
echo "<br />";
}
?>
check this
<?php
$con=mysqli_connect("hostname","username","password","info");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM people");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['people_name'];
echo "<br>";
}
?>
OR
<?php
$con=mysqli_connect("hostname","username","password");
// Check connection
if ($con)
{
echo "connected to db";
}
else
{
echo "not connected to db";
}
$db_selected = mysql_select_db("info", $con);
if (!$db_selected)
{
die ("Can\'t use info: " . mysql_error());
}
$result = mysqli_query("SELECT * FROM people");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['people_name'];
echo "<br>";
}
?>