I try to echo my database (hosted on ipage.com) using php and I get this error : Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, cgiadmin#yourhostingaccount.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
What is wrong? $mysqli = new mysqli("site.ipagemysql.com", "user", "pass", "database");
alone, works, so it is not a error in the connection right? or I have to contact the host?
My code
<?php
$mysqli = new mysqli("site.ipagemysql.com", "user", "pass", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", $mysqli->host_info);
$result = mysqli_query($link, "SELECT DATABASE()"))
$row = mysqli_fetch_row($result);
printf("Default database is %s.\n", $row[0]);
mysqli_free_result($result);
?>
You have one ) too many on line 12, it's missing a semicolon and you haven't changed the $link variable to correspond with the $mysqli variable on line 3.
$result = mysqli_query($mysqli, "SELECT DATABASE()");
Also, you're mixing object oriented style with procedural style. I'd recommend you to use OO style only. So the code would be:
<?php
$mysqli = new mysqli("site.ipagemysql.com", "user", "pass", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", $mysqli->host_info);
$result = mysqli->query("SELECT DATABASE()"))
$row = $result->fetch_row();
printf("Default database is %s.\n", $row[0]);
$result->close();
$mysqli->close();
?>
Related
This question already has answers here:
Turning query errors to Exceptions in MySQLi [duplicate]
(3 answers)
Closed 1 year ago.
I am using PHP and mysqli and trying to create the DB if it does not exist in my dbLogin file. Seems like it should be a fairly simple thing to do. It seems to be getting hung up on the first creation attempt with an error and won't move past or catch the error.
Here is my very simple code:
<?php
$con = '';
try {
$con = new mysqli("host", "user", "pass", "db");
}
catch(Exception $e) {
echo "Error: $e<br />";
$con = new mysqli("host", "user", "pass");
$con->query("CREATE DATABASE IF NOT EXISTS db;");
}
/* check connection */
if ($con->connect_errno) {
printf("Connect failed: %s\n", $con->connect_error);
exit();
}
?>
Now, when I run the code above, all I get is the following error:
Warning: mysqli::__construct(): (HY000/1049): Unknown database
'db' in xyz\DBLogin.php on line 6 Connect
failed: Unknown database 'db'
Line 6 is the line inside the try block. It does not create the database and does not move past the error.
Your problem is that mysqli::__construct only generates a warning if it can't connect to the database, and you can't directly catch a warning. Now you can workaround that (for example, see this question) but it's probably simpler to do something like this:
$con = new mysqli("host", "user", "pass");
if ($con->connect_errno) {
printf("Connect failed: %s\n", $con->connect_error);
exit();
}
if (!$con->select_db('db')) {
echo "Couldn't select database: " . $con->error;
if (!$con->query("CREATE DATABASE IF NOT EXISTS db;")) {
echo "Couldn't create database: " . $con->error;
}
$con->select_db('db');
}
//check if the database exist - PHP
$cons = '';
$cons = new mysqli("host", "user", "pass");
$okk = mysqli_query($cons, "SHOW DATABASES LIKE 'db'");
$okkno = mysqli_num_rows($okk);
if($okkno <=0){
$fanya = mysqli_query($cons, "CREATE DATABASE IF NOT EXISTS db;");
echo "Database created successifully";
}
I am stuck with my php code. Parent php page reload again after header() works, Here is my code
<?php
$mysqli = new mysqli("localhost", "root", "root", "testing");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$sql ="update test set hit_count = hit_count+1";
$result = $mysqli->query($sql);
header('Location: http://www.google.com/');
die();
?>
Here some times i got 2 hit_count from db.
How it works, i added die() after header().
You should not output text before using header redirects as seen on the code below:
/* Select queries return a resultset */
if ($result = $mysqli->query($sql)) {
printf("updated");
}else{
echo "failed";
}
i'm trying to use mysqli module for PHP5 and it doesn't work. The module is activated in php.ini. What could be the problem?
<?php
require_once("class/guestbook.php");
$host = "localhost";
$user = "root";
$pass = "";
$db = "guestbook";
//Connect
$c = new mysqli($host, $user, $pass, $db);
//Check connection
if (mysqli_connect_errno()) {
printf("Connection failed! %s\n", mysqli_connect_error());
exit();
}
//Return the name of current database
if($result = $c->query("SELECT_DATABASE();")) {
$row = $result->fetch_row();
printf("Default database is %s.\n", $row[0]);
$result->close();
}
//Close connection
$c->close();
?>
Thank you!
The only thing wrong with your neat code, most probably, is your query itself.
SELECT_DATABASE(); // returns an error for obvious reasons
Means nothing to mysql. This does
SELECT DATABASE();
You don't see the error message because you don't check for errors on query execution. You are only looking for connection errors. Try an else{} block for $c->query and print the error message to see it.
I am new on this MySQLi connection. I have system codes here and working on WAMP server localhost. I changed the old mysql connection to new mysqli connection and got rid of these "deprecated errors". But But my problem is that I could not login or access query (sql related function). What I am saying is that I got rid of deprecated errors but could not login/access my database.
//This is my OLD connection:
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("towertec_master") or die(mysql_error());
//This is my NEW connection
$link = mysqli_connect ("localhost", "root", "", "towertec_master") or die(mysql_error());
Also here is my OLD fetch connection:
$sql=mysql_query("SELECT organization_name,address,phone,email FROM system_config");
$row=mysql_fetch_assoc($sql);
$organization_name=stripslashes(trim($row['organization_name']));
$address=stripslashes(trim($row['address']));
$phone=stripslashes(trim($row['phone']));
$email=stripslashes(trim($row['email']));
NEW fetch connection:
$query = "SELECT organization_name, address, phone, email FROM system_config" or die("Error in the consult.." . mysqli_error($link));
$result = mysqli_query($link, $query);
As pointed out, your are still using the deprecated error function. Also, for your connection, you need to create the connection object which you do with "new".
$link = new mysqli_connect ("localhost", "root", "", "towertec_master");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
For your query, you are putting the error function in the wrong place.
$query = "SELECT organization_name, address, phone, email FROM system_config";
$result = mysqli_query($link, $query);
if (!$result) {
printf("Error: %s\n", mysqli_error($link));
}
I'm currently learning mysqli and systematically replacing all of my deprecated queries throughout my script.
I have this query:
<?php
$link = mysqli_connect("host", "user", "pass", "name");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM pins WHERE id='$pinDetails->id'";
$result = mysqli_query($link, $query);
/* associative array */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$feature = $row['featured'];
/* free result set */
mysqli_free_result($result);
/* close connection */
mysqli_close($link);
?>
Which I can echo throughout the page as <?php echo $row['date_featured']; ?>
My question is how do I rework the code to remove the connection? I don't want to keep connecting to the db in every query when I have a general connection include at the top of the page.
Actually you need connection for this page to operate based on your SQL database ,, can you explain more that what do you mean by removing connection
you can use INCLUDE();